pytest标记跳过某些测试用例不执行

无条件跳过(@pytets.mark.skip)

举例:

# file_name: test_skip.py


import pytest


class Test_B:

    def test_a(self):
        print('\n------------------> test_a has ran')
        assert 1

    @pytest.mark.skip(reason="由于某种原因这个测试用例暂时不执行")
    def test_b(self):
        print('------------------> test_b has ran')
        assert 0


if __name__ == '__main__':
    pytest.main(['-s', 'test_skip.py'])

使用装饰器@pytest.mark.skip来标记测试用例test_b,在执行过程中跳过test_b不执行。参数reason为可选参数,表示跳过的原因是什么。

有条件跳过(@pytest.mark.skipif)

根据特定的条件,不执行标识的测试函数.

 方法:
     skipif(condition, reason=None)

 参数:
     condition:跳过的条件,必传参数
     reason:标注原因,必传参数

 使用方法:
     @pytest.mark.skipif(condition, reason="xxx") 

举例:

# file_name: test_skip.py


import pytest


class Test_B:

    def test_a(self):
        print('\n------------------> test_a has ran')
        assert 1

    @pytest.mark.skipif(condition=2 > 1, reason='不想执行了')
    def test_c(self):
        print("-------------------> test_c has ran")
        assert 0


if __name__ == '__main__':
    pytest.main(['-s', 'test_skip.py'])

上面的例子中,但参数condition为True时就会跳过test_c,如果condition为False则仍然会执行test_c

 

上一篇:ol,ul,dl,table标签的基本语法


下一篇:modification of global variable “Promise.prototype.finally“ is not allowed when using plugins at app