加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 建站 > 正文

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

发布时间:2019-07-18 13:37:37 所属栏目:建站 来源:Atstudy网校
导读:年头参加到一个靠山体系开拓的项目中,内里涉及了许多接口,我做为项目组测试职员,必要对这些接口举办测试,一开始行使 postman 器材测试,极端利便。但跟着接口数目的增进,不仅要执行手动点击测试,并且,一旦接口参数变换,都从头变动接口参数,次数多

HTMLTestRunner 框架可用来天生可视化测试陈诉,并能很好的与 unittest 框架团结行使,接下来我们以一段代码来展示一下 HTMLTestRunner 的行使。

  1. if __name__=='__main__': 
  2.  
  3. from HTMLTestRunner import HTMLTestRunner 
  4.  
  5. testData = [ 
  6.  
  7. (10, 9, 19), 
  8.  
  9. (12, 13, 25), 
  10.  
  11. (12, 10, 22), 
  12.  
  13. (2, 4, 6) 
  14.  
  15.  
  16. suite = unittest.TestSuite() 
  17.  
  18. for i in testData: 
  19.  
  20. suite.addTest(ExtendTestCaseParams.parametrize(ApiTestSample,'test_jiafa',canshu=i)) 
  21.  
  22. currentTime = time.strftime("%Y-%m-%d %H_%M_%S") 
  23.  
  24. result_path = './test_results' 
  25.  
  26. if not os.path.exists(path): 
  27.  
  28. os.makedirs(path) 
  29.  
  30. report_path = result_path + '/' + currentTime + "_report.html" 
  31.  
  32. reportTitle = '测试陈诉' 
  33.  
  34. desc = u'测试陈诉详情' 
  35.  
  36. with open(report_path, 'wd') as f: 
  37.  
  38. runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc) 
  39.  
  40. runner.run(suite) 

测试功效如下:

下面具体讲授一下 html 陈诉的天生代码:

  1. runner = HTMLTestRunner(stream=fp, title=reportTitle, description=desc) 

HTMLTestRunner 中的 stream 暗示输入流,这里我们将文件描写符转达给 stream,title 参数暗示要输出的测试陈诉主题名称,description 参数是对测试陈诉的描写。在行使 HTMLTestRunner 时,有几点必要留意:

1)HTMLTestRunner 模块非 Python 自带库,必要到 HTMLTestRunner 的官网下载

该安装包;

2)官网的 HTMLTestRunner 模块仅支持 Python 2.x 版本,假如要在 Python 3.x中,必要修改部门代码,修改的代码部门请自行上网搜刮;

假如必要天生 xml 名目,只需将上面代码中的

  1. runner = HTMLTestRunner(stream=fp, title=reportTitle, description=desc) 
  2.  
  3. runner.run(suite) 

修改为如下代码

  1. import xmlrunner 
  2.  
  3. runner = xmlrunner.XMLTestRunner(output='report') 
  4.  
  5. runner.run(suite) 

4、接口测试分类

前面各人对接口哀求,测试框架和测试功效可视化方面有了深入的相识。有了前面的基本,对付接下来领略和编写接口测试会有很大辅佐。这里我们先来讲授一下接口测试与单位测试的区别。单位测试只针对函数举办多组参数测试,包罗正常和非常参数组合。而接口测试是针对某一接口举办多组参数测试。现实接口测试中,我们又将接口测试分为两种:

1)单接口测试;

2)多接口测试。

对付单接口测试,只需针对单个接口测试,测试数据按照接口文档中的参数法则来计划测试用例;对多接口测试,起主要确保接口之间挪用逻辑正确,然后再按照接口文档中的参数法则来计划用例举办测试。下面我就按照这两种差异环境的接口测试,用现实项目代码展示一下。

4.1 单接口测试

  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 = 'http://%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 test_register(self): 
  24.  
  25. for index, value in enumerate(self.param): 
  26.  
  27. print('Test Token {0} parameter is {1}'.format(index, value)) 
  28.  
  29. self.ip = self.param[1] 
  30.  
  31. self.name = self.param[2] 
  32.  
  33. self.desc = self.param[3] 
  34.  
  35. self.expectedValue = self.param[4] 
  36.  
  37. req = self.grant_register(self.ip, self.name, self.desc) 
  38.  
  39. self.assertIn(req.status_code, self.expectedValue, msg="Test Failed.") 
  40.  
  41. if __name__=='__main__': 
  42.  
  43. import random 
  44.  
  45. import string 
  46.  
  47. ip = '172.36.17.108' 
  48.  
  49. testData = [ 
  50.  
  51. (1, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200), 
  52.  
  53. (2, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200), 
  54.  
  55. (3, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200) 
  56.  
  57.  
  58. suite = unittest.TestSuite() 
  59.  
  60. for i in testData: 
  61.  
  62. suite.addTest(ExtendTestCaseParams.parametrize(TestApiSample,'test_register',canshu=i)) 
  63.  
  64. currentTime = time.strftime("%Y-%m-%d %H_%M_%S") 
  65.  
  66. path = './results' 
  67.  
  68. if not os.path.exists(path): 
  69.  
  70. os.makedirs(path) 
  71.  
  72. report_path = path + '/' + currentTime + "_report.html" 
  73.  
  74. reportTitle = '接口测试陈诉' 
  75.  
  76. desc = u'接口测试陈诉详情' 
  77.  
  78. with open(report_path, 'wd') as f: 
  79.  
  80. runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc) 
  81.  
  82. runner.run(suite) 

(编辑:湖南网)

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

热点阅读