【问题标题】:Is it possible to suppress the display of all parameterized arguments when a test fails in pytest当 pytest 中的测试失败时,是否可以禁止显示所有参数化参数
【发布时间】:2017-11-09 20:40:55
【问题描述】:

我目前正在使用 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

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

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

【问题讨论】:

    标签: pytest


    【解决方案1】:

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

    【讨论】:

    • 太棒了。刚刚试了一下,它完全符合我的要求。感谢您的帮助。
    猜你喜欢
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多