J_yang
J_yang

Reputation: 2812

Can I set pytest command line arguments in conftest?

The pytest I would like to do will always involve short traceback (--tb=short) and report skip reason (-rsx)

So instead of needing to do pytest --tb=short -rsx everytime, can I specify it somewhere, maybe conftest.py?

Thank you

Upvotes: 4

Views: 2650

Answers (2)

Urthor
Urthor

Reputation: 95

In addition to Konstantin's answer.

Programmatically set command line arguments, and all other configuration values, inside (ideally) pytest.ini inside your tests (or root) folder, or a pyproject.toml.

Or an alternative as per https://docs.pytest.org/en/6.2.x/customize.html

The reason, which the documentation omits, is that Pytest supports plugins.

Plugins are initialized before conftest.py is evaluated and read the configurations once.

Hence, most configuration options shouldn't be manipulated within conftest.py.

Upvotes: 2

Konstantin
Konstantin

Reputation: 578

If you always want to involve short traceback and report skip reason you can write it into a configuration file pytest.ini

[pytest]
addopts = -rsx --tb=short

Alternatively, you can set a PYTEST_ADDOPTS environment variable to add command line options while the environment is in use:

export PYTEST_ADDOPTS="-rsx --tb=short"

Upvotes: 7

Related Questions