[pytest-dev] question about pytest_addoption

Bruno Oliveira nicoddemus at gmail.com
Tue Mar 1 08:20:44 EST 2016


Hi Bryan,

On Sun, Feb 28, 2016 at 4:21 PM Bryan Berry <bryan.berry at gmail.com> wrote:

> A specific fixture library, say foo_fixture that handles the installation
> of some particularly hairy proprietary software, may want to add additional
> custom options. Perhaps, we need to pass this fixture a special flag
> to enable ipv6 support.
>
> Here is how I have currently attempted to support this feature and it
> isn't working
> at all. Could anyone point me towards a working solution?
>

I didn't study your solution thoroughly, but I would suggest to use the
usual pytest hooks instead of trying to come up with your own solution for
fixtures to add command line options. Here's how I would do it:

# foo_fixture.py
def pytest_addoption(parser):
    parser.addoption('--ipv6', action='store_true', default=False)

@pytest.fixture
def foo_fixture(request):
    ipv6 = request.config.getoption('--ipv6')
    # lots of complicated stuff

# conftest.py
pytest_plugins = ['foo_fixture']

By using the `pytest_plugins` variable, pytest will load `foo_fixture` as a
plugin and automatically call the appropriate hooks and make any fixtures
declared in that module available for tests to use.

HTH,
Bruno.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20160301/8e76c1ce/attachment.html>


More information about the pytest-dev mailing list