pytest E fixture 'mocker' not found - pytest-mock missing in CI requirements
Restores the missing mocker fixture by declaring pytest-mock in CI dependencies. Use when pytest fails in CI with fixture 'mocker' not found while passing locally. Covers confirming the plugin is absent, adding it to the test requirements, and verifying registration. Not for runtime failures of the mocker fixture itself.
TL;DR
The mocker fixture is not built into pytest; it comes from the pytest-mock plugin. It works locally because someone installed the plugin on their machine long ago, but CI installs only what the requirements declare, so the fixture is unknown there. Add pytest-mock to the test dependencies, reinstall in the CI job, and confirm the plugin registers with pytest --fixtures.
The query
pytest E fixture 'mocker' not found - pytest-mock missing in CI requirementsUse this when
- pytest fails in CI with fixture 'mocker' not found
- the same tests pass on every developer machine
- mocker is used across many test files
Not for
- the mocker fixture exists but mocking misbehaves at runtime (a usage bug, not a missing plugin)
- a custom fixture also named mocker shadows the plugin one (rename yours)
- pytest itself is missing in CI (fix the base install first)
Steps
1. Confirm the plugin is present locally but absent in CI
Check both environments. The asymmetry is the diagnosis.
pip show pytest-mock # locally: installed
pytest --trace-config 2>&1 | grep -i mock- name: Check plugin in CI
run: pip show pytest-mock || echo "pytest-mock NOT installed"Expected output: local shows the plugin installed and CI shows it missing.
2. Add pytest-mock to the declared test dependencies
Put it where the CI install step reads: the requirements file, the test extra, or the dev dependency group. Pin a version.
# requirements-test.txt
pytest-mock==3.14.0Expected output: the dependency is declared in a file CI installs from.
3. Reinstall in CI and verify the fixture registers
Reinstall and ask pytest to list its fixtures. The mocker fixture appearing in the list is the acceptance check.
pip install -r requirements-test.txt
pytest --fixtures -q 2>&1 | grep -A2 "^mocker"Expected output: the mocker fixture is listed and the previously failing tests collect and run.
Tool compatibility
- pytest 7 - 8
- pytest-mock 3.x
- pip-based CI installs
Variant phrasings
- E fixture 'mocker' not found in CI
- pytest fixture 'mocker' not found after fresh install
- mocker fixture missing in GitHub Actions
Why it happens
Pytest discovers fixtures from installed plugins at startup. A plugin installed ad hoc on a laptop is invisible to a clean CI environment that installs strictly from declared requirements. The fixture error is therefore a dependency-declaration bug wearing a test-collection disguise.
Edge cases / pitfalls
- lockfile drift causes the twin of this bug: requirements list pytest-mock but the lockfile resolved it away; regenerate the lockfile
- pytest -p no:mock disables the plugin even when installed; check for stray -p flags in addopts
- some teams install test deps only for a lint job and forget the test job; every job that runs pytest needs the plugin
- unittest.mock is always available as a fallback, but switching APIs mid-suite is churn; declare the plugin instead
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_A-KG5E4LrW3GsX5mqP8XFQ