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,后續更新。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM