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