pytest-operator

0.43.2 · active · verified Fri Apr 17

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.

Common errors

Warnings

Install

Imports

Quickstart

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"

view raw JSON →