Skip to main content

A pytest plugin for easily instantiating reproducible mock resources.

Project description

CircleCI codecov Documentation Status

Introduction

Code which depends on external resources such a databases (postgres, redshift, etc) can be difficult to write automated tests for. Conventional wisdom might be to mock or stub out the actual database calls and assert that the code works correctly before/after the calls.

However take the following, simple example:

def serialize(users):
    return [
        {
            'user': user.serialize(),
            'address': user.address.serialize(),
            'purchases': [p.serialize() for p in user.purchases],
        }
        for user in users
    ]

def view_function(session):
    users = session.query(User).join(Address).options(selectinload(User.purchases)).all()
    return serialize(users)

Sure, you can test serialize, but whether the actual query did the correct thing truly requires that you execute the query.

The Pitch

Having tests depend upon a real postgres instance running somewhere is a pain, very fragile, and prone to issues across machines and test failures.

Therefore pytest-mock-resources (primarily) works by managing the lifecycle of docker containers and providing access to them inside your tests.

As such, this package makes 2 primary assumptions:

  • You're using pytest (hopefully that's appropriate, given the package name)
  • For many resources, docker is required to be available and running (or accessible through remote docker).

If you aren't familiar with Pytest Fixtures, you can read up on them in the Pytest documentation.

In the above example, your test file could look something like

from pytest_mock_resources import create_postgres_fixture
from models import ModelBase

pg = create_postgres_fixture(ModelBase, session=True)

def test_view_function_empty_db(pg):
  response = view_function(pg)
  assert response == ...

def test_view_function_user_without_purchases(pg):
  pg.add(User(...))
  pg.flush()

  response = view_function(pg)
  assert response == ...

def test_view_function_user_with_purchases(pg):
  pg.add(User(..., purchases=[Purchase(...)]))
  pg.flush()

  response = view_function(pg)
  assert response == ...

Existing Resources (many more possible)

  • SQLite

    from pytest_mock_resources import create_sqlite_fixture
    
  • Postgres

    from pytest_mock_resources import create_postgres_fixture
    
  • Redshift

    note Uses postgres under the hood, but the fixture tries to support as much redshift functionality as possible (including redshift's COPY/UNLOAD commands).

    from pytest_mock_resources import create_redshift_fixture
    
  • Mongo

    from pytest_mock_resources import create_mongo_fixture
    
  • Redis

    from pytest_mock_resources import create_redis_fixture
    
  • MySQL

    from pytest_mock_resources import create_mysql_fixture
    

Features

General features include:

  • Support for "actions" which pre-populate the resource you're mocking before the test
  • Async fixtures
  • Custom configuration for container/resource startup

Installing

# Basic fixture support
pip install "pytest-mock-resources"

# For postgres install EITHER of the following:
pip install "pytest-mock-resources[postgres-binary]"
pip install "pytest-mock-resources[postgres]"

# For postgres async
pip install "pytest-mock-resources[postgres-async]"

# For redshift install EITHER of the following:
# (redshift fixtures require postgres dependencies...)
pip install "pytest-mock-resources[postgres, redshift]"
pip install "pytest-mock-resources[postgres-binary, redshift]"

# For mongo install the following:
pip install "pytest-mock-resources[mongo]"

# For redis
pip install "pytest-mock-resources[redis]"

# For mysql
pip install "pytest-mock-resources[mysql]"

Possible Future Resources

  • Rabbit Broker
  • AWS Presto

Feel free to file an issue if you find any bugs or want to start a conversation around a mock resource you want implemented!

Python 2

Releases in the 1.x series were supportive of python 2. However starting from 2.0.0, support for python 2 was dropped. We may accept bugfix PRs for the 1.x series, however new development and features will not be backported.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-mock-resources-2.4.3.tar.gz (33.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_mock_resources-2.4.3-py3-none-any.whl (47.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest-mock-resources-2.4.3.tar.gz.

File metadata

  • Download URL: pytest-mock-resources-2.4.3.tar.gz
  • Upload date:
  • Size: 33.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.6 Darwin/21.4.0

File hashes

Hashes for pytest-mock-resources-2.4.3.tar.gz
Algorithm Hash digest
SHA256 7281e8e6c92cd83335a952b8e3e1a3c76dd2f9b0588cd1518ca36346f79d8cf9
MD5 edd445a9df845c381c316c331d0c1f98
BLAKE2b-256 c35fe5c4489cd69d888c4e34b40d91379d8ac730172a1cebb67b2935292f7339

See more details on using hashes here.

File details

Details for the file pytest_mock_resources-2.4.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_mock_resources-2.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a3f82b69b80d934f25a887f0474b7af55f0fcb4257fad7ffb03d27368c1bd212
MD5 8008cd511ed53d6d74780722243af518
BLAKE2b-256 b2b79e9bdfde8cda35240a11b3a13e29a1214864756babe3987f83437c7be1ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page