Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Add extra command line parameters to Pytest - conftest - getoption

In the conftest.py file we can use the pytest_addoption function to defined new command line options.

  • --demo expects a value.
  • --noisy is a boolean flag. By default it is false.

In the tests we need to use the request fixture and the getoption method to get the value of each option

def pytest_addoption(parser):
    parser.addoption("--demo")
    parser.addoption("--noisy", action='store_true')
def test_me(request):
    print(request.config.getoption("--demo"))
    print(request.config.getoption("--noisy"))

pytest -s
None
False
pytest -s --demo Hello --noisy
Hello
True