Yauhen Mardan
Yauhen Mardan

Reputation: 427

pyproject.toml configuration to ignore a specific path?

Are there any ways to set a path to ignore in pyproject.toml like:

#pyproject.toml
[tool.pytest.ini_options]
ignore = ["path/to/test"]

Instead of using addopts:

#pyproject.toml
[tool.pytest.ini_options]
addopts = "--ignore=path/to/test"   

Upvotes: 24

Views: 11467

Answers (1)

Jongwook Choi
Jongwook Choi

Reputation: 8920

The list of configuration options you can add in the [tool.pytest.ini_options] (or pytest.ini) is documented in https://docs.pytest.org/en/7.1.x/reference/reference.html#configuration-options, which includes addopts, norecursedirs, etc. As of now, there is no such option like ignore for test discovery that can be listed in pytest.ini or pyproject.toml.

However, you have some alternatives:

Use testpaths

https://docs.pytest.org/en/7.1.x/reference/reference.html#confval-testpaths

It would be a good idea to maintain a whitelist (includelist) to run tests on, like the package or test suites. This sets a list of directories that should be searched for test discovery.

[tool.pytest.ini_options]
testpaths = ["your_package", "testing"]

Use collect_ignore in testconf.py

https://docs.pytest.org/en/7.1.x/example/pythoncollection.html#customizing-test-collection https://docs.pytest.org/en/7.1.x/reference/reference.html#global-variables

You can add a global variable collect_ignore or connect_ignore_glob in conftest.py to customize which files/directories should be excluded while collecting tests. This is more powerful as it allows dynamic, runtime value as opposed to in pyproject.toml.

# conftest.py
collect_ignore = ["path/to/test/excluded"]
collect_ignore_glob = ["*_ignore.py"]

Remarks

As a side note, norecursedirs may be used for excluding some submodules or subdirectories but this would not be a good idea, because this should contain patterns to ignore some build artifacts or ignored directories, e.g., _build, .venv, dist, etc.

Upvotes: 14

Related Questions