pytest "ERROR: file or directory not found: tests/" in CI but works locally - path issue
Fixes the pytest file or directory not found error for tests/ that appears only in CI. Use when pytest collects fine locally but errors in CI with a path error, or when the CI job's working directory is uncertain. Not for missing test files locally, and not for import errors inside collected tests.
Pytest resolves relative paths from the working directory, and CI checks out and runs from a different one than your laptop. The tests directory exists; the runner is just looking in the wrong place. Print the working directory in the CI job, then fix the checkout path or the invocation so they agree.
pytest "ERROR: file or directory not found: tests/" in CI but works locally - path issueSteps
Add a step to the CI job that prints the working directory and lists its contents before pytest runs. Compare that against where tests/ actually lives in the checkout.
Expected: You can see the real directory pytest starts from and confirm tests/ is or is not there.
Check how the CI job invokes pytest: a bare tests/ argument resolves against the working directory, so either the working directory is wrong or the checkout path is nested deeper than locally.
Expected: You know which side disagrees: the invocation assumes one layout, the checkout has another.
Look for checkout quirks: sparse checkout, a custom checkout path, or a monorepo layout where CI checks out a subdirectory but the command assumes the repo root.
Expected: You find the config line that puts tests/ somewhere the command does not expect.
Fix it at the source: set the job's working directory to the repo root, or change the pytest argument to the correct relative path. Prefer fixing the working directory so every later step agrees too.
Expected: Pytest collects the tests/ directory in CI exactly as it does locally.
Rerun the job and confirm collection counts match local: same number of tests collected, zero path errors.
Expected: CI reports the same collected test count as your laptop.
Use this when
- pytest cannot find tests/ in CI but finds it locally
- collection works on a laptop and errors in the pipeline
- you inherited a CI config and do not know its working directory
Not for this skill when
- tests/ is genuinely missing locally too (the path is wrong everywhere)
- pytest collects but tests fail on import (that is a module path issue)
- the error names a single test file, not the whole directory
Variant phrasings
- pytest file or directory not found tests in CI only
- ERROR file or directory not found tests works locally
- pytest path issue CI working directory checkout path
Why it happens
Pytest does not search for your test directory; it resolves the path you give it against the process working directory. Locally you run it from the repo root out of habit, so tests/ resolves. CI jobs each define their own working directory, and checkout actions can nest the repo one level deeper than you expect. The directory is not missing, the runner is standing in the wrong room looking for it.
Edge cases
- Windows runners use different path separators; if the job matrix includes Windows, quote the path and avoid shell-specific tricks.
- Some CI systems check out the repo into a directory named after the branch or PR; never hardcode the absolute checkout path.
- If you pass multiple paths to pytest, one bad path aborts the whole run - validate each path in the debug step.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_EYEoWriKoxiSLO7HDDyD8A