All Projects → schireson → pytest-mock-resources

schireson / pytest-mock-resources

Licence: MIT license
Pytest Fixtures that let you actually test against external resource (Postgres, Mongo, Redshift...) dependent code.

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to pytest-mock-resources

sql2mongo
Use SQL to query MongoDB
Stars: ✭ 14 (-83.33%)
Mutual labels:  mongo
tutorials
Collection of tutorials for various libraries and technologies
Stars: ✭ 98 (+16.67%)
Mutual labels:  mongo
GradleMongoPlugin
Gradle plugin for running a managed instance of Mongo.
Stars: ✭ 48 (-42.86%)
Mutual labels:  mongo
MacroUtils
MacroUtils is a collection of high-level APIs in order to make your life easier when writing STAR-CCM+ JAVA macros.
Stars: ✭ 32 (-61.9%)
Mutual labels:  pytest
Registration-and-Login-using-MERN-stack
Simple Registration and Login component with MERN stack
Stars: ✭ 210 (+150%)
Mutual labels:  mongo
pygments-pytest
A pygments lexer for pytest output
Stars: ✭ 23 (-72.62%)
Mutual labels:  pytest
pymongo inmemory
A mongo mocking library with an ephemeral MongoDB running in memory.
Stars: ✭ 25 (-70.24%)
Mutual labels:  mongo
express-mongo-jwt-boilerplate
Express Mongo JsonWebToken boilerplate
Stars: ✭ 100 (+19.05%)
Mutual labels:  mongo
pytest-datafiles
pytest plugin to create a tmpdir containing a preconfigured set of files and/or directories.
Stars: ✭ 75 (-10.71%)
Mutual labels:  pytest
Python-Studies
All studies about python
Stars: ✭ 56 (-33.33%)
Mutual labels:  pytest
nbcelltests
Cell-by-cell testing for production Jupyter notebooks in JupyterLab
Stars: ✭ 66 (-21.43%)
Mutual labels:  pytest
derivejs
DeriveJS is a reactive ODM - Object Document Mapper - framework, a "wrapper" around a database, that removes all the hassle of data-persistence by handling it transparently in the background, in a DRY manner.
Stars: ✭ 54 (-35.71%)
Mutual labels:  mongo
mongo-playground
Helps developers run mongo queries
Stars: ✭ 16 (-80.95%)
Mutual labels:  mongo
mango
Use mongo-go-driver like mgo
Stars: ✭ 37 (-55.95%)
Mutual labels:  mongo
Rin
Rin is a Redshift data Importer by SQS messaging.
Stars: ✭ 27 (-67.86%)
Mutual labels:  redshift
pytest-embedded
A pytest plugin that designed for embedded testing
Stars: ✭ 40 (-52.38%)
Mutual labels:  pytest
koa-session-mongoose
Mongoose store for Koa sessions
Stars: ✭ 29 (-65.48%)
Mutual labels:  mongo
toutiao
模仿今日头条,实现 APP 端,Server 端, Web 管理端
Stars: ✭ 17 (-79.76%)
Mutual labels:  mongo
PyTest
pytest runner and view annotator for sublime text 3
Stars: ✭ 20 (-76.19%)
Mutual labels:  pytest
continuous-integration-with-python
How to test your python code. How to automatically run your tests for your Python code. How to get reports of the tests coverage
Stars: ✭ 25 (-70.24%)
Mutual labels:  pytest

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
  • Moto

    from pytest_mock_resources import create_moto_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]"

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

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].