--- id: pytest-mock-resources version: "2.12.4" license: MIT license_treatment: permissive maintenance: active --- # pytest-mock-resources — A pytest plugin for easily instantiating reproducible mock resources. License: permissive · Maintenance: active · Downloads: 312.4K/mo ## 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 above — 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 pip install pytest-mock-resources uv add pytest-mock-resources 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_current - Install friction: low - Maintenance: active - Downloads: 312.4K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags pytest database fixtures, docker-based test resources, integration testing with real databases, pytest postgres fixture, mock database for testing, containerized test resources, pytest sqlalchemy testing, pytest-plugin, integration-testing, docker-fixtures [View on SkillFed](https://skillfed.io/packages/pytest-mock-resources) · [View on PyPI](https://pypi.org/project/pytest-mock-resources/)