1.老黄历的请求
http.laohuangli.py如下
import requests class HttpRequest:def http_request(self,data): headers = {'User-Agent': 'Mozilla/5.0'} url = 'http://v.juhe.cn/laohuangli/d' res = requests.post(url,data,headers = headers) return res.json()
2.编写测试用例laohuangli_testcase.py,主要针对传入的参数data
import unittest from auto_Testwork.laohuangli_unittest.http_laohuangli_request import HttpRequest suite = unittest.TestSuite() # 存储用例 class LaoHuangli(unittest.TestCase): # 1.正确的响应结果:"error_code":0 'reason': 'successed' def test_right(self): data = {"key":"4d71d4e7d0242d755352ff2fe85dc333","date":"2019-11-27"} res = HttpRequest().http_request(data) self.assertEqual('successed', res['reason'], '查询失败') def test_right2(self): data = {"key":"4d71d4e7d0242d755352ff2fe85dc333","date":"2019/11/27"} res = HttpRequest().http_request(data) self.assertEqual('successed', res['reason'], '查询失败') def test_right3(self): data = {"key":"4d71d4e7d0242d755352ff2fe85dc333","date":"2019-11-27 00:00:00"} res = HttpRequest().http_request(data) self.assertEqual('successed', res['reason'], '查询失败') # 2.data为空:{'result': None, 'resultcode': '101', 'reason': '错误的请求KEY', 'error_code': 10001} def test_data_null(self): data = {} res = HttpRequest().http_request(data) self.assertEqual('101', res['resultcode'], 'resultcode应为101') # 3.key为空:{'error_code': 10001, 'resultcode': '101', 'reason': '错误的请求KEY', 'result': None} def test_key_null(self): data = {"key":"","date":"2019-11-14"} res = HttpRequest().http_request(data) self.assertEqual('101', res['resultcode'], 'resultcode应为101') # 4.key错误:{'error_code': 10001, 'resultcode': '101', 'reason': '错误的请求KEY', 'result': None} def test_key_error(self): data = {"key":"4d71d4e7d0242d755352ff2fe85dc1dc","date":"2019-11-14"} res = HttpRequest().http_request(data) self.assertEqual('101', res['resultcode'], 'resultcode应为101') # 5.date 为空: {"reason":"日志不能为空","result":null,"error_code":206501} def test_date_null(self): data = {"key":"4d71d4e7d0242d755352ff2fe85dc333","date":""} res = HttpRequest().http_request(data) self.assertEqual(206501, res['error_code'], 'error_code应为206501') # 6.date信息不完整 def test_date_error(self): data = {"key":"4d71d4e7d0242d755352ff2fe85dc333","date":"2019-11"} res = HttpRequest().http_request(data) self.assertEqual(None, res['result'], 'result应为None') def test_date_error2(self): #{'error_code': 0, 'reason': 'successed', 'result': None} data = {"key":"4d71d4e7d0242d755352ff2fe85dc333","date":"2019"} res = HttpRequest().http_request(data) self.assertEqual(None, res['result'], 'result应为None') # 7.date超过上限:2026 (上限为:2025-12-31) def test_date_invalid(self): data = {"key": "4d71d4e7d0242d755352ff2fe85dc333", "date": "2026-01-01"} res = HttpRequest().http_request(data) self.assertEqual(None, res['result'], 'result应为None') if __name__ =='__main__': unittest.main()
3.执行测试用例,并生成测试报告
import HTMLTestRunner # 写好一个模块,可以直接调用 import unittest from laohuangli_testcase import LaoHuangli suite = unittest.TestSuite() # 存储用例 loader = unittest.TestLoader() # 创建一个加载器 suite.addTest(loader.loadTestsFromTestCase(LaoHuangli)) #具体到类 with open('test_laohuangli_report.html','wb') as file: runner = HTMLTestRunner.HTMLTestRunner(stream=file, verbosity=2, title='12.02老黄历测试报告单', description='老黄历测试报告详情如下' ) runner.run(suite) if'__name__' == '__main__': unittest.TestCase()
测试报告单如下: