Registry / testing / pytest-container

pytest-container

JSON →
library0.4.4pypypi✓ verified 87d ago

Pytest fixtures for writing container-based tests with support for OCI containers (Docker/Podman). Version 0.4.4 provides decorators and fixtures to manage container lifecycle. Active development, monthly releases.

pip install pytest-container
INSTALL
IMPORT
SIG · PYTEST-CONTAINER
P
pytest-container
testingpythonv0.4.4
Install
2.9s avg
Import
644ms
Disk
31MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.684s · 32.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.9s · import 0.604s · 33MB
31MB installed
● package 31MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

container
from pytest_container import container
from pytest_container.fixtures import container
container fixture is at top-level
DerivedContainer
from pytest_container import DerivedContainer

Basic usage: use the container fixture or define a custom DerivedContainer class.

from pytest_container import container, DerivedContainer def test_container(container): # container is a pytest fixture that starts a container # inspect container object assert container.connection.run("echo hello").stdout.strip() == "hello" # Define custom container image class TestWithCustomImage: CONTAINER_IMAGE = DerivedContainer( base="registry.opensuse.org/opensuse/tumbleweed:latest", containerfile=""" RUN zypper -n in python3 """ ) def test_custom(self, container): assert container.connection.run("python3 --version").rc == 0
Debug
Known issues
breakingVersion 0.4.x changed the fixture from `container` to require explicit import. Old code using `pytest_container` as a plugin may break if fixture is not imported.
fix
Add `from pytest_container import container` at the top of your test file.
affects: <0.4.0
gotchaThe `container` fixture works only if the test class has a `CONTAINER_IMAGE` attribute or uses `@pytest.mark.parametrize`. Otherwise, the fixture raises an error.
fix
Define a `CONTAINER_IMAGE` class attribute or use `container` fixture with a parametrized image.
affects: all
deprecatedThe `container_request` fixture is deprecated in 0.4.x. Use the `container` fixture instead.
fix
Replace `container_request` with `container` in your test functions.
affects: >=0.4.0
Errors
Common errors & fixes
AttributeError: module 'pytest_container' has no attribute 'container'
The `container` fixture is not automatically injected; it must be imported.
fix
Add `from pytest_container import container` at the top of your test file.
Fixture 'container' not found
The `container` fixture is not available because it hasn't been imported or the plugin not installed.
fix
Ensure pytest-container is installed (`pip install pytest-container`) and import the fixture: `from pytest_container import container`.
Upgrade
Version history
0.4.4latest on PyPI · released Jun 30, 2025
Audit
Dependencies
pytestrequiredCore test runner
container-derivationrequiredContainer image derivation logic
dockeroptionalDocker SDK (optional if using podman)
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
pytest-container — pip install pytest-container · libregistry