python requests+unittest+HTTPTestRunner 完整示例(未带发送邮件)

 需要下载第三方库:requests(构造请求库),HTTPTestRunner(生产测试报告),ddt(数据驱动库);python自带库:unittest
 1 #!/usr/bin/env python
 2 
 3 import requests
 4 import unittest
 5 import time
 6 from Frist_request_demo import HTTPTestRunner
 7 
 8 class HttpbinRuquetTest(unittest.TestCase):
 9     def setUp(self) -> None:
10         pass
11 
12     def tearDown(self) -> None:
13         pass
14 
15     def test_httpbin_get_request(self):
16         """
17         get请求,参数说明:
18         @URL:请求url地址
19         @hearders:请求的头部信息
20         @params:请求发送参数
21         @verify:ssl证书验证,可选
22         @timeout:请求超时设置,可选
23         @proxies:代理ip,可选
24         :return: 响应json和状态码
25         """
26 
27         url = 'http://httpbin.org/get'
28         headers = {
29         "Accept": "application/json",
30         "Accept-Encoding": "gzip, deflate",
31         "Accept-Language": "zh-CN,zh;q=0.9",
32         "Host": "httpbin.org",
33         "Referer": "http://httpbin.org/",
34         "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
35         }
36         data = {"name":"tom","age":18}
37 
38         res = requests.get(url=url,headers=headers,params=data,timeout=3,verify=False)
39         response_status = res.status_code
40         response_json = res.json()
41 
42         print(response_status,response_json)
43 
44 
45     def test_httpbin_post_request(self):
46         """
47         @url:请求URL
48         @headers:请求头部信息
49         @data:请求发送数据
50         :return:
51         """
52         url = 'http://httpbin.org/post'
53         headers = {
54             "Accept": "application/json",
55             "Accept-Encoding": "gzip, deflate",
56             "Accept-Language": "zh-CN,zh;q=0.9",
57             "Host": "httpbin.org",
58             "Referer": "http://httpbin.org/",
59             "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
60         }
61         payload = {"name": "tom", "age": 18}
62 
63         res = requests.post(url=url,headers=headers,data=payload)
64         response_json = res.json()
65 
66         print(response_json)
67 
68 
69 if __name__ == '__main__':
70     """
71     @test_dir:说明测试用例的存放路径
72     @discover:载装目录下所有符合pattern要求的测试文件
73     @report_filename:测试报告的文件名,是html文件
74     @runner:构造HTTPTestRunner实例,运行discover的文件下测试用例
75     """
76     test_dir = "../script"
77     discover = unittest.defaultTestLoader.discover(test_dir, pattern='*_test.py')
78     report_filename = f'../data/reports/tmp_{time.strftime("%Y%m%d%H%M%S")}.html'
79 
80     with open(report_filename,'w',encoding='utf-8') as f:
81         runner = HTTPTestRunner.HTMLTestRunner(stream=f,title='httpbin.org请求测试报告',description="仅做参考")
82         runner.run(discover)

HTTPTestRunner.py文件是经过修改后,不然容易遇到各种兼容python3问题,下载链接:链接:https://pan.baidu.com/s/1sDm08eWhEikP-0igSx_tFg  密码:i2eg。 ddt是python数据驱动包,此次因展示一个简单requests,后续更新。。。

原文地址:https://www.cnblogs.com/shiyuheng/p/12163062.html