python接口自动化-skip和pytest-ordering(3)

skip:
@pytest.mark.skip(reason="bug未修复") 跳过标记的测试用例,()中填写跳过的原因,当然可以省略
什么时候跳过? 如果该测试用例不需要执行,可以跳过。还有该测试用例有bug,并且未修复,我们也可以选择跳过。

pytest额外插件1:
pytest-ordering: 可以对需要运行的测试用例的顺序进行排序
1、安装相关库:
pip install pytest-ordering
  安装后 order排序才能起作用

2、在需要排序的测试用例上边添加装饰器:@pytest.mark.run(order=2)
3、order =1 =2 =3 代表运行顺序,1先执行 2再执行... 注意 order也可以为负值,比如 -1 代表 倒数第一执行。

实战代码

# 使用requests发送 post请求
import requests, pytest


class TestLogin:
    # 登录成功
    @pytest.mark.run(order=-1)
    def test_login01(self):
        response = requests.post(url="http://127.0.0.1:5000/user_login",
                                 data={"username": "xiaohua", "password": "a123456"})
        result = response.json()  # 把响应结果转为字典格式
        assert result == {'code': '1000', 'msg': '登录成功'}  # 断言 result(实际的响应结果)  == 预期结果

    # 用户名错误
    @pytest.mark.skip(reason="bug未修复")
    def test_login02(self):
        response = requests.post(url="http://127.0.0.1:5000/user_login",
                                 data={"username": "Test004ERR", "password": "a123456"})
        result = response.json()  # 把响应结果转为字典格式
        assert "用户名或密码错误" in str(result)  # 断言 响应结果中包含

    # 用户名为空
    @pytest.mark.run(order=1)
    def test_login03(self):
        response = requests.post(url="http://127.0.0.1:5000/user_login",
                                 data={"username": "", "password": "a123456"})
        result = response.json()  # 把响应结果转为字典格式
        assert result["msg"] == "用户名不能为空!" and result["code"] == "1003"  # 断言响应结果中的value值是否和预期结果一致

    # 密码错误
    @pytest.mark.run(order=3)
    def test_login04(self):
        response = requests.post(url="http://127.0.0.1:5000/user_login",
                                 data={"username": "Test004", "password": "a123456ERR"})
        result = response.json()  # 把响应结果转为字典格式
        assert result == {'code': '1005', 'msg': '用户名或密码错误!'}

    # 密码为空
    @pytest.mark.run(order=2)
    def test_login05(self):
        response = requests.post(url="http://127.0.0.1:5000/user_login",
                                 data={"username": "Test004", "password": ""})
        result = response.json()  # 把响应结果转为字典格式
        assert "密码不能为空!" in str(result)


if __name__ == "__main__":
    # 知识点1:运行当前模块的下的内容
    pytest.main(['-vs','./test_pytest03.py'])  # 运行标记test03

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值