[pytest-dev] question about pytest_addoption

Bryan Berry bryan.berry at gmail.com
Tue Mar 1 08:30:46 EST 2016


Hi Bruno, I already successfully use that approach from one fixtures
package. However, I want to add command-line options from one or more
fixtures packages. Say i use ProductAFixture library that adds a
command-line option and ProductBFixture that also adds a command-line
option. How can i take advantage of both?

On Tue, Mar 1, 2016 at 8:20 AM, Bruno Oliveira <nicoddemus at gmail.com> wrote:

> 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/c8a19498/attachment.html>


More information about the pytest-dev mailing list