1.mark标记
1.@pytest.mark.xxx
2.跳过测试
1.@pytest.mark.skip(reason=xxx)
说明 reason参数是非必填的,但是为了规范最好带上“reason参数”
import pytestdef test_hello0():print("test_hello0")assert 1==1@pytest.mark.skip(reason="【原因】skip的示例")def test_hello1():print("test_hello1:需要skip")assert 1==2
2.@pytest.mark.skipif(reason=xxx)
skipif()的判定条件可以是任何的Python表达式:
| 判定结果 | 说明 | 备注 |
|---|---|---|
| True | 执行skip操作 | True时,testcase被skip |
| False | 不执行skip操作 | False时,testcase不被skip—> 不能跳过执行 |
说明 reason参数是非必填的,但是为了规范最好带上“reason参数”
a = 1 # 定义一个全局变量a,用于skipif的判断操作@pytest.mark.skipif(a != 1, reason="a不等于1,False则不执行(即不能跳过)")def test_hello2():print("test_hello2:False,skip跳过操作失效")assert 1 == 2@pytest.mark.skipif(a == 1, reason="a不等于1,True执行skip(即能跳过)")def test_hello3():print("test_hello3:需要skipif")assert 1 == 2
3.skip()与skipif()的区别
| 区别 | 说明 |
|---|---|
| skip() | 不具备if判断 |
| skipif() | 具备if判断 |
3.标记预期会失败的用例
1.@pytest.mark.xfail
def test_hello1():print("test_hello1:不带任何装饰器")assert 1 == 1# @pytest.mark.xfail作用:标记预期会失败的测试用例【最终结果标记为 XFAIL】@pytest.mark.xfail(reason="【原因】预期失败的标记为XFAIL")def test_hello2():print("test_hello2:带任何装饰器xfail")assert 1 == 11
2.skip()/skipif()与xfail()的区别
| 区别 | 说明 |
|---|---|
| skip()/skipif() | 测试会直接跳过,而不会执行,最终结果标记为skipped |
| xfail() | 测试会执行,最终结果标记为xfailed |
