pytest-mock-resources
A pytest plugin for easily instantiating reproducible mock resources.
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-resourcesuv
uv add pytest-mock-resourcespoetry
poetry add pytest-mock-resourcesInstalling 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
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
pytest-databasesProvides pytest fixtures that spin up isolated…
permissive · top 15,000 on PyPI
pytest-docker-toolsProvides pytest fixture factories for…
permissive · top 5,000 on PyPI
pytest-redisA pytest plugin that provides fixtures for…
copyleft · top 15,000 on PyPI
pytest-pgsqlA pytest plugin that provides fixtures for…
permissive · top 15,000 on PyPI
mock-alchemyProvides mock helpers for SQLAlchemy sessions…
permissive · top 15,000 on PyPI
pytest-mysqlA pytest plugin that provides MySQL and MariaDB…
copyleft · top 15,000 on PyPI
pytest-containerA pytest plugin that automates container…
copyleft · top 15,000 on PyPI
pytest-postgresqlA pytest plugin that provides fixtures to…
copyleft · top 5,000 on PyPI
pytest-shutilProvides pytest fixtures and utilities for…
permissive · top 15,000 on PyPI
pytest-grpcA pytest plugin that provides fixtures to write…
permissive · top 15,000 on PyPI