Hello lad
Hello lad

Reputation: 18790

Pytest not recognizing added options

I have a project directory looks like following

Projects/
....this_project/
........this_project/
............__init__.py
............code.py
............tests/
................conftest.py
................test_1.py
................test_2.py

and I added a command line option (--PALLADIUM_CONFIG) by putting following code into conftest.py

def pytest_addoption(parser):
    parser.addoption("--PALLADIUM_CONFIG", action="store")

@pytest.fixture
def PALLADIUM_CONFIG(request):
    return request.config.getoption("--PALLADIUM_CONFIG")

And what strange is:

if I cd into

Projects/this_project/this_project

or

Projects/this_project/this_project/tests

and run

py.test --PALLADIUM_CONFIG=***

if runs well

but if I locate myself in for example

Projects/this_project

or

Projects

then pytest gives me error

py.test: error: unrecognized arguments: --PALLADIUM_CONFIG=***

Upvotes: 30

Views: 54039

Answers (1)

Bruno Oliveira
Bruno Oliveira

Reputation: 15255

That's a limitation of pytest itself. Take a look at the Writing Plugins docs:

Note that pytest does not find conftest.py files in deeper nested sub directories at tool startup. It is usually a good idea to keep your conftest.py file in the top level test or project root directory.

One solution is to create an external plugin, or move the option to a conftest file nearer the root.

Upvotes: 29

Related Questions