简体   繁体   English

如何从命令行激活 PyTest 夹具?

[英]How do I activate a PyTest fixture from the command line?

I'm using Python3.8 and pytest.我正在使用 Python3.8 和 pytest。 How can I activate a fixture for my session based on some command line specification?如何根据一些命令行规范为我的 session 激活夹具? Right now I have a tests/conftest.py file that includes现在我有一个 tests/conftest.py 文件,其中包括

@pytest.fixture(name="fixture1", scope="session")
def fixture1():
    ...

@pytest.fixture(name="fixture2", scope="session")
def fixture2():
    ...

But I only want certain fixtures included in my session and I want to be able to specify that in either a pytest.ini file or via command line arguments I pass to pytest. But I only want certain fixtures included in my session and I want to be able to specify that in either a pytest.ini file or via command line arguments I pass to pytest. How do I do that?我怎么做?

Edit: This is my use case...编辑:这是我的用例......

If the command line option is enabled, I'd like to depend on one fixture如果启用了命令行选项,我想依赖一个夹具

@pytest.fixture(name="az_sql_db_inst")
def az_db_connection_fixture(az_sql_creds, wait_for_sql_server)

but if the command line isn't enabled, I'd like to depend on another fixture...但如果没有启用命令行,我想依赖另一个夹具......

@pytest.fixture(name="az_sql_db_inst")
def az_db_connection_fixture(az_sql_creds, wait_for_docker_sql_server)

You can add command line flags that can be used to enable your fixtures:您可以添加可用于启用固定装置的命令行标志:

conftest.py conftest.py

def pytest_addoption(parser):
    parser.addoption('--option2', action='store_const', const=True)
    parser.addoption('--option2', action='store_const', const=True)

Than you have to check for these arguments in your fixture:比你必须在你的夹具中检查这些 arguments :

@pytest.fixture(name="fixture1", scope="session", autouse=True)
def fixture1(request):
   # only use with option1 command line argument
   if request.config.getoption("--option1"):
       ...
    
@pytest.fixture(name="fixture2", scope="session", autouse=True)
def fixture2(request):
   # only use with option2 command line argument
   if request.config.getoption("--option2"):
       ...

@pytest.fixture(name="fixture3", scope="session", autouse=True)
def fixture3(request):
   # only use if option1 command line argument is not provided
   if not request.config.getoption("--option1"):
       ...

I used autouse=True here as I expect that the fixtures execute different setup code, your usage may vary of course.我在这里使用autouse=True ,因为我希望灯具执行不同的设置代码,当然您的使用可能会有所不同。

You are now able to call:您现在可以调用:

  • pytest -> no fixture will be applied pytest -> 不应用任何夹具
  • pytest --option1 -> fixture1 will be applied pytest --option1 -> fixture1 将被应用
  • pytest --option1 --option2 -> both fixtures will be applied pytest --option1 --option2 -> 两个夹具都将被应用

You can also add these arguments to your pytest.ini :您还可以将这些 arguments 添加到您的pytest.ini

[pytest]
# always apply fixture2
addopts = --option2

EDIT:编辑:
As for the followup question about the inherited fixture, you can do something like this:至于关于继承夹具的后续问题,您可以执行以下操作:

@pytest.fixture
def wait_for_sql_server(request):
   if request.config.getoption("--my_option"):
       ...

@pytest.fixture
def wait_for_docker(request):
   if not request.config.getoption("--my_option"):
       ...

@pytest.fixture(name="az_sql_db_inst")
def az_db_connection_fixture(
    az_sql_creds, wait_for_sql_server, wait_for_docker):
    ...

EDIT2:编辑2:
If you are not able to write or adapt the base fixtures ( wait_for_ in the EDIT part of the question) yourself, you can go a slighly other way.如果您无法自己编写或调整基本装置(问题的编辑部分中的wait_for_ ),您可以采用 go 稍微其他方式。

You can write separate implementations of your base fixture in separate plugins, and load the needed plugin based on the configuration:您可以在单独的插件中编写基本夹具的单独实现,并根据配置加载所需的插件:

plugin_docker.py plugin_docker.py

@pytest.fixture
def wait_for_service(wait_for_docker):
    yield

plugin_server.py插件服务器.py

@pytest.fixture
def wait_for_service(wait_for_sql_server):
    yield

conftest.py conftest.py

def pytest_addoption(parser):
    parser.addoption('--docker', action='store_const', const=True)

def pytest_configure(config):    
    use_docker = config.getoption("--docker")
    plugin_name = 'plugin_docker' if use_docker else 'plugin_server'
    if not config.pluginmanager.has_plugin(plugin_name):
        config.pluginmanager.import_plugin(plugin_name)

@pytest.fixture(name="az_sql_db_inst")
def az_db_connection_fixture(az_sql_creds, wait_for_service):
    ...    

The wait_for_service fixtures are just a wrapper around the actual fixtures, but this way you can derive from the same fixture in both scenarios. wait_for_service固定装置只是实际固定装置的包装器,但这样您可以在两种情况下从同一个固定装置派生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM