当前位置:   article > 正文

接口框架 python+unittest+request+HTMLTestRunner

接口框架 python+unittest+request+HTMLTestRunner

request的各种方法主要用来处理客户端浏览器提交的请求中的各项参数和选项。如POST,GET最常用的两种请求

官方文档:http://docs.python-requests.org/en/master/user/quickstart/#

安装方法:https://www.cnblogs.com/cheng10/p/9875207.html

unittest:https://docs.python.org/3/library/unittest.html

简单小练习:https://www.cnblogs.com/cheng10/p/10431310.html

HTMLTestRunner配置方法:https://www.cnblogs.com/cheng10/p/10438156.html

开正文,上代码

  1. #导入需要用到的模块
  2. import requests
  3. import unittest
  4. from HTMLTestRunner import HTMLTestRunner
  5. import time
  6. import os
  7. import sys
  8. sys.path.append('C:/Users/Desktop/requests_test.py')
  9. ABSPATH = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))
  10. class MyTest (unittest.TestCase):
  11. #单元测试必须以setUP开头
  12. def setUp(self):
  13. pass
  14. def test_case(self):
  15. self.r = requests.get("https://www.baidu.com")
  16. self.r.status_code
  17. self.r.encoding = 'utf-8'
  18. self.r.text
  19. # r.json()
  20. assert self.r.status_code == 200
  21. assert "百度一下" in self.r.text
  22. def tearDown(self):
  23. pass
  24. if __name__=="__main__":
  25. #unittest.main()
  26. suite =unittest.TestSuite()
  27. suite.addTest(MyTest("test_case"))
  28. #定义date日期,time时间
  29. date =time.strftime("%Y%m%d")
  30. time =time.strftime("%Y%m%d-%H%M%S")
  31. #保存报告路径
  32. path ="./report/api"
  33. if not os.path.exists(path):
  34. os.makedirs(path)
  35. else:
  36. pass
  37. report_path =path + time + "report.html"
  38. report_title =u"测试报告"
  39. desc =u"接口自动化测试报告详情"
  40. with open(report_path,'wb') as report:
  41. runner =HTMLTestRunner(stream=report,title=report_title,description=desc)
  42. runner.run(suite)
  43. report.close()

  

最后结果提醒HTMLTestRunner一定要配置正确

就成功啦

 

 

转载于:https://www.cnblogs.com/cheng10/p/10511177.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/249865
推荐阅读
相关标签
  

闽ICP备14008679号