接下来。装逼开始....
接口自动化会牵扯到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': [