加入收藏 | 设为首页 | 会员中心 | 我要投稿 新余站长网 (https://www.0790zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

Python接口测试自动化实战及代码示例:含Get、Post等方法

发布时间:2019-07-17 17:40:33 所属栏目:优化 来源:Atstudy网校
导读:年初参与到一个后台系统开发的项目中,里面涉及了很多接口,我做为项目组测试人员,需要对这些接口进行测试,一开始使用 postman 工具测试,很是方便。但随着接口数量的增加,不光要执行手动点击测试,而且,一旦接口参数变动,都重新更改接口参数,次数多

上述代码中的 register()为注册接口函数,test_register()为测试注册接口函数,testData 为测试数据,这里没有完全做到测试脚本与测试数据分离。为了实现测试数据与测试脚本分离,可以将 testData 列表单独写在文本文件或者数据库中,运行测试脚本时再去加载这些数据,就能实现测试脚本与测试数据的分离。

4.2 多接口测试

  1. class TestApiSample(ExtendTestCaseParams): 
  2.  
  3. def setUp(self): 
  4.  
  5. pass 
  6.  
  7. def tearDown(self): 
  8.  
  9. pass 
  10.  
  11. def register(self, ip, name, desc): 
  12.  
  13. url = 'https://%s/api/v1/reg' % ip 
  14.  
  15. headers = {"Content-Type": "application/x-www-form-urlencoded"} 
  16.  
  17. para = {"app_name": name, "description": desc} 
  18.  
  19. req = self.Post(url, para, headers) 
  20.  
  21. return req 
  22.  
  23. def oauth2_basic(self, ip, name, desc): 
  24.  
  25. apps = self.register(ip, name, desc) 
  26.  
  27. apps = apps.json() 
  28.  
  29. url = 'http://%s/api/v1/basic' % ip 
  30.  
  31. data = {"client_id":apps['appId'], "client_secret":apps['appKey']} 
  32.  
  33. headers = None 
  34.  
  35. req = requests.post(url, data, headers) 
  36.  
  37. basic = str(req.content, encoding='utf-8') 
  38.  
  39. return apps, basic, req 
  40.  
  41. def test_oauth2_basic(self): 
  42.  
  43. count = 0 
  44.  
  45. for i in self.param: 
  46.  
  47. count += 1 
  48.  
  49. self.ip = self.param[1] 
  50.  
  51. self.name = self.param[2] 
  52.  
  53. self.desc = self.param[3] 
  54.  
  55. self.expected = self.param[4] 
  56.  
  57. apps, basic, req = self.oauth2_basic(self.ip, self.name, self.desc) 
  58.  
  59. self.assertIn(req.status_code, self.expected, msg="Grant Failed.") 
  60.  
  61. if __name__=='__main__': 
  62.  
  63. import random 
  64.  
  65. import string 
  66.  
  67. ipAddr = '172.36.17.108' 
  68.  
  69. testData = [ 
  70.  
  71. (1, ipAddr, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200), 
  72.  
  73. (2, ipAddr, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200), 
  74.  
  75. (3, ipAddr, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200) 
  76.  
  77.  
  78. suite = unittest.TestSuite() 
  79.  
  80. for i in testData: 
  81.  
  82. suite.addTest(ExtendTestCaseParams.parametrize(TestApiSample, 'test_oauth2_basic', 
  83.  
  84. canshu=i)) 
  85.  
  86. currentTime = time.strftime("%Y-%m-%d %H_%M_%S") 
  87.  
  88. path = '../Results' 
  89.  
  90. if not os.path.exists(path): 
  91.  
  92. os.makedirs(path) 
  93.  
  94. report_path = path + '/' + currentTime + "_report.html" 
  95.  
  96. reportTitle = '接口测试报告' 
  97.  
  98. desc = u'接口测试报告详情' 
  99.  
  100. with open(report_path, 'wd') as f: 
  101.  
  102. runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc) 
  103.  
  104. runner.run(suite) 

(编辑:新余站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读