接口自动化:第一个测试用例

图片

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

接口自动化会牵扯到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': [
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值