【问题标题】:xfail pytest tests from the command line来自命令行的 xfail pytest 测试
【发布时间】:2021-10-16 06:29:00
【问题描述】:

我有一个测试套件,我需要将一些测试标记为 xfail,但我无法编辑测试函数本身来添加标记。是否可以使用 pytest 从命令行指定某些测试应该是 xfail ?或者除此之外,至少通过向 pytest.ini 或 conftest.py 添加一些内容?

【问题讨论】:

    标签: python pytest


    【解决方案1】:

    我不知道执行此操作的命令行选项,但如果您可以过滤掉相应的测试,您可以实现 pytest_collection_modifyitems 并在这些测试中添加一个 xfail 标记:

    conftest.py

    
    names_to_be_xfailed = ("test_1", "test_3")
    
    def pytest_collection_modifyitems(config, items):
        for item in items:
            if item.name in names_to_be_xfailed:
                item.add_marker("xfail")
    

    或者,如果名称不是唯一的,您也可以按 item.nodeid 过滤。

    【讨论】:

    • 这看起来可行。现在有一个后续问题:有没有办法从命令行设置一个 conftest.py 文件?
    • 我不这么认为,conftest.py 必须在它所应用的测试的根路径中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2012-07-18
    • 1970-01-01
    • 2021-11-30
    • 2015-04-04
    相关资源
    最近更新 更多