Registry / testing / pytest-operator

pytest-operator

JSON →
library0.43.2pypypiunverified

pytest-operator provides a set of pytest fixtures designed to simplify the testing of Charmed Operators, particularly for integration and functional tests. It abstracts away much of the complexity of deploying and managing Juju models and charms within a test environment. The current version is 0.43.2, and it maintains an active release cadence with frequent patch updates and occasional minor feature releases.

pip install pytest-operator
INSTALL
IMPORT
SIG · PYTEST-OPERATOR
P
pytest-operator
testingpythonv0.43.2
Install
11.8s avg
Import
Disk
160MB
Pass rate
8/ 10
Env Coverage8 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.43.2 · 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
glibc
py 3.10
✓ —
✓ 11.7s
py 3.11
✓ —
✓ 12s
py 3.12
✓ —
✓ 10.85s
py 3.13
✕ build_error
✕ build_error
py 3.9
✓ —
✓ 12.7s
160MB installed
● package 160MB
Code
Verified usage

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

pytest_plugins
pytest_plugins = ['pytest_operator.plugin']
from pytest_operator import plugin
pytest plugins are loaded via the `pytest_plugins` variable in a `conftest.py` or test module, not direct import.

This quickstart demonstrates a basic integration test using `pytest-operator`. It shows how to declare the plugin in `conftest.py`, build a charm using `ops_test.build_charm()`, deploy it to a temporary Juju model, and assert its status. Ensure `charmcraft` is installed if you are building charms locally.

# conftest.py pytest_plugins = ["pytest_operator.plugin"] # tests/integration/test_my_charm.py import pytest from pathlib import Path # Assuming your charm source is in a 'src' directory relative to the tests CHARM_ROOT = Path(__file__).parent.parent / "src" async def test_charm_is_deployed(ops_test): # Build the charm locally charm = await ops_test.build_charm(CHARM_ROOT) # Deploy the charm to the ephemeral Juju model await ops_test.model.deploy(charm) # Wait for the application to reach an idle state await ops_test.model.wait_for_idle(apps=[charm.name], status="active", timeout=600) # Assertions about the application state assert ops_test.model.applications[charm.name].units[0].workload_status == "active"
Debug
Known issues
gotchaThe `ops_test` fixture operates on a clean, ephemeral Juju model for each test session. Any deployments or changes made during a test run are automatically cleaned up afterward, meaning no persistent state between test runs.
fix
Be aware that external Juju models are not affected. If you need to debug a model, you can set `PYTEST_OPERATOR_KEEP_MODEL=true` in your environment, but manual cleanup will be required.
affects: All versions
gotchaTo use `ops_test.build_charm()`, `charmcraft` must be installed and accessible in your system's PATH. If `charmcraft` is not found, charm building will fail.
fix
Install charmcraft: `snap install charmcraft --classic` or ensure it's in your virtual environment if using a tool like `pipx`.
affects: All versions
gotchaIntegration tests can be significantly slower than unit tests due to the overhead of building charms, deploying applications, and waiting for Juju model states.
fix
Design tests to be as efficient as possible. Leverage shared fixtures (e.g., a single deployment for multiple tests in a module if safe) and consider using `pytest-xdist` for parallel execution, if applicable, for larger test suites.
affects: All versions
Upgrade
Version history
0.43.2latest on PyPI · released Oct 4, 2025
Audit
Dependencies
pytestrequiredpytest-operator is a plugin for pytest.
charmcraftoptionalUsed for building charm bundles within test environments. Required for ops_test.build_charm().
Agent activity
11 hits · last 30 days
node
8
Bingbot
1
Resources
pytest-operator — pip install pytest-operator · libregistry