pytest "ERROR: file or directory not found" / rootdir Problems
pytest computes a rootdir from where it runs and the config files it finds. When CI runs from an unexpected directory, the rootdir is wrong - config and conftest.py aren’t loaded, and paths don’t resolve.
What this error means
pytest reports ERROR: file or directory not found, or behaves as if your config doesn’t exist (markers unknown, plugins off, fixtures missing). The printed rootdir: line is not where you expect.
ERROR: file or directory not found: tests
rootdir: /home/runner/work/repo
configfile: (none found)Common causes
Running from the wrong working directory
CI invokes pytest from a parent or subdirectory where the test path doesn’t exist, so the path argument is "not found" and rootdir is computed elsewhere.
No config file at the expected level
pytest derives rootdir from the nearest pyproject.toml/pytest.ini/tox.ini/setup.cfg. If none is where you think, config-driven behavior silently turns off.
How to fix it
Run from the repo root with explicit config
cd "$GITHUB_WORKSPACE"
pytest -c pyproject.toml tests/Pin rootdir and testpaths in config
Make the layout explicit so rootdir is stable regardless of where CI starts.
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "-ra"Verify what pytest detected
- Read the
rootdir:andconfigfile:lines pytest prints at startup. - If
configfile: (none found), your config isn’t where pytest looked. - Pass
-c <config>and an explicit test path to remove ambiguity.
How to prevent it
- Run pytest from a consistent working directory in CI.
- Keep a single config file at the repo root with
testpathsset. - Check the startup
rootdir:/configfile:lines when behavior seems off.