Pytest Fixture Not Found Error Fix
In this tutorial, you'll learn about Pytest Fixture Not Found Error Fix. We cover key concepts, practical examples, and best practices.
You run pytest and get fixture 'db_session' not found — pytest cannot locate the fixture you defined, even though it's in the same directory.
The Problem
# WRONG — fixture defined in a regular test file
def test_database(db_session):
assert db_session.is_connected
fixture 'db_session' not found
available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, ...
The fixture db_session is defined in a helper module but not imported. pytest discovers fixtures only from conftest.py files and from the test file itself.
Step-by-Step Fix
1. Move shared fixtures to conftest.py
# tests/conftest.py
import pytest
@pytest.fixture
def db_session():
session = create_test_session()
yield session
session.close()
@pytest.fixture
def api_client():
from myapp import create_app
app = create_app()
return app.test_client()
Any test file inside tests/ can now use db_session and api_client without importing them.
2. Use pytest's fixture discovery correctly
# tests/test_users.py — no import needed
def test_create_user(db_session):
user = db_session.query(User).first()
assert user is None
3. Check conftest.py location
tests/
conftest.py # fixtures available to all test files in tests/
integration/
conftest.py # fixtures only available in integration/
test_api.py
unit/
test_models.py # uses conftest.py from tests/
Fixtures in tests/integration/conftest.py are only available to tests in tests/integration/ and its subdirectories.
4. Use pytest_plugins for third-party fixtures
# conftest.py
pytest_plugins = ['pytest_django', 'pytest_asyncio']
Third-party package fixtures like django_db_setup or event_loop need explicit plugin registration.
Expected output:
collected 3 items
tests/test_users.py::test_create_user PASSED
tests/test_models.py::test_query PASSED
tests/integration/test_api.py::test_endpoint PASSED
Prevention Tips
- Define shared fixtures in
conftest.py, not in helper imports - Use a top-level
conftest.pyfor project-wide fixtures - Keep scoped fixtures in directory-level
conftest.pyfiles - Register third-party fixtures with
pytest_plugins - Run
pytest --fixturesto list all discovered fixtures
Common Mistakes with fixture not found
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world PYTEST code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro