request的各類方法主要用來處理客戶端瀏覽器提交的請求中的各項參數和選項。如POST,GET最經常使用的兩種請求html
官方文檔:http://docs.python-requests.org/en/master/user/quickstart/#python
安裝方法:http://www.javashuo.com/article/p-cxjrmkyj-bv.htmljson
unittest:https://docs.python.org/3/library/unittest.htmlapi
簡單小練習:http://www.javashuo.com/article/p-zhltvevb-ew.html瀏覽器
HTMLTestRunner配置方法:http://www.javashuo.com/article/p-vpofxttx-dd.htmlapp
開正文,上代碼單元測試
#導入須要用到的模塊
import requests
import unittest
from HTMLTestRunner import HTMLTestRunner
import time
import os
import sys
sys.path.append('C:/Users/Desktop/requests_test.py')
ABSPATH = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))
class MyTest (unittest.TestCase):
#單元測試必須以setUP開頭
def setUp(self):
pass
def test_case(self):
self.r = requests.get("https://www.baidu.com")
self.r.status_code
self.r.encoding = 'utf-8'
self.r.text
# r.json()
assert self.r.status_code == 200
assert "百度一下" in self.r.text
def tearDown(self):
pass
if __name__=="__main__":
#unittest.main()
suite =unittest.TestSuite()
suite.addTest(MyTest("test_case"))
#定義date日期,time時間
date =time.strftime("%Y%m%d")
time =time.strftime("%Y%m%d-%H%M%S")
#保存報告路徑
path ="./report/api"
if not os.path.exists(path):
os.makedirs(path)
else:
pass
report_path =path + time + "report.html"
report_title =u"測試報告"
desc =u"接口自動化測試報告詳情"
with open(report_path,'wb') as report:
runner =HTMLTestRunner(stream=report,title=report_title,description=desc)
runner.run(suite)
report.close()
最後結果提醒HTMLTestRunner必定要配置正確測試
就成功啦ui