SupercovBlog

Guide

pytest ignore directory: --ignore, norecursedirs and collect_ignore

Every way to skip a folder of tests, collected with pytest 9.2.0, and the three that quietly ignore nothing.

Ignore the path, not the name. A card: pytest --ignore=integration ignores nothing; pytest --ignore=tests/integration skips 2 tests; collect_ignore in tests/conftest.py works.

To skip a directory, pass its path from where you run pytest: pytest --ignore=tests/integration. To skip it every time, put collect_ignore = ["integration"] in that folder’s parent conftest.py, or set norecursedirs, which replaces pytest’s defaults. A bare --ignore=integration matches nothing.

Ways to ignore tests at a glance

To skipUse
One directory, this runpytest --ignore=tests/integration
Files by patternpytest --ignore-glob='*slow*'
A directory, every runcollect_ignore = ["integration"] in tests/conftest.py
Directories by name, every runnorecursedirs = ["integration", ".*", "build", "venv"]
Everything outside tests/testpaths = ["tests"]
One testpytest --deselect tests/unit/test_pricing.py::test_rejects_zero
Tests by namepytest -k 'not slow'

We collected every setup with pytest 9.2.0 and Python 3.13 on a project with tests/unit (3 tests), tests/integration (2), a legacy/tests folder (1) and a stale copy of a test in build/lib/tests (1). With no options, pytest collected 6: everything except build/.

A grid of seven pytest setups against four folders, unit, integration, legacy and build. A plain run collects unit, integration and legacy: 6 tests. --ignore=tests/integration: 4. --ignore=integration: still 6. norecursedirs integration also collects build: 5. testpaths tests: 5. collect_ignore integration: 3. Naming tests/integration with --ignore: 2.
Which folders pytest 9.2.0 collected with each setup.

–ignore and –ignore-glob

pytest --ignore=tests/integration --ignore=legacy

Each --ignore takes a path relative to where you run pytest; repeat it for more. This collected the 3 unit tests. A trailing slash made no difference, but --ignore=integration ignored nothing: all 6 tests ran. For a pattern, --ignore-glob matches file paths, so --ignore-glob='*/integration/*' and --ignore-glob='*slow*' both worked; quote the pattern so the shell does not expand it.

To keep the option in config, add it to addopts:

[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "--ignore=tests/integration"

Ignore a directory in pyproject.toml

norecursedirs takes directory names or patterns:

[tool.pytest.ini_options]
norecursedirs = ["integration", "legacy"]

This skipped both directories, and collected the stale build/lib/tests copy that the plain run left out. Setting norecursedirs replaces the defaults, which include build, dist, venv, node_modules and .*. Copy those back into the list:

norecursedirs = ["integration", "legacy", ".*", "build", "dist", "venv", "node_modules"]

testpaths = ["tests"] limits where a plain pytest looks, which left out legacy/ and build/ at once: 5 tests.

A diagram of pytest's order: paths you name are always collected, then testpaths, then norecursedirs, which replaces the defaults build, venv and dot folders, then --ignore and collect_ignore. Among the tests collected, -k deselects by name, -m by marker, and --deselect one test.
How pytest picks the tests to run, in the order it applies each option.

collect_ignore in conftest.py

# tests/conftest.py
collect_ignore = ["integration"]
collect_ignore_glob = ["*slow*"]

Paths are relative to the conftest.py, so integration here means tests/integration. With collect_ignore the run collected 3 tests; with collect_ignore_glob alone, 4. Being Python, the list can depend on the platform or the Python version.

Why your pytest ignore isn’t working

What we triedWhat happened
--ignore=integration run from the project rootNothing ignored
norecursedirs = ["integration"]Integration skipped, build/ collected
pytest tests/integration with --ignore=tests/integrationBoth integration tests ran
pytest tests/integration with norecursedirsBoth ran: a path you name is collected
-m 'not integration' without a marker on the testsNothing deselected

A path given on the command line wins over every ignore. To exclude tests by level with -m, mark them first, with pytestmark = pytest.mark.integration in the module.

Ignored tests and coverage

Skipping a directory also drops the code only those tests run. With --cov=shop, the full run covered 8 of 8 statements; with --ignore=tests/integration, 5 of 8, since ship() is tested only there. To leave source files out of the report instead, see pytest coverage exclude.

What Supercov adds

npx supercov -- pytest --ignore=tests/integration
npx supercov runs latest gaps

Supercov runs the same command, ignore options included, and records which test covered each line, with branches and MC/DC. Run the integration directory separately and npx supercov merge combines the two runs; see Merge coverage reports. For the tools side by side, see Python code coverage.

More coverage guides