分级运行:我们有的时候,在运行测试用例的过程中,想要运行部分测试用例,可以用 mark进行分级
工作中的使用场景:冒烟测试,分模块执行测试用例。
比如:现在自动化代码运行过程中,1000条测试用例,有200条报错,我们需要先跑主干流程,也就是核心测试用例。
import pytest, requests
class TestLogin:
# 1.正确流程
@pytest.mark.T1
def test_login01(self):
response = requests.post(url='http://127.0.0.1:5000/user_login',
data={"username": "xiaohua", "password": "a123456"})
res = response.json()
print(res)
assert {'code': '1000', 'msg': '登录成功'} == res
# 2.用户名错误
@pytest.mark.T2
def test_login02(self):
response = requests.post(url='http://127.0.0.1:5000/user_login',
data={"username": "xiaoniu1", "password": "a123456"})
res = response.json()
print(res)
assert "用户名或密码错误" in str(res)
# 3.用户名为空
@pytest.mark.T2
def test_login03(self):
response = requests.post(url='http://127.0.0.1:5000/user_login',
data={"username": "", "password": "a123456"})
res = response.json()
print(res)
assert res["msg"] == "用户名不能为空!" and res["code"] == "1003" # 断言响应结果中的value值是否和预期结果一致
# 4.密码错误
@pytest.mark.T2
def test_login04(self):
response = requests.post(url='http://127.0.0.1:5000/user_login',
data={"username": "xiaoniu", "password": "a1234567"})
res = response.json()
print(res)
assert {'code': '1005', 'msg': '用户名或密码错误!'} == res
# 5.密码为空
@pytest.mark.T1
def test_login05(self):
response = requests.post(url='http://127.0.0.1:5000/user_login',
data={"username": "xiaoniu", "password": ""})
res = response.json()
print(res)
assert {'code': '1004', 'msg': '密码不能为空!'} == res
if __name__ == "__main__":
# 知识点1: 运行该模块下的所有测试用例 -s -v -q
# pytest.main(['-vs','./test_pytest03_1.py']) # 运行指定模块
pytest.main(['-vs','-m T1', './test_pytest03_1.py']) # 运行指定模块