IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> 接口自动化:第一个测试用例 -> 正文阅读

[开发测试]接口自动化:第一个测试用例

图片

接下来。装逼开始....

接口自动化会牵扯到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

更多内容欢迎关注公众号
  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-08-26 12:24:47  更:2021-08-26 12:25:03 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/11 5:40:15-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码