Novice学Pytest(5)-测试用例执行状态
一、前言
用例脚本执行完成后,每条用例都有自己的状态,常见的状态有:
- passed:测试通过
- failed:断言失败
- error:用例质量不行,代码报错(eg:fixture不存在,fixture里面有报错)
- xfail:预期失败,加了@pytest.mark.xfail()
二、error举例
1.参数不存在
import pytest @pytest.fixture() def user(): print("获取yy用户名") user = "liyebin01" assert user == "liyebin" def test_user(user): assert user == "liyebin01"
说明:user参数不存在,所以用例执行error
2.fixture有错
import pytest @pytest.fixture() def user(): print("获取yy用户名") user = "liyebin01" assert user == "liyebin" return user def test_user(user): assert user == "liyebin01"
说明:fixture里面断言失败,所以fixture会报错;因为test_user调用了错误的fixture,所以error表示用例有问题
三、failed举例
import pytest @pytest.fixture() def user(): print("===获取yy用户名===") user = "liyebin01" return user def test_user(user): assert user == "liyebin"
说明:fixture返回的变量断言失败
import pytest @pytest.fixture() def user(): print("===获取yy用户名===") user = "Isaymore" return user def test_user(user): raise NameError assert user == "Isaymore"
说明:用例执行期间抛出异常
summary:
- 测试用例的代码有异常,包括主动抛出异常或代码有异常,都算failed
- 当测试用例调用的fixture有异常,或传入的参数有异常的时候,都算error
- 如果一份测试报告中,error的测试用例数量越多,说明测试用例质量越差
四、xfail举例
import pytest # 断言装饰器 @pytest.mark.xfail(raises=ZeroDivisionError) def test_divide(): 1 / 0
说明:
- 代码有异常,且和raised的异常类匹配,所以是xfail(算测试通过的一种,表示符合期望捕捉到的异常),并不算failed
- 如果和raised的异常类不匹配,则是failed
参考链接:https://www.cnblogs.com/poloyy/p/12653187.html
一行代码一行诗