@pytest.mark.skipif

介绍

pytest.mark.skipif可以根据条件判断是否执行该用例,
第一个参数传入条件 可以传入True/False如果条件应该被跳过或条件字符串
第二个参数是说明/原因 reason="2大于1"

有条件的跳过用例

import pytest

  @pytest.mark.skipif(1<2,reason="2大于1") # 条件满足跳过/否则执行
  def test_function(self):
      assert 1

可以在模块之间共享模块

# 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():
        ...
    

对于较大的测试套件,最好有一个文件来定义标记,然后在整个测试套件中始终如一地应用这些标记。
或者,您可以使用条件字符串代替布尔值,但它们不能在模块之间轻松共享,因此主要出于向后兼容性的原因支持它们。

官网文档

posted @ 2022-06-29 16:21  zhq9  阅读(235)  评论(0编辑  收藏  举报