Package: pytest-mock-resources
IntroductionCode 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,simpleexample:defserialize(users):return[{'user':user.serialize(),'address':user.address.serialize(),'purchases':[p.serialize()forpinuser.purchases],}foruserinusers]defview_function(session):users=session.query(User).join(Address).options(selectinload(User.purchases)).all()returnserialize(users)Sure, you can testserialize, but whether the actualquerydid the correct thingtrulyrequires that you execute the query.The PitchHaving tests depend upon arealpostgres instance running somewhere is a pain, very fragile, and
prone to issues across machines and test failures.Thereforepytest-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 usingpytest(hopefully that's appropriate, given the package name)For many resources,dockeris 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 thePytest
documentation.In the above example, your test file could look something likefrompytest_mock_resourcesimportcreate_postgres_fixturefrommodelsimportModelBasepg=create_postgres_fixture(ModelBase,session=True)deftest_view_function_empty_db(pg):response=view_function(pg)assertresponse==...deftest_view_function_user_without_purchases(pg):pg.add(User(...))pg.flush()response=view_function(pg)assertresponse==...deftest_view_function_user_with_purchases(pg):pg.add(User(...,purchases=[Purchase(...)]))pg.flush()response=view_function(pg)assertresponse==...Existing Resources (many more possible)SQLitefrompytest_mock_resourcesimportcreate_sqlite_fixturePostgresfrompytest_mock_resourcesimportcreate_postgres_fixtureRedshiftnoteUses postgres under the hood, but the fixture tries to support as much redshift
functionality as possible (including redshift'sCOPY/UNLOADcommands).frompytest_mock_resourcesimportcreate_redshift_fixtureMongofrompytest_mock_resourcesimportcreate_mongo_fixtureRedisfrompytest_mock_resourcesimportcreate_redis_fixtureMySQLfrompytest_mock_resourcesimportcreate_mysql_fixtureMotofrompytest_mock_resourcesimportcreate_moto_fixtureFeaturesGeneral features include:Support for "actions" which pre-populate the resource you're mocking before the testAsync fixturesCustom configuration for container/resource startupInstallation# Basic fixture support i.e. SQLitepipinstall"pytest-mock-resources"# General, docker-based fixture supportpipinstall"pytest-mock-resources[docker]"# Mongo fixture support, installs `pymongo`pipinstall"pytest-mock-resources[mongo]"# Moto fixture support, installs non-driver extras specific to moto supportpipinstall"pytest-mock-resources[moto]"# Redis fixture support, Installs `redis` clientpipinstall"pytest-mock-resources[redis]"# Redshift fixture support, installs non-driver extras specific to redshift supportpipinstall"pytest-mock-resources[redshift]"Additionally there are number ofconvenienceextras currently provided
for installing drivers/clients of specific features. However in most cases,
youshouldalready be installing the driver/client used for that fixture
as as first-party dependency of your project.As such, we recommend against using these extras, and instead explcitly depending
on the package in question in your own project's 1st party dependencies.# Installs psycopg2/psycopg2-binary driverpipinstall"pytest-mock-resources[postgres-binary]"pipinstall"pytest-mock-resources[postgres]"# Installs asyncpg driverpipinstall"pytest-mock-resources[postgres-async]"# Installs pymysql driverpipinstall"pytest-mock-resources[mysql]"Possible Future ResourcesRabbit BrokerAWS PrestoFeel free to file anissueif you find
any bugs or want to start a conversation around a mock resource you want implemented!Python 2Releases 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.