skip 和 skipif的用法

skip:无条件跳过

@pytest.mark.skip

 1 import pytest
 2  
 3 class Test_One():
 4     @pytest.mark.skip
 5     def test_01(self):
 6         print("===========> test_01")
 7  
 8     def test_02(self):
 9         print("===========> test_02")
10  
11 if __name__ == "__main__":
12     pytest.main(["-rs", "test_three.py::Test_one"])

可以看到执行结果第8行中,unconditional skip 表示无条件跳过

 1 F:\Python3.6.5\python.exe E:/xxx/test_three.py
 2 ============================= test session starts =============================
 3 platform win32 -- Python 3.6.5, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
 4 rootdir: E:\xxx, configfile: pytest.ini
 5 collected 2 items
 6 test_three.py s.                                                         [100%]
 7 =========================== short test summary info ===========================
 8 SKIPPED [1] test_three.py:9: unconditional skip
 9 ======================== 1 passed, 1 skipped in 0.51s =========================
10 Process finished with exit code 0

 

skipif:有条件跳过

@pytest.mark.skipif(condition=跳过的条件, reason=跳过的原因))

当条件为True时,则跳过

 1 import pytest
 2  
 3 class Test_One():
 4     @pytest.mark.skipif(condition=1<2, reason="参数condition为True,跳过测试")
 5     def test_03(self):
 6         print("===========> test_03")
 7  
 8     @pytest.mark.skipif(condition=1>2, reason="参数condition为False,不跳过测试")
 9     def test_04(self):
10         print("===========> test_04")
11  
12 if __name__ == "__main__":
13     pytest.main(["-rs", "test_three.py::Test_One"])

上面代码第 4 行,1<2 为True,跳过该条用例

下方执行结果第 8 行,展示了 reason 参数对应的值

上方代码第 13 行,1 > 2 为 False ,则不跳过该用例

 1 F:\Python3.6.5\python.exe E:/xxx/test_three.py
 2 ============================= test session starts =============================
 3 platform win32 -- Python 3.6.5, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
 4 rootdir: E:\xxx, configfile: pytest.ini
 5 collected 2 items
 6 test_three.py s.                                                         [100%]
 7 =========================== short test summary info ===========================
 8 SKIPPED [1] test_three.py:9: 参数condition为True,跳过测试
 9 ======================== 1 passed, 1 skipped in 1.13s =========================
10 Process finished with exit code 0

 

一些实用技巧 

  1. 多个skipif可以装饰同一个函数,有一个条件成立就可以跳过
  2. skip和skipif都可以作用在测试类上,则该类下的方法都将跳过
  3. 用例中调用pytest.skip(reason)方法也可跳过用例(下方代码中第 3 行)
     1 def test_skip2():
     2     print "1111111111"
     3     pytest.skip("该跳过用例了啦啦啦")
     4  
     5  
     6 执行结果:
     7  
     8 E:\PycharmProject\TestLibrary1\Pytttest>pytest -q test_skip.py
     9 ss                                                                       [100%]
    10 2 skipped in 0.07 seconds

     

  4. skip或者skipif可以赋值给一个变量,方便复用
     1 a=5
     2 skipif_condition = pytest.mark.skipif(a==10,reason="a=10")
     3 @skipif_condition
     4 def test_skip4():
     5     print 666666666
     6  
     7  
     8 ============================= test session starts =============================
     9 platform win32 -- Python 2.7.13, pytest-4.6.4, py-1.8.0, pluggy-0.12.0
    10 rootdir: E:\PycharmProject\TestLibrary1\Pytttest
    11 plugins: html-1.21.1, metadata-1.8.0, rerunfailures-7.0
    12 collected 1 item                                                               
    13  
    14 test_skip.py .                                                           [100%]
    15  
    16 ========================== 1 passed in 0.06 seconds ===========================

     

posted @ 2020-12-17 16:32  一只Newbie  阅读(566)  评论(0)    收藏  举报