Skip to main content

Pytest plugin to collect code coverage from applications running inside Docker containers

Project description

pytest-cov-container

PyPI - Version PyPI - Python Version security: bandit

Collect code coverage from Python applications running inside Docker containers during integration tests. Works alongside pytest-cov to combine container coverage with your local test coverage.

Built for projects using AWS SAM local testing, but works with any Docker-based test workflow.

How It Works

  1. Before tests — injects a coverage wrapper, .coveragerc, and run.sh into your SAM build directory
  2. During tests — your containerized app runs under coverage via the injected wrapper
  3. After tests — extracts .coverage.* files from containers and runs coverage combine to merge them with local results

Installation

pip install pytest-cov-container

Requires Python 3.11+.

Configuration

Add to your pyproject.toml:

[tool.pytest-cov-container]
image_pattern = "samcli/lambda*"
label = "pytest-cov-container"

[tool.pytest-cov-container.path_mapping]
"src/api" = "/var/task"

[tool.pytest-cov-container.python]
build_dir = ".aws-sam/build/ApiFunction"
entrypoint = "uvicorn app:app --host 0.0.0.0 --port 8080"
Key Description
image_pattern Glob pattern to match container image tags
label Docker label to filter containers
path_mapping Maps host source paths to container paths (used by coverage combine)
build_dir SAM build output directory where coverage files are injected
entrypoint The command your app runs inside the container
language Language driver to use (default: "python")
enabled Set to false to disable (default: true)

Usage

Run your tests with --cov as usual:

pytest --cov=src/api tests/

The plugin activates automatically when:

  • pytest-cov is active (--cov flag present)
  • [tool.pytest-cov-container] is configured in pyproject.toml

Disable it for a run with:

pytest --cov=src/api --no-cov-container tests/

Mid-Session Collection

If you need to collect coverage before containers stop (e.g., in a session-scoped fixture teardown), use the public API:

from pytest_cov_container import collect_container_coverage

@pytest.fixture(scope="session")
def sam_api():
    proc = start_sam(...)
    yield SAM_URL
    collect_container_coverage()
    proc.terminate()

This sends SIGUSR1 to running containers to flush coverage data, then extracts the files.

Pluggable Drivers

Language support is pluggable via entry points. The built-in Python driver handles:

  • Writing .coveragerc with parallel = true and sigterm = true
  • Writing _cov_wrapper.py that starts coverage, handles SIGTERM/SIGUSR1, and runs your entrypoint
  • Writing run.sh to bootstrap the wrapper
  • Extracting .coverage.* files from /tmp in containers

To add a driver for another language, register an entry point:

[project.entry-points."pytest_cov_container.drivers"]
node = "my_package.drivers.node:NodeDriver"

Drivers must implement the LanguageDriver protocol from pytest_cov_container.models.

Development

# Run tests
hatch test

# Run across all Python versions
hatch test --all

# Format and lint
hatch fmt

# Type check
hatch run types:check

# Security scan
hatch run security:scan

License

pytest-cov-container is distributed under the terms of the MIT license.

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

pytest_cov_container-0.0.1.tar.gz (53.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_cov_container-0.0.1-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file pytest_cov_container-0.0.1.tar.gz.

File metadata

  • Download URL: pytest_cov_container-0.0.1.tar.gz
  • Upload date:
  • Size: 53.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_cov_container-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0bdb010c6b9782ec2eb3f19b6a164b2a078b197faf217d2af6e566cfbd380368
MD5 2dc8bcfd8dd3a19db6657936906af655
BLAKE2b-256 e28e3de1a502c0181ae69fffb0c0a955ffbc44d273334b904db7ad2a96dea6c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_cov_container-0.0.1.tar.gz:

Publisher: release.yaml on tomaDev/pytest-cov-container

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_cov_container-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_cov_container-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0200f657973703a3ea0e6a9b2cf0c46a8cda495849223bec39c8d8e6dfa56f1c
MD5 144abbe8d85213f79649277445e4fe65
BLAKE2b-256 cc3c30e5e89455c35465203333c9e172c09a710b46151fd2cf9f0510bdf91531

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_cov_container-0.0.1-py3-none-any.whl:

Publisher: release.yaml on tomaDev/pytest-cov-container

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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