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

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

发布时间:2019-07-18 18:13:56 所属栏目:建站 来源: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中,需要修改部分代码,修改的代码部分请自行上网搜索;

(编辑:萍乡站长网)

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

推荐文章
    热点阅读