Skip to content

Pytest Fixture Not Found Error Fix

DodaTech Updated 2026-06-24 2 min read

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.py for project-wide fixtures
  • Keep scoped fixtures in directory-level conftest.py files
  • Register third-party fixtures with pytest_plugins
  • Run pytest --fixtures to list all discovered fixtures

Common Mistakes with fixture not found

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. 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

### Why does pytest say "fixture not found" when the file exists?

The fixture must be in a conftest.py file or in the same test file that uses it. pytest does not scan all Python files for fixtures — only conftest.py and the test file itself. Move the fixture to conftest.py or mark the helper module with pytest_plugins.

What is the difference between conftest.py scope levels?

A conftest.py in a directory makes its fixtures available to all tests in that directory and subdirectories. Higher directories don't automatically inherit. Place project-wide fixtures in a top-level conftest.py and directory-specific fixtures in subdirectory conftest.py files.

How do I use fixtures from a third-party package?

Register the package with pytest_plugins = ['package_name'] in your conftest.py. Some packages auto-register — check their documentation. Run pytest --fixtures -v to verify the fixtures appear in the list.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro