Skip to main content

Pytest fixtures to manage external services such as Cockroach DB, Vault, or Redis

Project description

managed-service-fixtures

managed-service-fixtures is a collection of pytest fixtures used 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-fixtures is designed to help you write Integration tests that require an external service be active. In the simplest case, where pytest is run serially and manages starting and stopping the service, then managed-service-fixtures is basically a wrapper around the excellent mirakuru.py library with some Pydantic modeling 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 with pytest-xdist. A naive fixture that starts and stops a service with mirakuru, even if it were sessions coped, would end up creating one service for each worker. managed-service-fixtures addresses this situation by using FileLock and 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 the pytest fixtures. You might want to point your tests towards a service on a remote cluster. You might also want to stop pytest from 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 in managed-service-fixtures will not try to handle lifecycle management itself.

Example

See the tests/ directory for more usage examples.

# tests/conftest.py
# https://docs.pytest.org/en/7.1.x/how-to/fixtures.html#using-fixtures-from-other-projects

pytest_plugins = 'managed_service_fixtures'
# tests/test_vault.py
import hvac
from managed_service_fixtures import VaultDetails


def test_vault_connection(managed_vault: VaultDetails):
    client = hvac.Client(url=managed_vault.url, token=managed_vault.token)
    assert client.is_authenticated()
    assert client.sys.is_initialized()
    assert not client.sys.is_sealed()

Fixtures

You may need to install a system library or CLI depending on which service you want to manage with mirakuru / managed-service-fixtures.

ASGI apps

managed-service-fixtures supports running an ASGI app (such as a FastAPI or Starlette app) with uvicorn as a managed service. You may want to use this if:

  • You're using httpx.AsyncClient for async tests and need to test websocket endpoints
  • You have a websockets application/library and need to spin up a server to test request/responses

A downside to running an ASGI app in an external process is that you lose breakpoint/debug support in your tests.

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

managed_service_fixtures-0.3.1.tar.gz (12.9 kB view hashes)

Uploaded Source

Built Distribution

managed_service_fixtures-0.3.1-py3-none-any.whl (16.2 kB view hashes)

Uploaded Python 3

Supported by

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