Skip to main content

pytest-operator

PyTest plugin to make it easy to create integration tests for Operator Charms.

Usage

Include pytest-operator in the deps of your tox.ini file:

[testenv]
deps =
  pytest
  pytest-operator

Then, just start using the ops_test fixture in your async tests. This module-scoped fixture provides a libjuju Model, helpers to build charms for testing, and the ability to abort testing so that the remaining tests in the module are automatically xfailed (you can also mark a test so that this happens automatically if the test fails; this is typically used on the initial deployment test, where subsequent tests depend on the deployment having succeeded).

As an additional nicety, you don't have to explicitly mark an async test with @pytest.mark.asyncio; if it's a test function / method and it's async, it will be marked automatically.

Example:

import pytest


@pytest.mark.abort_on_fail
async def test_build_and_deploy(ops_test):
    my_charm = await ops_test.build_charm(".")
    await ops_test.model.deploy(my_charm)
    await ops_test.model.wait_for_idle()


async def test_status(ops_test):
    assert ops_test.model.applications["my-charm"].units[0].workload_status == "active"

Building/Downloading Charm Resources

Quite often, when charms are preparing for integration tests, the charms may need to attach resources to the charm for it to function. In these cases, the integration code must either build the resources or pull those from external resources.

Example:

async def test_build_and_deploy(ops_test):
    charm = await ops_test.build_charm(".")

    build_script = Path.cwd() / "build-charm-resources.sh"
    resources = await ops_test.build_resources(build_script)

    if resources:
        # created a dict from list of a filenames
        resources = {rsc.stem: rsc for rsc in resources}
    else:
        arch_resources = ops_test.arch_specific_resources(charm)
        resources = await ops_test.download_resources(
            charm, resources=arch_resources
        )
        
    assert resources, "Failed to build or download charm resources."
    
    log.info("Build Bundle...")
    bundle = ops_test.render_bundle(
        "tests/data/bundle.yaml", charm=charm, **resources
    )

    log.info("Deploy Bundle...")
    juju_cmd = ["deploy", "-m", ops_test.model_full_name, str(bundle)]
    rc, stdout, stderr = await ops_test.juju(*juju_cmd)
    assert rc == 0, f"Bundle deploy failed: {(stderr or stdout).strip()}"

    await ops_test.model.wait_for_idle()
    ...

Reference

More details can be found in the reference docs.

Release files for pytest-operator 0.43.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-operator 0.43.2
File Size Uploaded
pytest_operator-0.43.2.tar.gz 149.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-operator 0.43.2
File Interpreter ABI Platform
pytest_operator-0.43.2-py3-none-any.whl Python 3 none any Details

Total release size: 197.9 kB

Release files / pytest_operator-0.43.2.tar.gz

Download URL pytest_operator-0.43.2.tar.gz
Size 149.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3db34dcd9c114a2e41a9bc61da72daf1264e7644fd5b92e855f250cb337e01c3
BLAKE2b-256 checksum
How to use checksums
2e48880facbcaca080b852854f60fd46b37ca2985b27dc7f1713857d9de6d469
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pytest_operator-0.43.2-py3-none-any.whl

Download URL pytest_operator-0.43.2-py3-none-any.whl
Size 48.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d7d01ffe35d14b75577fd80a07c34f0a9f4835cfc6d373b8e2f995bcb4146bda
BLAKE2b-256 checksum
How to use checksums
96a022b018287f4ff7a36cdb79745c196f28897184d27e18b90177f5dfc2a0f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.43.2 This release

2 release files

0.42.0

2 release files

0.40.0

2 release files

0.37.0

2 release files

0.35.0

2 release files

0.34.0

2 release files

0.33.0

2 release files

0.32.0

2 release files

0.31.0

2 release files

0.30.0

2 release files

0.29.0

2 release files

0.28.0

2 release files

0.27.0

2 release files

0.26.0

2 release files

0.25.0

2 release files

0.24.0

2 release files

0.23.0

2 release files

0.22.0

2 release files

0.21.1

2 release files

0.21.0

2 release files

0.20.0

2 release files

0.19.0

2 release files

0.18.0

2 release files

0.17.0

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.11.0

1 release file

0.10.1

1 release file

0.10.0

1 release file

0.9.0

1 release file

0.8.4

1 release file

0.8.3

2 release files

0.8.2

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.0

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page