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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file managed_service_fixtures-0.3.1.tar.gz.

File metadata

  • Download URL: managed_service_fixtures-0.3.1.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.2 Linux/5.15.0-1050-azure

File hashes

Hashes for managed_service_fixtures-0.3.1.tar.gz
Algorithm Hash digest
SHA256 211530bd643cc9b31fa1e1c3ba6dff75f023cd3719188c0f5177647f9a98e664
MD5 6cf87266a9fd2ebe86bbf37f2240e486
BLAKE2b-256 62dee35c9dae4a3f5ec21e326815a504ab6bada40af44892c38873ff7b8c79ce

See more details on using hashes here.

File details

Details for the file managed_service_fixtures-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for managed_service_fixtures-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 912a1702aa3aff0f1e396f6580bb4515f7b1c0f8025b9b65ed1f64864f6d9f45
MD5 06cb888ceac784c5c06e9819f70049db
BLAKE2b-256 03f74853d043dc42cb7b24c871a771ed1fb083cf920cd9f235f76307a255ed14

See more details on using hashes here.

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