Pytest-灯具依赖关系的动态解析

问题描述 投票:0回答:1

我无法找到一种方法来改变灯具的依赖关系,而不是像下面这样。问题是我需要根据pytest.config.getoption参数确定依赖项,而不是这里使用的依赖项(变量在模块级别解析)。

我需要获得两种测试模式:快速和完整,并保持相同的测试源代码。

pytest_generate_tests似乎没用,或者至少我不知道如何在这里使用它。

import pytest


DO_FULL_SETUP = "some condition that I need take from request.config.getoption(), not like this"

if DO_FULL_SETUP:
    # such a distinction is valid from interpreter's (and pytest's) point of view

    @pytest.fixture(scope="session")
    def needed_environment(a_lot_of, expensive, fixtures_needed):
        """This does expensive setup that I need 
        to avoid in "fast" mode. Takes about a minute (docker pull, etc..)"""

else:
    @pytest.fixture
    def needed_environment():
        """This does a fast setup, has "function scope" 
        and doesn't require any additional fixtures. Takes ~20ms"""


def test_that_things(needed_environment):
    """At this moment I don't want to distinguish what 
     needed_environment is. Tests have to pass in both modes."""

我无法找到一种方法来改变灯具的依赖关系,而不是像下面这样。问题是我需要根据pytest.config.getoption参数确定依赖项,...

python dynamic runtime pytest fixtures
1个回答
0
投票

这可以使用request.getfixturevalue('that_fixture_name')完成。灯具可以在运行时调用。在这种情况下,甚至没有灯具的范围违规('session''function')。

© www.soinside.com 2019 - 2024. All rights reserved.