我无法找到一种方法来改变灯具的依赖关系,而不是像下面这样。问题是我需要根据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参数确定依赖项,...
这可以使用request.getfixturevalue('that_fixture_name')
完成。灯具可以在运行时调用。在这种情况下,甚至没有灯具的范围违规('session'
与'function'
)。