[pytest-dev] question about pytest_addoption

Bryan Berry bryan.berry at gmail.com
Sun Feb 28 14:21:29 EST 2016


dear pytest folks,

I am creating an automation framework based on pytest for my employer that i
hope to someday soon open-source. This framework is tentatively titled
'Ghost-elephant' after my child's imaginary friend. At a 5,000 foot view my
ghost-elephant manages virtual machines and a bag of configuration
variables needed
to execute the tests.

Ghost-elephant adds some custom command-line options but I also want to also
allow libraries, say added by separate fixture libraries, to add
command-line
flags. For example, Ghost-elephant adds a flag --skip-cleanup that my
custom software
fixtures use to determine if the fixture's cleanup step should be skipped.

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 really want to minimize the code that a user needs to add to their
contest.py to take advantage of this.

# foo_fixture.py

def pytest_addoption_foo(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

def pytest_addoptions():
    option_adders = []
    gs = globals()
    for g in gs.keys():
        if isinstance(gs[g], function) and
g.startswith('pytest_addoption_'):
            option_adders.append(gs[g])
    def pytest_addoption(parser):
        for o in option_adders:
            o.__call__(parser)

pytest_addoptions()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20160229/42c996a7/attachment.html>


More information about the pytest-dev mailing list