加入收藏 | 设为首页 | 会员中心 | 我要投稿 我爱故事小小网_铜陵站长网 (http://www.0562zz.com/)- 视频终端、云渲染、应用安全、数据安全、安全管理!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

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

发布时间:2019-07-17 21:07:09 所属栏目:优化 来源:Atstudy网校
导读:副标题#e# 年初参与到一个后台系统开发的项目中,里面涉及了很多接口,我做为项目组测试人员,需要对这些接口进行测试,一开始使用 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) 

(编辑:我爱故事小小网_铜陵站长网)

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

热点阅读