- 使用pytest.mark.skip()或者pytest.mark.skipif()来装饰测试用例方法或者测试用例类
- pytest.mark.skip():无条件跳过,括号中可以写reason参数,表示跳过原因,也可以不写。
- pytest.mark.skipif():满足某些条件时跳过,括号中写跳过条件和跳过原因,一般情况下,在某些条件下跳过执行用例才使用。
- 无论是@pytest.mark.skip()标签还是@pytest.mark.skipif()标签,如果你想在多个测试方法上装饰,依次写起来很麻烦的话,此时可以定义一个变量让它等于标签,然后在装饰的时候用该变量代替标签。也可以写在某个公共方法中,通过导入来使用。
- 方法内跳过,在方法内容通过一些条件来判断是否跳过执行用例,可以结合if条件使用,当满足某些条件的时候跳过。
先看代码test_01.py:
import pytest from webTest.Common.logger import logger my_skip = pytest.mark.skipif(1 == 1, reason='自定义的跳过标签') class Test01: def test_1(self): try: assert 1 == 1 except AssertionError as e: logger.error(e) raise e else: logger.info("测试通过") finally: print("测试执行完成") @pytest.mark.skip(reason="使用skip跳过") def test_2(self): assert 1 == 1 @pytest.mark.skipif(1 == 1, reason="使用skipif跳过") def test_3(self): assert 1 == 1 @my_skip def test_4(self): assert 1 == 1 def test_5(self): if(1 == 1): pytest.skip(reason='方法内跳过') else: assert 1 == 0
执行用例的main.py函数如下:
if __name__ == '__main__': pytest.main(['-rs','test_01.py'])
执行结果如下: