1.mark标记

1.@pytest.mark.xxx

2.跳过测试

1.@pytest.mark.skip(reason=xxx)

说明 reason参数是非必填的,但是为了规范最好带上“reason参数”

  1. import pytest
  2. def test_hello0():
  3. print("test_hello0")
  4. assert 1==1
  5. @pytest.mark.skip(reason="【原因】skip的示例")
  6. def test_hello1():
  7. print("test_hello1:需要skip")
  8. assert 1==2

skip.png

2.@pytest.mark.skipif(reason=xxx)

skipif()的判定条件可以是任何的Python表达式:

判定结果 说明 备注
True 执行skip操作 True时,testcase被skip
False 不执行skip操作 False时,testcase不被skip—> 不能跳过执行

说明 reason参数是非必填的,但是为了规范最好带上“reason参数”

  1. a = 1 # 定义一个全局变量a,用于skipif的判断操作
  2. @pytest.mark.skipif(a != 1, reason="a不等于1,False则不执行(即不能跳过)")
  3. def test_hello2():
  4. print("test_hello2:False,skip跳过操作失效")
  5. assert 1 == 2
  6. @pytest.mark.skipif(a == 1, reason="a不等于1,True执行skip(即能跳过)")
  7. def test_hello3():
  8. print("test_hello3:需要skipif")
  9. assert 1 == 2

skipif.png

3.skip()与skipif()的区别

区别 说明
skip() 不具备if判断
skipif() 具备if判断

3.标记预期会失败的用例

1.@pytest.mark.xfail

  1. def test_hello1():
  2. print("test_hello1:不带任何装饰器")
  3. assert 1 == 1
  4. # @pytest.mark.xfail作用:标记预期会失败的测试用例【最终结果标记为 XFAIL】
  5. @pytest.mark.xfail(reason="【原因】预期失败的标记为XFAIL")
  6. def test_hello2():
  7. print("test_hello2:带任何装饰器xfail")
  8. assert 1 == 11

xfail.png

2.skip()/skipif()与xfail()的区别

区别 说明
skip()/skipif() 测试会直接跳过,而不会执行,最终结果标记为skipped
xfail() 测试会执行,最终结果标记为xfailed