Pytest八(skip跳过用例)

skip:

跳过测试函数的最简单方法是使用跳过装饰器标记它,可以传递一个可选的原因

@pytest.mark.skip(reason="no way of currently testing this")

def test_the_unknown():

也可以通过调用来在测试执行或设置期间强制跳过pytest.skip(reason)功能:

def test_function():
    if not valid_config():
        pytest.skip("unsupported configuration")

也可以使用pytest.skip(reason,allow_module_level = True)跳过整个模块级别:

import pytest
if not pytest.config.getoption("--custom-flag"):
    pytest.skip("--custom-flag is missing, skipping tests", allow_module_level=Tru

二、skipif:有条件地跳过某些内容

 

 

 

 

 

 

1、模块之间共享skipif标记
本文件内调用

 

 


import pytest
if_n=pytest.mark.skipif(1!=2,reason="1不等于2跳过")
@if_n
def test_out():
print('输出正确')

def test_out2():
print('输出正确2')
if __name__ == "__main__":
pytest.main(["-s", "test_26.py"])
示例2:

 

 示例3:在另一个文件调用

 

 

 


三、skip类或模块

 

 

 

 

 

 


四、skip文件或目录:跳过整个文件或目录

例如,如果测试依赖于特定于Python的版本功能或包含您不希望pytest运行的代码。 在这种情况下,您必须排除文件和目录来自收藏。 有关更多信息,请参阅自定义测试集合

五、skip缺少导入依赖项

1、可以在模块级别或测试或测试设置功能中使用以下帮助程序
docutils = pytest.importorskip("docutils")
2、如果无法在此处导入docutils,则会导致测试跳过结果。 你也可以跳过库的版本号
docutils = pytest.importorskip("docutils", minversion="0.3")

六、概要

  1.无条件地跳过模块中的所有测试:

    pytestmark = pytest.mark.skip(“all tests still WIP”)

2.根据某些条件跳过模块中的所有测试

pytestmark = pytest.mark.skipif(sys.platform == “win32”, “tests for linux

˓→ only”
3.如果缺少某些导入,则跳过模块中的所有测试
pexpect = pytest.importorskip(“pexpect”)
posted @ 2021-02-03 19:12  水绿冰蓝  阅读(100)  评论(0)    收藏  举报