Package: managed_service_fixtures
managed-service-fixturesmanaged-service-fixturesis a collection ofpytest fixturesused to manage external processes such as databases, redis, and vault while running integration tests.Testing Python applications that depend on external services such as a database, redis server, or storing data in Amazon S3 can be difficult. One solution is Unit testing: mock any kind of network IO and isolate tests to your application logic alone. With larger applications, you may find yourself in "mock hell" or discover that you're missing real-world bugs.managed-service-fixturesis designed to help you write Integration tests that require an external service be active. In the simplest case, wherepytestis run serially and manages starting and stopping the service, thenmanaged-service-fixturesis basically a wrapper around the excellentmirakuru.pylibrary with somePydanticmodeling for the service connection details. There are two common non-simple use cases this library addresses as well.The first non-simple use-case is running tests in parallel withpytest-xdist. A naive fixture that starts and stops a service withmirakuru, even if it were sessions coped, would end up creating one service for each worker.managed-service-fixturesaddresses this situation by usingFileLockand a state file that each worker registers itself in. Only one worker ends up being the manager, responsible for starting the service and then shutting it down once all other workers have unregistered themselves (completed their tests).The second non-simple use-case is managing services outside of thepytestfixtures. You might want to point your tests towards a service on a remote cluster. You might also want to stoppytestfrom tearing down a database after the tests complete so that you can introspect and debug what is in there. In those cases where you are manually starting and stopping services, you can set environment variables pointing to a file with connection details to those services, then the fixtures inmanaged-service-fixtureswill not try to handle lifecycle management itself.ExampleSee thetests/directory for more usage examples.# tests/conftest.py# https://docs.pytest.org/en/7.1.x/how-to/fixtures.html#using-fixtures-from-other-projectspytest_plugins='managed_service_fixtures'# tests/test_vault.pyimporthvacfrommanaged_service_fixturesimportVaultDetailsdeftest_vault_connection(managed_vault:VaultDetails):client=hvac.Client(url=managed_vault.url,token=managed_vault.token)assertclient.is_authenticated()assertclient.sys.is_initialized()assertnotclient.sys.is_sealed()FixturesYou may need to install a system library or CLI depending on which service you want to manage withmirakuru/managed-service-fixtures.managed_cockroachstarts an in-memory instance ofCockroachDB, seeinstall instructionsfor setting up thecockroachCLImanaged_motostarts aMoto - Mock AWS Service,pip install mototo enable the CLImanaged_redisstarts aRedisserver, Seeinstall instructionsto enable theredis-serverCLImanaged_vaultstarts aVaultserver, seeinstall instructionsto enable thevaultCLIASGI appsmanaged-service-fixturessupports running an ASGI app (such as aFastAPIorStarletteapp) withuvicornas a managed service. You may want to use this if:You're usinghttpx.AsyncClientforasync testsand need to test websocket endpointsYou have awebsocketsapplication/library and need to spin up a server to test request/responsesA downside to running an ASGI app in an external process is that you lose breakpoint/debug support in your tests.