当 pytest 测试失败时是否可以抑制所有参数化参数的显示

Che*_*ynx 2 pytest

我目前正在使用 python 3.5.1 和 3.6 以及最新版本的 pytest。当使用参数化测试运行 pytest 时,我希望任何失败的测试仅显示失败的测试,而不是参数化测试的所有设置。

解释...

我使用 @pytest.mark.parametrize 装饰器编写了许多测试,以允许我使用许多不同的输入运行测试。

我还为参数化参数传递了一个 id 列表,如下例所示:

@pytest.mark.parametrize('input_name12, output_name12',
    [
        ('chloroform', None),
        ('chloroform-d', (['deuterated'], '-d'))]
    ],
    ids=[
        "unlabelled chloroform",
        "chloroform deuterio-labelled with -d"
    ]
def test_isotope_extract(input_name12, output_name12):
    assert isotope_extract(input_name12) == output_name12
Run Code Online (Sandbox Code Playgroud)

在某些情况下,我想在测试中运行 10 多组参数。这通常工作正常。但是,当我运行测试时,如果一个或多个参数化测试失败,则每次失败时,所有参数化测试的整个块都会输出到终端。即,上述代码中的一项测试失败将导致该块中的所有代码以及断言失败的具体细节显示在终端中。

有没有什么方法可以抑制整个参数化测试的显示,这样当我失败时我只能看到失败的断言和关联的 Id?

The*_*ler 5

这是因为 pytest 打印了失败函数的代码。您可以通过使用来防止这种情况--tb=short