【问题标题】:How do I disable a test using pytest?如何使用 pytest 禁用测试?
【发布时间】:2016-11-21 10:13:03
【问题描述】:

假设我有一堆测试:

def test_func_one():
    ...

def test_func_two():
    ...

def test_func_three():
    ...

是否有装饰器或类似的东西可以添加到函数中以防止 pytest 仅运行该测试?结果可能类似于...

@pytest.disable()
def test_func_one():
    ...

def test_func_two():
    ...

def test_func_three():
    ...

【问题讨论】:

    标签: python testing pytest


    【解决方案1】:

    Pytest 有 skip 和 skipif 装饰器,类似于 Python unittest 模块(使用 skipskipIf),可以在文档 here 中找到。

    链接中的示例可以在这里找到:

    @pytest.mark.skip(reason="no way of currently testing this")
    def test_the_unknown():
        ...
    
    import sys
    @pytest.mark.skipif(sys.version_info < (3,3),
                        reason="requires python3.3")
    def test_function():
        ...
    

    第一个示例总是跳过测试,第二个示例允许您有条件地跳过测试(当测试依赖于平台、可执行版本或可选库时非常有用。

    例如,如果我想检查是否有人安装了库 pandas 进行测试。

    import sys
    try:
        import pandas as pd
    except ImportError:
        pass
    
    @pytest.mark.skipif('pandas' not in sys.modules,
                        reason="requires the Pandas library")
    def test_pandas_function():
        ...
    

    【讨论】:

    • 有没有类似 Mocha (Node.js) 的优雅 .skip 的东西? it('tests something'...) --> it.skip('tests something'...) 这将禁用该特定测试。它还有一个方便的反面:.only,它只会运行该测试而不会运行其他任何东西。
    • 我的意思是,对于only 部分,您可以通过命令行或用于初始化测试运行器的任何方式执行此操作:stackoverflow.com/a/36539692/4131059 至于itit.skip,我相信这里的功能完全涵盖了这一点。
    【解决方案2】:

    skip decorator 可以胜任:

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

    reason 参数是可选的,但最好指定跳过测试的原因)。

    还有skipif() 允许在满足某些特定条件时禁用测试。


    这些装饰器可以应用于方法、函数或类。

    skip all tests in a module,定义一个全局的pytestmark变量:

    # test_module.py
    pytestmark = pytest.mark.skipif(...)
    

    【讨论】:

      【解决方案3】:

      当您想跳过 pytest 中的测试时,可以使用 skipskipif 装饰器标记测试。

      跳过测试

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

      跳过测试的最简单方法是使用skip 装饰器对其进行标记,该装饰器可以传递可选的reason

      还可以通过调用pytest.skip(reason) 函数在测试执行或设置期间强制跳过。这在导入期间无法评估跳过条件时很有用。

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

      根据条件跳过测试

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

      如果您想根据条件跳过,则可以改用skipif。在前面的示例中,在 Python3.6 之前的解释器上运行时会跳过测试函数。

      最后,如果您想跳过某个测试,因为您确定它失败了,您还可以考虑使用 xfail 标记来表示您预计测试会失败。

      【讨论】:

        【解决方案4】:

        我不确定它是否已被弃用,但您也可以在测试中使用 pytest.skip 函数:

        def test_valid_counting_number():
             number = random.randint(1,5)
             if number == 5:
                 pytest.skip('Five is right out')
             assert number <= 3
        

        【讨论】:

        • 前来求助pytest,留作参考
        • 它引用了什么。我必须知道
        • 恐怕这早于我的时代了。但是,我很高兴我现在知道了。
        【解决方案5】:

        如果你想跳过测试而不是硬编码标记,最好使用关键字表达式来转义它。

        pytest test/test_script.py -k 'not test_func_one'
        

        注意:这里的'keyword expression'基本上是使用pytest(或python)提供的关键字来表达一些东西并完成一些事情。我上面的例子,'not'是一个关键字。

        有关更多信息,请参阅此link

        更多关键字表达示例:参考this答案

        【讨论】:

        • 什么是“关键字表达式”?你的意思是“关键字抑制”吗?无论如何,您可以添加对它的引用吗?但是没有“编辑:”、“更新:”或类似的 - 答案应该看起来好像是今天写的。
        • @PeterMortensen 我加了一点。请告诉我这是否有帮助。
        【解决方案6】:

        即使您怀疑测试会失败,您也可能想要运行测试。对于这种情况https://docs.pytest.org/en/latest/skipping.html 建议使用装饰器 @pytest.mark.xfail

        @pytest.mark.xfail
        def test_function():
            ...
        

        在这种情况下,Pytest 仍会运行您的测试并让您知道它是否通过,但不会抱怨和中断构建。

        【讨论】:

          【解决方案7】:

          您可以通过自定义 pytest 标记对一组测试用例进行测试,然后只执行您想要的那些测试用例。或者反过来,运行除另一组之外的所有测试:

          @pytest.mark.my_unit_test
          def test_that_unit():
          ...
          
          @pytest.mark.my_functional_test
          def test_that_function():
          ...
          

          然后,只运行一组单元测试,例如: pytest -m my_unit_test

          反之,如果你想运行所有测试,除了一组: pytest -m "not my_unit_test"

          How to combine several marks

          More examples in official documentation

          如果你对测试用例有良好的逻辑分离,看起来会更方便。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-06-24
            • 1970-01-01
            • 1970-01-01
            • 2022-10-19
            • 2021-12-29
            • 2020-02-02
            • 2023-04-04
            相关资源
            最近更新 更多