skillfed

pytest-mock-resources

A pytest plugin for easily instantiating reproducible mock resources.

pytest-mock-resources v2.12.4 312.4K downloads/30d#7,725 on PyPI205
Permissive license MIT Active released

What it is and what it does

pytest-mock-resources is a pytest plugin that manages the lifecycle of containerized databases and services for testing. Instead of mocking database calls or maintaining a shared test database, it spins up isolated real instances (PostgreSQL, MySQL, Redis, MongoDB, SQLite, Redshift, Moto) in Docker containers, tears them down after each test, and provides them as pytest fixtures. This lets you write tests that execute actual queries against a real schema, catching bugs that mocks would miss—like incorrect joins, N+1 queries, or schema mismatches—while keeping tests isolated and reproducible across machines.

The package assumes you're using pytest and have Docker available. It handles container startup, teardown, and provides a session object (for SQL databases via sqlalchemy) or client (for NoSQL/cache stores) that you inject into your test functions. It supports both synchronous and async fixtures, making it suitable for modern async test suites.

Use it for:

  • Test sqlalchemy ORM queries with real joins and eager loading to verify query correctness before deployment.
  • Validate data access layers against actual PostgreSQL or MySQL schemas without mocking database calls.
  • Test Redis or MongoDB integration code with real container instances that reflect production behavior.
  • Run integration tests in CI/CD pipelines where each test gets a fresh, isolated database state.
  • Develop tests for Redshift-specific features like COPY/UNLOAD commands using a compatible fixture.
  • Catch N+1 query problems and lazy-loading bugs that mock-based tests would not surface.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Provides pytest fixtures that spin up real database and service containers (PostgreSQL, MySQL, Redis, MongoDB, etc.) for integration testing, eliminating the need to mock database queries.

Yes, if you write integration tests for code that queries databases or interacts with services. The low install friction, active maintenance, permissive license, and absence of known vulnerabilities make it a solid choice. The Docker requirement is a real constraint for some environments, but for standard development and CI setups it is straightforward. Use it when mocking database calls is insufficient and you need to test actual query logic.

Install

pytest-mock-resources on PyPI

pip

pip install pytest-mock-resources

uv

uv add pytest-mock-resources

poetry

poetry add pytest-mock-resources

Installing pytest-mock-resources

Before you install

Low friction installation with a pure-Python wheel. Maintenance is active with a recent release and ongoing commits. Core dependencies are lightweight (pytest, sqlalchemy, typing_extensions, importlib-metadata), though Docker availability is required for most fixtures beyond SQLite.

License in practice

MIT license is permissive; you can use this package freely in commercial and private projects with minimal restrictions.

Quickstart

pip install pytest-mock-resources

from pytest_mock_resources import create_postgres_fixture

pg = create_postgres_fixture(session=True)

def test_query(pg):
    pg.add(User(name='Alice'))
    pg.flush()
    result = pg.query(User).all()
    assert len(result) == 1

Docker must be installed and running (or accessible via remote Docker) for database fixtures other than SQLite. Requires Python 3.7 or later.

Verify before relying

  • Whether async fixture support works with all database backends or only specific ones.
  • Performance characteristics when running many tests with container lifecycle overhead.
  • Compatibility with CI/CD environments that restrict Docker access.
  • Whether pre-population via 'actions' is supported for all resource types.

Package facts

License MIT (permissive)
Python support supports the current Python release (<4,>=3.7)
Install friction low — pure-Python wheel
Runtime dependencies 4 — pytest, sqlalchemy, typing_extensions, importlib-metadata
Maintenance actively maintained — 331 days since the last release
Last repo commit
First released
Downloads 312,380/month — #7,725 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: pytest_mock_resources-2.12.4-py3-none-any.whl

Keywords: pytest, sqlalchemy, docker, fixture, mock

Framework :: PytestLicense :: OSI Approved :: MIT LicenseProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9

Tags

pytest database fixturesdocker-based test resourcesintegration testing with real databasespytest postgres fixturemock database for testingcontainerized test resourcespytest sqlalchemy testing
pytest-pluginintegration-testingdocker-fixtures

More Testing packages