Pytest plugin to collect code coverage from applications running inside Docker containers
Project description
pytest-cov-container
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
- Before tests — injects a coverage wrapper,
.coveragerc, andrun.shinto your SAM build directory - During tests — your containerized app runs under
coveragevia the injected wrapper - After tests — extracts
.coverage.*files from containers and runscoverage combineto 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 (
--covflag present) [tool.pytest-cov-container]is configured inpyproject.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
.coveragercwithparallel = trueandsigterm = true - Writing
_cov_wrapper.pythat starts coverage, handlesSIGTERM/SIGUSR1, and runs your entrypoint - Writing
run.shto bootstrap the wrapper - Extracting
.coverage.*files from/tmpin 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0bdb010c6b9782ec2eb3f19b6a164b2a078b197faf217d2af6e566cfbd380368
|
|
| MD5 |
2dc8bcfd8dd3a19db6657936906af655
|
|
| BLAKE2b-256 |
e28e3de1a502c0181ae69fffb0c0a955ffbc44d273334b904db7ad2a96dea6c5
|
Provenance
The following attestation bundles were made for pytest_cov_container-0.0.1.tar.gz:
Publisher:
release.yaml on tomaDev/pytest-cov-container
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_cov_container-0.0.1.tar.gz -
Subject digest:
0bdb010c6b9782ec2eb3f19b6a164b2a078b197faf217d2af6e566cfbd380368 - Sigstore transparency entry: 1167718854
- Sigstore integration time:
-
Permalink:
tomaDev/pytest-cov-container@8aed374622697619a501772be9419ee56ae1cd95 -
Branch / Tag:
refs/tags/0.0.1 - Owner: https://github.com/tomaDev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@8aed374622697619a501772be9419ee56ae1cd95 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pytest_cov_container-0.0.1-py3-none-any.whl.
File metadata
- Download URL: pytest_cov_container-0.0.1-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0200f657973703a3ea0e6a9b2cf0c46a8cda495849223bec39c8d8e6dfa56f1c
|
|
| MD5 |
144abbe8d85213f79649277445e4fe65
|
|
| BLAKE2b-256 |
cc3c30e5e89455c35465203333c9e172c09a710b46151fd2cf9f0510bdf91531
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_cov_container-0.0.1-py3-none-any.whl -
Subject digest:
0200f657973703a3ea0e6a9b2cf0c46a8cda495849223bec39c8d8e6dfa56f1c - Sigstore transparency entry: 1167718896
- Sigstore integration time:
-
Permalink:
tomaDev/pytest-cov-container@8aed374622697619a501772be9419ee56ae1cd95 -
Branch / Tag:
refs/tags/0.0.1 - Owner: https://github.com/tomaDev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@8aed374622697619a501772be9419ee56ae1cd95 -
Trigger Event:
push
-
Statement type: