【问题标题】:Accessing pytest options set in pytest.ini in marking conditions在标记条件下访问 pytest.ini 中设置的 pytest 选项
【发布时间】:2020-05-30 09:45:51
【问题描述】:

我正在为 webapp 编写测试,有时需要一段时间才能在生产服务器上看到更改。我不希望这些测试失败,但我也不想忽略它们。因此,我想在针对生产服务器运行时将此类测试标记为 xfail,但我无法访问标记中可以在测试用例内部访问的选项对象。一旦这些测试通过 XPASS,标记就会被删除。这些测试也不应该是

有没有可能这样做?

@pytest.mark.xfail(
    options.getini("base_url") == options.getini("production_server"), 
    reason="Change not yet deployed to the production server")
def test_apidoc(self, request, base_url):

我能看到的唯一解决方法是:

def test_apidoc(self, request, base_url):
    not_production_ready(request)
    if request.config.getoption("base_url") == request.config.getini("production_server"):
        pytest.xfail("Changes not yet deployed to the production")
    pass

但这会立即忽略测试。

【问题讨论】:

    标签: python selenium-webdriver pytest


    【解决方案1】:

    您可以动态附加标记 - 我会介绍另一个标记,例如provisional_xfail 仅用于一个目的:如果设置与所需设置匹配并且存在标记,则测试会添加 xfail 标记。示例:

    # conftest.py
    
    def pytest_collection_modifyitems(config, items):
        if config.getoption('prod') == True:
            for item in items:
                if item.get_closest_marker('provisional_xfail') is not None:
                    item.add_marker(pytest.mark.xfail(reason='Should fail for now with prod settings'))
    

    测试获得provisional_xfail 标记:

    import pytest
    
    
    @pytest.mark.provisional_xfail
    def test_spam():
        assert False
    

    【讨论】:

    • 感谢您的提示!我不得不稍微编辑您的 add_marker 函数,因为它不接受 reason 参数。所以,我改用了item.add_marker(pytest.mark.xfail(reason='Should fail for now with prod settings')),除此之外,它就像一个魅力!
    • @lukashino 真丢脸!你是对的,add_marker 不接受带有 args 的标记名称。更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2022-06-27
    • 2011-01-18
    • 2018-06-22
    相关资源
    最近更新 更多