接下来。装逼开始....
接口自动化会牵扯到pytest框架的知识点,公众号中有pytest框架的使用文章总结,可自行翻阅……
安装pytest
pip install pytest
更新pytest
pip install -U pytest
安装完成后记得在pycharm中把默认框架unittests修改称pytest框架运行...
测试函数
写一条用例是,可以直接DEF定义测试函数,必须TEST_开头,不然pytest框架识别不到测试用例:
?
import requests
def test_example():
with requests.Session() as s:
url = "http://apis.juhe.cn/fapig/euro2020/schedule"
params = {"type": 1, "key": "9d0dfd9dbaf51de283ee8a88e58e218b"}
response = s.get(url, params=params)
print(response)
或者直接写多条例子,以此类推:
import requests
def test_example():
with requests.Session() as s:
url = "http://apis.juhe.cn/fapig/euro2020/schedule"
params = {"type": 1, "key": "9d0dfd9dbaf51de283ee8a88e58e218b"}
response = s.get(url, params=params)
print(response)
def test_example_01():
with requests.Session() as s:
url = "http://apis.juhe.cn/fapig/euro2020/schedule"
params = {"type": 1, "key": "9d0dfd9dbaf51de283ee8a88e58e218b"}
response = s.get(url, params=params)
print(response)
测试类
测试类必须是测试类用例,使用pytest框架识别不到测试类用例
import pytest
import requests
class TestExample:
s = requests.Session()
def test_example(self, ):
with self.s as s:
url = "http://apis.juhe.cn/fapig/euro2020/schedule"
params = {"type": 1, "key": "9d0dfd9dbaf51de283ee8a88e58e218b"}
response = s.get(url, params=params)
print(response)
def test_example_01(self):
with self.s as s:
url = "http://apis.juhe.cn/fapig/euro2020/schedule"
params = {"type": 1, "key": "9d0dfd9dbaf51de283ee8a88e58e218b"}
response = s.get(url, params=params)
print(response)
if __name__ == '__main__':
pytest.main(["-v", "-s", "test_example"])
相比测试函数,测试类能更好的归纳测试用例...
断言
接口只要请求成功,就会返回200;200只是接口请求成功,并不意味着真正的成功;所以得把接口的数据返回出来,再校验数据是否符合预期结果
可以直接断言两值相等
import requests
?
?
def test_example():
with requests.Session() as s:
url = "http://apis.juhe.cn/fapig/euro2020/schedule"
params = {"type": 1, "key": "9d0dfd9dbaf51de283ee8a88e58e218b"}
response = s.get(url, params=params)
assert response.json()["reason"] == "查询成功!"
============================= test session starts ==============================
collecting ... collected 1 item
?
test_example.py::test_example PASSED [100%]{'reason': '查询成功!', 'result': {'data': [{'schedule_date': '2021-06-12', 'schedule_date_format': '06月12日', 'schedule_week': '周六', 'schedule_current': '0', 'schedule_list': [{'date': '2021-06-12', 'date_time': '2021-06-12 03:00:00', 'teama_name': '土耳其', 'teamb_name': '意大利', 'teama_score': '0', 'teamb_score': '3', 'match_status': '3', 'match_des': '完赛', 'match_type': '1', 'match_type_des': 'A组第1轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/A2.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/A3.png'}, {'date': '2021-06-12', 'date_time': '2021-06-12 21:00:00', 'teama_name': '威尔士', 'teamb_name': '瑞士', 'teama_score': '1', 'teamb_score': '1', 'match_status': '3', 'match_des': '完赛', 'match_type': '1', 'match_type_des': 'A组第1轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/A4.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/A1.png'}]}, {'schedule_date': '2021-06-13', 'schedule_date_format': '06月13日', 'schedule_week': '周日', 'schedule_current': '1', 'schedule_list': [{'date': '2021-06-13', 'date_time': '2021-06-13 00:00:00', 'teama_name': '丹麦', 'teamb_name': '芬兰', 'teama_score': '0', 'teamb_score': '1', 'match_status': '3', 'match_des': '完赛', 'match_type': '1', 'match_type_des': 'B组第1轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/B5.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/B7.png'}, {'date': '2021-06-13', 'date_time': '2021-06-13 03:00:00', 'teama_name': '比利时', 'teamb_name': '俄罗斯', 'teama_score': '3', 'teamb_score': '0', 'match_status': '3', 'match_des': '完赛', 'match_type': '1', 'match_type_des': 'B组第1轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/B6.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/B8.png'}, {'date': '2021-06-13', 'date_time': '2021-06-13 21:00:00', 'teama_name': '英格兰', 'teamb_name': '克罗地亚', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'D组第1轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/D14.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/D16.png'}]}, {'schedule_date': '2021-06-14', 'schedule_date_format': '06月14日', 'schedule_week': '周一', 'schedule_current': '0', 'schedule_list': [{'date': '2021-06-14', 'date_time': '2021-06-14 00:00:00', 'teama_name': '奥地利', 'teamb_name': '北马其顿', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'C组第1轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/C12.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/C9.png'}, {'date': '2021-06-14', 'date_time': '2021-06-14 03:00:00', 'teama_name': '荷兰', 'teamb_name': '乌克兰', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'C组第1轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/C11.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/C10.png'}, {'date': '2021-06-14', 'date_time': '2021-06-14 21:00:00', 'teama_name': '苏格兰', 'teamb_name': '捷克', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'D组第1轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/D13.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/D15.png'}]}, {'schedule_date': '2021-06-15', 'schedule_date_format': '06月15日', 'schedule_week': '周二', 'schedule_current': '0', 'schedule_list': [{'date': '2021-06-15', 'date_time': '2021-06-15 00:00:00', 'teama_name': '波兰', 'teamb_name': '斯洛伐克', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'E组第1轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/E18.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/E17.png'}, {'date': '2021-06-15', 'date_time': '2021-06-15 03:00:00', 'teama_name': '西班牙', 'teamb_name': '瑞典', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'E组第1轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/E19.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/E20.png'}]}, {'schedule_date': '2021-06-16', 'schedule_date_format': '06月16日', 'schedule_week': '周三', 'schedule_current': '0', 'schedule_list': [{'date': '2021-06-16', 'date_time': '2021-06-16 00:00:00', 'teama_name': '匈牙利', 'teamb_name': '葡萄牙', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'F组第1轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/F21.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/F24.png'}, {'date': '2021-06-16', 'date_time': '2021-06-16 03:00:00', 'teama_name': '法国', 'teamb_name': '德国', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'F组第1轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/F22.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/F23.png'}, {'date': '2021-06-16', 'date_time': '2021-06-16 21:00:00', 'teama_name': '芬兰', 'teamb_name': '俄罗斯', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'B组第2轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/B7.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/B8.png'}]}, {'schedule_date': '2021-06-17', 'schedule_date_format': '06月17日', 'schedule_week': '周四', 'schedule_current': '0', 'schedule_list': [{'date': '2021-06-17', 'date_time': '2021-06-17 00:00:00', 'teama_name': '土耳其', 'teamb_name': '威尔士', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'A组第2轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/A2.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/A4.png'}, {'date': '2021-06-17', 'date_time': '2021-06-17 03:00:00', 'teama_name': '意大利', 'teamb_name': '瑞士', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'A组第2轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/A3.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/A1.png'}, {'date': '2021-06-17', 'date_time': '2021-06-17 21:00:00', 'teama_name': '乌克兰', 'teamb_name': '北马其顿', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'C组第2轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/C10.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/C9.png'}]}, {'schedule_date': '2021-06-18', 'schedule_date_format': '06月18日', 'schedule_week': '周五', 'schedule_current': '0', 'schedule_list': [{'date': '2021-06-18', 'date_time': '2021-06-18 00:00:00', 'teama_name': '丹麦', 'teamb_name': '比利时', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'B组第2轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/B5.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/B6.png'}, {'date': '2021-06-18', 'date_time': '2021-06-18 03:00:00', 'teama_name': '荷兰', 'teamb_name': '奥地利', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'C组第2轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/C11.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/C12.png'}, {'date': '2021-06-18', 'date_time': '2021-06-18 21:00:00', 'teama_name': '瑞典', 'teamb_name': '斯洛伐克', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'E组第2轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/E20.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/E17.png'}]}, {'schedule_date': '2021-06-19', 'schedule_date_format': '06月19日', 'schedule_week': '周六', 'schedule_current': '0', 'schedule_list': [{'date': '2021-06-19', 'date_time': '2021-06-19 00:00:00', 'teama_name': '克罗地亚', 'teamb_name': '捷克', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'D组第2轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/D16.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/D15.png'}, {'date': '2021-06-19', 'date_time': '2021-06-19 03:00:00', 'teama_name': '英格兰', 'teamb_name': '苏格兰', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'D组第2轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/D14.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/D13.png'}, {'date': '2021-06-19', 'date_time': '2021-06-19 21:00:00', 'teama_name': '匈牙利', 'teamb_name': '法国', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'F组第2轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/F21.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/F22.png'}]}, {'schedule_date': '2021-06-20', 'schedule_date_format': '06月20日', 'schedule_week': '周日', 'schedule_current': '0', 'schedule_list': [{'date': '2021-06-20', 'date_time': '2021-06-20 00:00:00', 'teama_name': '葡萄牙', 'teamb_name': '德国', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'F组第2轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/F24.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/F23.png'}, {'date': '2021-06-20', 'date_time': '2021-06-20 03:00:00', 'teama_name': '西班牙', 'teamb_name': '波兰', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'E组第2轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/E19.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/E18.png'}]}, {'schedule_date': '2021-06-21', 'schedule_date_format': '06月21日', 'schedule_week': '周一', 'schedule_current': '0', 'schedule_list': [{'date': '2021-06-21', 'date_time': '2021-06-21 00:00:00', 'teama_name': '瑞士', 'teamb_name': '土耳其', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'A组第3轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/A1.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/A2.png'}, {'date': '2021-06-21', 'date_time': '2021-06-21 00:00:00', 'teama_name': '意大利', 'teamb_name': '威尔士', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'A组第3轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/A3.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/A4.png'}]}, {'schedule_date': '2021-06-22', 'schedule_date_format': '06月22日', 'schedule_week': '周二', 'schedule_current': '0', 'schedule_list': [{'date': '2021-06-22', 'date_time': '2021-06-22 00:00:00', 'teama_name': '乌克兰', 'teamb_name': '奥地利', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'C组第3轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/C10.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/C12.png'}, {'date': '2021-06-22', 'date_time': '2021-06-22 00:00:00', 'teama_name': '北马其顿', 'teamb_name': '荷兰', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'C组第3轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/C9.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/C11.png'}, {'date': '2021-06-22', 'date_time': '2021-06-22 03:00:00', 'teama_name': '芬兰', 'teamb_name': '比利时', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'B组第3轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/B7.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/B6.png'}, {'date': '2021-06-22', 'date_time': '2021-06-22 03:00:00', 'teama_name': '俄罗斯', 'teamb_name': '丹麦', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'B组第3轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/B8.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/B5.png'}]}, {'schedule_date': '2021-06-23', 'schedule_date_format': '06月23日', 'schedule_week': '周三', 'schedule_current': '0', 'schedule_list': [{'date': '2021-06-23', 'date_time': '2021-06-23 03:00:00', 'teama_name': '克罗地亚', 'teamb_name': '苏格兰', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'D组第3轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/D16.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/D13.png'}, {'date': '2021-06-23', 'date_time': '2021-06-23 03:00:00', 'teama_name': '捷克', 'teamb_name': '英格兰', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'D组第3轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/D15.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/D14.png'}]}, {'schedule_date': '2021-06-24', 'schedule_date_format': '06月24日', 'schedule_week': '周四', 'schedule_current': '0', 'schedule_list': [{'date': '2021-06-24', 'date_time': '2021-06-24 00:00:00', 'teama_name': '瑞典', 'teamb_name': '波兰', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'E组第3轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/E20.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/E18.png'}, {'date': '2021-06-24', 'date_time': '2021-06-24 00:00:00', 'teama_name': '斯洛伐克', 'teamb_name': '西班牙', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'E组第3轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/E17.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/E19.png'}, {'date': '2021-06-24', 'date_time': '2021-06-24 03:00:00', 'teama_name': '葡萄牙', 'teamb_name': '法国', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'F组第3轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/F24.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/F22.png'}, {'date': '2021-06-24', 'date_time': '2021-06-24 03:00:00', 'teama_name': '德国', 'teamb_name': '匈牙利', 'teama_score': '-', 'teamb_score': '-', 'match_status': '1', 'match_des': '未开赛', 'match_type': '1', 'match_type_des': 'F组第3轮', 'teama_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/F23.png', 'teamb_logo_url': 'https://juhe.oss-cn-hangzhou.aliyuncs.com/api_image/616/circular/F21.png'}]}], 'ext': {'current_match_type': '1', 'current_match_type_des': '小组赛'}}, 'error_code': 0}
?
?
============================== 1 passed in 0.58s ===============================
?
Process finished with exit code 0
或者增加逻辑运算进行断言
class TestExample:
s = requests.Session()
?
def test_example(self):
with self.s as s:
url = "http://apis.juhe.cn/fapig/euro2020/schedule"
params = {"type": 1, "key": "9d0dfd9dbaf51de283ee8a88e58e218b"}
response = s.get(url, params=params)
?
assert response.json()["reason"] == "查询成功!" and response.status_code == 200
?
?
if __name__ == '__main__':
pytest.main(["-v", "-s", "test_example"])
assert就是断言的关键字,如果两个结果不相等,就会抛错误.....
============================= test session starts ==============================
collecting ... collected 1 item
?
test_example.py::test_example FAILED [100%]
test_example.py:11 (test_example)
查询成功! != 查询成功
?
Expected :查询成功
Actual :查询成功!
<Click to see difference>
?
def test_example():
with requests.Session() as s:
url = "http://apis.juhe.cn/fapig/euro2020/schedule"
params = {"type": 1, "key": "9d0dfd9dbaf51de283ee8a88e58e218b"}
response = s.get(url, params=params)
> assert response.json()["reason"] == "查询成功"
E AssertionError: assert '查询成功!' == '查询成功'
?
test_example.py:19: AssertionError
?
?
============================== 1 failed in 0.60s ===============================
?
Process finished with exit code 1
从捕获的异常可以清晰的看到错误信息,是实际结果和预期结果不相等,抛出了AssertionError错误信息
至此,第一个自动化用例运行成功
?
以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你,如有疑问、歧义,直接私信留言会及时修正发布;感觉还不错记得点赞呦,谢谢!
未完,待续…
一直都在努力,希望您也是!
微信搜索公众号:就用python
|