Pytest - Advanced

Passing parameters in the command line

In your pytest:

test_module.py
@pytest.mark.unit
def test_print_name(name):
    print ("Displaying name: %s" % name)

In your configuration:

conftest.py
def pytest_addoption(parser):
    parser.addoption("--name", action="store", default="default name")


def pytest_generate_tests(metafunc):
    # This is called for every test. Only get/set command line arguments
    # if the argument is specified in the list of test "fixturenames".
    option_value = metafunc.config.option.name
    if 'name' in metafunc.fixturenames and option_value is not None:
        metafunc.parametrize("name", [option_value])

Then you can run from the command line with a command line argument:

pytest -s test_module.py --name abc
Option -s is a shortcut for --capture=no. It is not mandatory!
Share Comments
comments powered by Disqus