跳过用例skip

1、装饰器,放在函数前面,跳过用例 @pytest.mark.skip(reason="no way of currently testing this")

import pytest

def test1():
    print('操作1')
    print("-----------------------------------------------")

@pytest.mark.skip(reason="no way of currently testing this")
def test12():
    print('操作2')
    print("-----------------------------------------------")


def test3():
    print('操作3')

    print("-----------------------------------------------")


if __name__ == '__main__':
    pytest.main(['-s', "text.fix2.py"]

2、放在函数里面,只控制某条用例

import pytest


def test_123():
    pytest.skip("Not implemented")
    assert 1 == 0


def test_234():
    assert 1 == 1

3、跳过某个模块

@pytest.importskip("模块名")

4、根据版本去控制跳过某个模块

@pytest.importskip("模块名",minversion="version_num")

5、如果您希望有条件地跳过某些内容,则可以使用skipif代替

import sys
@pytest.mark.skipif(sys.version_info < (3,6),
reason="requires python3.6 or higher")
def test_function():
    ...

如果条件在收集期间评估为True,则将跳过测试函数,具有指定的原因使用-rs时出现在摘要中。

您可以在模块之间共享skipif标记。参考以下案例

# content of test_mymodule.py
import mymodule
minversion = pytest.mark.skipif(mymodule.__versioninfo__ < (1,1),
reason="at least mymodule-1.1 required")
@minversion
def test_function():
    ...

您可以导入标记并在另一个测试模块中重复使用它:

# test_myothermodule.py
from test_mymodule import minversion
@minversion
def test_anotherfunction():
    ...

对于较大的测试套件,通常最好有一个文件来定义标记,然后一致适用于整个测试套件。

或者,您可以使用条件字符串而不是布尔值,但它们之间不能轻易共享它们支持它们主要是出于向后兼容的原因

6、skip类或模块

@pytest.mark.skipif(sys.platform == 'win32',
reason="does not run on windows")
class TestPosixCalls(object):
    def test_function(self):
        "will not be setup or run under 'win32' platform"

警告:强烈建议不要在使用继承的类上使用skipif。 pytest中的一个已知错误标记可能会导致超类中的意外行为。

猜你喜欢

转载自www.cnblogs.com/guo2733/p/10536060.html