Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify more than one option in pytest config [pytest_addoption]

Tags:

python

pytest

Is it possible to add more than one option in the command line for pytest? I see that I can add the pytest_addoption hook to the conftest.py file, but I'm wondering how to go about adding more than one option(s).

like image 329
Andrea Avatar asked Jul 09 '13 20:07

Andrea


1 Answers

You can specify arbitrarily many command-line options using the pytest_addoption hook.

Per the pytest hook documentation:

Parameters: parser – To add command line options, call parser.addoption(...). To add ini-file values call parser.addini(...).

The pytest_addoption hook is passed a parser object. You can add as many command-line options as you want by calling parser.addoption(...) as many times as you want.

So an example of adding two parameters is as simple as:

def pytest_addoption(parser):
    parser.addoption('--foo', action='store_true', help='Do foo')
    parser.addoption('--bar', action='store_false', help='Do not do bar')

And like any other py.test hook this needs to go into a conftest.py file.

like image 109
Frank T Avatar answered Oct 18 '22 04:10

Frank T