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
开正文, 上代码
- # 导入需要用到的模块
- 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 一定要配置正确
就成功啦
来源: http://www.bubuko.com/infodetail-2983937.html