【问题标题】:Pytest - skip (xfail) mixed with parametrizePytest - 跳过(xfail)与参数化混合
【发布时间】:2015-12-17 15:57:28
【问题描述】:

有没有一种方法可以使用 @incremental 插件,就像描述的 att Pytest: how to skip the rest of tests in the class if one has failed? 和 @pytest.mark.parametrize 一样,如下所示:

@pytest.mark.incremental
Class TestClass:
  @pytest.mark.parametrize("input", data)
  def test_preprocess_check(self,input):
    # prerequisite for test

  @pytest.mark.parametrize("input",data)
  def test_process_check(self,input):
    # test only if test_preprocess_check succeed

我遇到的问题是,在 test_preprocess_check 第一次失败时,我的数据集的给定输入,以下 test_preprocess_checktest_process_check标记为“xfail”。 我期望的行为是,在我的参数化数据集的每个新“输入”处,测试将以增量方式运行。

例如:数据 = [0,1,2]

如果只有 test_preprocess_check(0) 失败:

我收到以下报告: 1 次失败,5 次失败

但我希望报告: 1 次失败,1 次失败,4 次通过

谢谢

【问题讨论】:

    标签: pytest skip


    【解决方案1】:

    经过一些实验,我找到了一种将@incremental 泛化为与 parametrize 注释一起使用的方法。只需重写 _previousfailed 参数,使其对每个输入都是唯一的。参数 _genid 正是需要。

    我添加了一个@pytest.mark.incrementalparam 来实现这一点。

    代码变成:

    def pytest_runtest_setup(item):
    
        previousfailed_attr = getattr(item, "_genid",None)
        if previousfailed_attr is not None:
            previousfailed = getattr(item.parent, previousfailed_attr, None)
            if previousfailed is not None:
                pytest.xfail("previous test failed (%s)" %previousfailed.name)
    
        previousfailed = getattr(item.parent, "_previousfailed", None)
        if previousfailed is not None:
            pytest.xfail("previous test failed (%s)" %previousfailed.name)
    
    def pytest_runtest_makereport(item, call): 
    
        if "incrementalparam" in item.keywords: 
            if call.excinfo is not None:
                previousfailed_attr = item._genid
                setattr(item.parent,previousfailed_attr, item)
    
        if "incremental" in item.keywords:
            if call.excinfo is not None:
                parent = item.parent
                parent._previousfailed = item
    

    有趣的是,它不能在没有参数化的情况下使用,因为参数化注释会自动创建 _genid 变量。

    希望这可以帮助别人而不是我。

    【讨论】:

    • 注意 _genid 是由您的“输入”数据集组成的,因此可能导致变量名无效,将其映射到 100% 有效的变量名可以避免一些问题
    猜你喜欢
    • 2016-03-24
    • 2021-11-30
    • 2021-10-16
    • 2015-10-04
    • 2019-11-18
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多