Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest not recognizing added options

Tags:

python

pytest

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=***
like image 864
Hello lad Avatar asked Jul 20 '15 17:07

Hello lad


People also ask

Where is pytest config file?

Many pytest settings can be set in a configuration file, which by convention resides on the root of your repository or in your tests folder.

Where should pytest INI go?

Users can customize some pytest behavior using a configuration file called pytest. ini . This file is usually placed at the root of the repository and contains a number of configuration values that are applied to all test runs for that project.

How do I bypass pytest?

The simplest way to skip a test function is to mark it with the skip decorator which may be passed an optional reason : @pytest. mark.

What is a Conftest?

Conftest is a utility to help you write tests against structured configuration data. For instance, you could write tests for your Kubernetes configurations, Tekton pipeline definitions, Terraform code, Serverless configs or any other structured data.


1 Answers

That's a limitation of pytest itself. Take a look at the 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.

like image 169
Bruno Oliveira Avatar answered Oct 16 '22 21:10

Bruno Oliveira