API

Fixture Functions

class pytest_mock_resources.StaticStatements(*statements)

A discriminator for statements which are safe to execute exactly once.

pytest_mock_resources.create_mongo_fixture(scope='function')

Produce a mongo fixture.

Any number of fixture functions can be created. Under the hood they will all share the same database server.

Parameters

scope – Passthrough pytest’s fixture scope.

pytest_mock_resources.create_mysql_fixture(*ordered_actions, scope='function', tables=None, session=None, engine_kwargs=None)

Produce a MySQL fixture.

Any number of fixture functions can be created. Under the hood they will all share the same database server.

Parameters
  • ordered_actions – Any number of ordered actions to be run on test setup.

  • scope – Passthrough pytest’s fixture scope.

  • tables – Subsets the tables created by ordered_actions. This is generally most useful when a model-base was specified in ordered_actions.

  • session – Whether to return a session instead of an engine directly. This can either be a bool or a callable capable of producing a session.

  • engine_kwargs – Optional set of kwargs to send into the engine on creation.

pytest_mock_resources.create_postgres_fixture(*ordered_actions, scope='function', tables=None, session=None, async_=False, createdb_template='template1', engine_kwargs=None, template_database=True, actions_share_transaction=None)

Produce a Postgres fixture.

Any number of fixture functions can be created. Under the hood they will all share the same database server.

Parameters
  • ordered_actions – Any number of ordered actions to be run on test setup.

  • scope – Passthrough pytest’s fixture scope.

  • tables – Subsets the tables created by ordered_actions. This is generally most useful when a model-base was specified in ordered_actions.

  • session – Whether to return a session instead of an engine directly. This can either be a bool or a callable capable of producing a session.

  • async – Whether to return an async fixture/client.

  • createdb_template – The template database used to create sub-databases. “template1” is the default chosen when no template is specified.

  • engine_kwargs – Optional set of kwargs to send into the engine on creation.

  • template_database – Defaults to True. When True, amortizes the cost of performing database setup through ordered_actions, by performing them once into a postgres “template” database, then creating all subsequent per-test databases from that template.

  • actions_share_transaction – When True, the transaction used by ordered_actions context will be the same as the one handed to the test function. This is required in order to support certain usages of ordered_actions, such as the creation of temp tables through a `Statements object. By default, this behavior is enabled for synchronous fixtures for backwards compatibility; and disabled by default for asynchronous fixtures (the way v2-style/async features work in SQLAlchemy can lead to bad default behavior).

pytest_mock_resources.create_redis_fixture(scope='function')

Produce a Redis fixture.

Any number of fixture functions can be created. Under the hood they will all share the same database server.

Note

If running tests in parallel, the implementation fans out to different redis “database”s, up to a 16 (which is the default container fixed limit). This means you can only run up to 16 simultaneous tests.

Additionally, any calls to flushall or any other cross-database calls will still represent cross-test state.

Finally, the above notes are purely describing the current implementation, and should not be assumed. In the future, the current database selection mechanism may change, or databases may not be used altogether.

Parameters

scope (str) – The scope of the fixture can be specified by the user, defaults to “function”.

Raises

KeyError – If any additional arguments are provided to the function than what is necessary.

pytest_mock_resources.create_redshift_fixture(*ordered_actions, scope='function', tables=None, session=None, async_=False, createdb_template='template1', engine_kwargs=None, template_database=True, actions_share_transaction=None)

Produce a Redshift fixture.

Any number of fixture functions can be created. Under the hood they will all share the same database server.

Note that, by default, redshift uses a postgres container as the database server and attempts to reintroduce approximations of Redshift features, such as S3 COPY/UNLOAD, redshift-specific functions, and other specific behaviors.

Parameters
  • ordered_actions – Any number of ordered actions to be run on test setup.

  • scope – Passthrough pytest’s fixture scope.

  • tables – Subsets the tables created by ordered_actions. This is generally most useful when a model-base was specified in ordered_actions.

  • session – Whether to return a session instead of an engine directly. This can either be a bool or a callable capable of producing a session.

  • async – Whether to return an async fixture/client.

  • createdb_template – The template database used to create sub-databases. “template1” is the default chosen when no template is specified.

  • engine_kwargs – Optional set of kwargs to send into the engine on creation.

  • template_database – Defaults to True. When True, amortizes the cost of performing database setup through ordered_actions, by performing them once into a postgres “template” database, then creating all subsequent per-test databases from that template.

  • actions_share_transaction – When True, the transaction used by ordered_actions context will be the same as the one handed to the test function. This is required in order to support certain usages of ordered_actions, such as the creation of temp tables through a `Statements object. By default, this behavior is enabled for synchronous fixtures for backwards compatibility; and disabled by default for asynchronous fixtures (the way v2-style/async features work in SQLAlchemy can lead to bad default behavior).

pytest_mock_resources.create_sqlite_fixture(*ordered_actions, scope='function', tables=None, session=None, decimal_warnings=False, postgres_like=True)

Produce a SQLite fixture.

Any number of fixture functions can be created. Under the hood they will all share the same database server.

Parameters
  • ordered_actions – Any number of ordered actions to be run on test setup.

  • scope – Passthrough pytest’s fixture scope.

  • tables – Subsets the tables created by ordered_actions. This is generally most useful when a model-base was specified in ordered_actions.

  • session – Whether to return a session instead of an engine directly. This can either be a bool or a callable capable of producing a session.

  • decimal_warnings – Whether to show sqlalchemy decimal warnings related to precision loss. The default False suppresses these warnings.

  • postgres_like – Whether to add extra SQLite features which attempt to mimic postgres enough to stand in for it for testing.

Fixture Config

class pytest_mock_resources.MongoConfig(**kwargs)

Define the configuration object for mongo.

Parameters
  • image (str) – The docker image:tag specifier to use for mongo containers. Defaults to "mongo:3.6".

  • host (str) – The hostname under which a mounted port will be available. Defaults to "localhost".

  • port (int) – The port to bind the container to. Defaults to 28017.

  • ci_port (int) – The port to bind the container to when a CI environment is detected. Defaults to 27017.

  • root_database (str) – The name of the root mongo database to create. Defaults to "dev-mongo".

class pytest_mock_resources.MysqlConfig(**kwargs)

Define the configuration object for MySql.

Parameters
  • image (str) – The docker image:tag specifier to use for mysql containers. Defaults to "mysql:5.6".

  • host (str) – The hostname under which a mounted port will be available. Defaults to "localhost".

  • port (int) – The port to bind the container to. Defaults to 5532.

  • ci_port (int) – The port to bind the container to when a CI environment is detected. Defaults to 5432.

  • username (str) – The username of the root user Defaults to "user".

  • password (str) – The password of the root password Defaults to "password".

  • root_database (str) – The name of the root database to create. Defaults to "dev".

class pytest_mock_resources.PostgresConfig(**kwargs)

Define the configuration object for postgres.

Parameters
  • image (str) – The docker image:tag specifier to use for postgres containers. Defaults to "postgres:9.6.10-alpine".

  • host (str) – The hostname under which a mounted port will be available. Defaults to "localhost".

  • port (int) – The port to bind the container to. Defaults to 5532.

  • ci_port (int) – The port to bind the container to when a CI environment is detected. Defaults to 5432.

  • username (str) – The username of the root postgres user Defaults to "user".

  • password (str) – The password of the root postgres password Defaults to "password".

  • root_database (str) – The name of the root postgres database to create. Defaults to "dev".

  • drivername (str) – The sqlalchemy driver to use Defaults to "postgresql+psycopg2".

class pytest_mock_resources.RedisConfig(**kwargs)

Define the configuration object for redis.

Parameters
  • image (str) – The docker image:tag specifier to use for redis containers. Defaults to "redis:5.0.7".

  • host (str) – The hostname under which a mounted port will be available. Defaults to "localhost".

  • port (int) – The port to bind the container to. Defaults to 6380.

  • ci_port (int) – The port to bind the container to when a CI environment is detected. Defaults to 6379.

pytest_mock_resources.pmr_mongo_config()

Override this fixture with a MongoConfig instance to specify different defaults.

Examples

>>> @pytest.fixture(scope='session')
... def pmr_mongo_config():
...     return MongoConfig(image="mongo:3.4", root_database="foo")
pytest_mock_resources.pmr_mysql_config()

Override this fixture with a MysqlConfig instance to specify different defaults.

Examples

>>> @pytest.fixture(scope='session')
... def pmr_mysql_config():
...     return MysqlConfig(image="mysql:5.2", root_database="foo")
pytest_mock_resources.pmr_postgres_config()

Override this fixture with a PostgresConfig instance to specify different defaults.

Examples

>>> @pytest.fixture(scope='session')
... def pmr_postgres_config():
...     return PostgresConfig(image="postgres:9.6.10", root_database="foo")
pytest_mock_resources.pmr_redis_config()

Override this fixture with a RedisConfig instance to specify different defaults.

Examples

>>> @pytest.fixture(scope='session')
... def pmr_redis_config():
...     return RedisConfig(image="redis:6.0")