Add your description here
Project description
The Canonical pytest plugin for Jubilant
pytest-jubilant is a pytest plugin for Jubilant.
Jubilant is a Python library that wraps the Juju CLI, primarily for use in charm integration tests. pytest-jubilant provides additional pytest-specific functionality on top of Jubilant.
Read more: pytest-jubilant's design goals
Getting started
pytest-jubilant's features are available to use as long as it's installed in the Python environment where you're invoking pytest. The best way to ensure this is to add both pytest and pytest-jubilant to your dependencies like this.
# pyproject.toml
[dependency-groups]
integration = [
"pytest>=9,<10",
"pytest-jubilant>=2,<3",
]
And ensure that the integration dependency group is installed when running your integration tests, for example with:
uv run --group integration pytest tests/integration
Get started writing your own Jubilant integration tests with the how-to guide in the Ops docs.
Read on for an explanation of the fixtures, CLI options, and markers provided by pytest-jubilant.
Fixtures
pytest-jubilant's fixtures are available as long as pytest-jubilant is installed. You can request a fixture by declaring it as an argument for the test that needs it.
juju
This is a module-scoped fixture that creates a temporary Juju model and tears it down when the tests in the module have finished.
You can use combinations of the --juju-model, --no-juju-setup, and --no-juju-teardown options to reuse models across multiple integration test runs.
[!TIP] Use
jubilant.Jujuas the type annotation for thejujufixture in your tests for better linting and IDE autocompletions.
Usage:
# test_smoke.py
"""Test that the charm can be deployed and go to active status."""
import jubilant
def test_deploy(juju: jubilant.Juju):
juju.deploy("./foo.charm", "foo")
juju.wait(lambda status: jubilant.all_active(status, "foo"), timeout=1000)
This test will spin up a temporary model named jubilant-<randomhex>-test-smoke. It will be torn down when the module-scoped juju fixture context exits.
juju_factory
This is a module-scoped fixture that you can use to manage multiple temporary Juju models. It's what the juju fixture is using behind the scenes. It's useful if you have test cases that require multiple models, for example testing cross-model relations.
[!TIP] Use
pytest_jubilant.JujuFactoryas the type annotation for thejuju_factoryfixture in your tests for better linting and IDE autocompletions.Note that the exposed
JujuFactorytype is just a protocol, and can't be used to directly create a Juju factory. Whenever you need one, request thejuju_factoryfixture.
Usage:
# test_cmr.py
"""Test cross model relations."""
import jubilant
import pytest
import pytest_jubilant
@pytest.fixture(scope="module")
def istio(juju_factory: pytest_jubilant.JujuFactory):
yield juju_factory.get_juju(suffix="istio")
def test_offer_consume_relate(juju: jubilant.Juju, istio: jubilant.Juju):
istio.deploy("istio-k8s", "istio")
istio.wait(lambda status: all_active(status, "istio"), timeout=1000)
juju.deploy("./foo.charm", "foo")
juju.wait(lambda status: jubilant.all_active(status, "foo"), timeout=1000)
juju.cli("offer", "foo:bar")
istio.cli("consume", f"{juju.model}:foo")
istio.cli("relate", "istio", "foo:bar")
This test will spin up two temporary models, one called jubilant-<randomhex>-test-cmr, and one called jubilant-<randomhex>-test-cmr-istio. They'll be torn down when the module context exits.
pytest tests/integration --juju-model hello will use hello instead of jubilant-<randomhex>. The module names combined with the suffixes you defined in the fixtures will give all generated models predictable names. The tests will reuse the existing models (if found) or create new ones with those names.
CLI options
pytest-jubilant extends pytest with several commandline arguments that you can add directly to your pytest invocation.
--no-juju-setup
Skip all tests marked with juju_setup and don't create any new models. This option is for re-running a test on an existing model which is already set up. Since setup can be very lengthy, it's often helpful to avoid re-running it when iterating on tests that assume the charm is up and running, for example when testing actions.
[!WARNING] It's an error to pass
--no-juju-setupwithout also specifying--juju-model.
Usage:
pytest tests/integration --no-juju-teardown
# Check the last line of output for the <model prefix>!
pytest tests/integration --no-juju-setup --juju-model <model prefix>
--no-juju-teardown
Skip all tests marked with juju_teardown and skip destroying the models.
Useful to inspect the state of a model after a (failed) test run.
[!WARNING] The
--keep-modelsflag used bypytest-operatoris unsupported as ofpytest-jubilant2.0! Be sure to use--no-juju-teardowninstead.
Usage:
pytest tests/integration --no-juju-teardown
[!TIP] The last line of output will tell you the
--juju-modelvalue to use if you want to rerun your tests using the same models. Be sure to pass--no-juju-setupas well to avoid failures when trying to perform setup steps that are already done.
--juju-model
By default, created Juju model names are prefixed with jubilant-<randomhex>, where <randomhex> is randomly generated each pytest run. Set --juju-model on the commandline to use a fixed prefix instead.
[!WARNING] Note that models created with this prefix will be torn down at the end of the test run just like any other, so if you're targeting existing models you care about, don't forget the
--no-juju-teardownflag!
Usage:
pytest tests/integration/test_foo.py --juju-model hello
# Runs the test on new 'hello-test-foo' model and tears it down afterwards.
pytest tests/integration/test_foo.py --juju-model hello --no-juju-teardown
# Runs the test on new 'hello-test-foo' model and keeps it.
pytest tests/integration/test_foo.py --juju-model hello --no-juju-setup --no-juju-teardown
# Runs the test on the existing 'hello-test-foo' model and keeps it.
# Note that we don't want to run the setup tests since they already ran.
juju add-model hello-test-bar # A whole new model.
pytest tests/integration/test_bar.py --juju-model hello --no-juju-teardown
# Runs the test on the existing 'hello-test-bar' model and keeps it.
# Note that we want to run the setup tests to deploy the charm(s) etc.
# since this is a new model.
--juju-switch
Switch to the model that is currently in scope, so you can keep an eye on the juju status as the tests progress. This won't be very helpful if you're running multiple test modules in parallel!
Only switches to models created by the juju fixture, not those created by juju_factory.
Usage:
pytest tests/integration -k test_something --juju-switch
# will switch you to the 'jubilant-<randomhex>-<module>' model as soon as it's created
pytest tests/integration -k test_something --juju-model hello --juju-switch
# will switch you to the 'hello-<module>' model as soon as it's created
--juju-dump-logs
When all the tests in a module have completed, but prior to tearing down the models owned by a juju_factory, dump the juju debug-log for each managed model into the specified directory.
- By default, logs aren't dumped, and
juju debug-logis only executed if tests failed, so that the last log lines can be sent to stderr. - If
--juju-dump-logsis passed, logs are dumped to<CWD>/.logs/. - If
--juju-dump-logs <target dir>is passed, logs are dumped to<target dir>/.
The file naming scheme is:
<module prefix>-<module name>[-<suffix>]-juju-debug.log
Usage:
pytest tests/integration/test_ingress.py --juju-dump-logs=debug_logs
# Once the tests are done, you'll find the logs in:
# ./debug_logs/jubilant-abcd1234-test-ingress-juju-debug.log
pytest tests/integration/test_ingress.py --juju-model foo --juju-dump-logs
# Once the tests are done, you'll find the logs in the default directory:
# ./.logs/foo-test-ingress-juju-debug.log
pytest integration/test_ingress.py
# No logs will be saved.
[!TIP] Use
--juju-dump-logsin combination with actions/upload-artifact to make your logs available in CI.For example:
# In your integration test job - run: tox -e integration -- --juju-dump-logs - name: Upload logs if: ${{ !cancelled() }} uses: actions/upload-artifact@v4 with: name: juju-dump-logs path: .logs
Markers
pytest-jubilant declares markers that you can apply to your tests with @pytest.mark.<marker>.
juju_setup
Marker for tests that prepare a model for use in later tests.
The --no-juju-setup option will skip any tests marked with juju_setup, in addition to not destroying the Juju models themselves.
[!TIP] To run only your
juju_setuptests and leave the models set up for manual interaction, try:pytest tests/integration -m juju_setup --no-juju-teardown
Usage:
import jubilant
import pytest
@pytest.mark.juju_setup
def test_deploy(juju: jubilant.Juju):
juju.deploy("A")
juju.deploy("B")
@pytest.mark.juju_setup
def test_relate(juju: jubilant.Juju):
juju.integrate("A", "B")
juju_teardown
Marker for tests that perform destructive actions on a model.
The --no-juju-teardown option will skip any tests marked with juju_teardown, in addition to not destroying the Juju models themselves.
Usage:
import jubilant
import pytest
@pytest.mark.juju_teardown
def test_disintegrate(juju: jubilant.Juju):
juju.remove_relation("A", "B")
@pytest.mark.juju_teardown
def test_destroy(juju: jubilant.Juju):
juju.remove_application("A")
juju.remove_application("B")
Project and community
pytest-jubilant is an open source project that warmly welcomes community contributions, suggestions, fixes and constructive feedback.
For support, join Charm Development on Matrix.
To follow along with updates and tips about charm development, join our Discourse forum.
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_jubilant-2.0.1.tar.gz.
File metadata
- Download URL: pytest_jubilant-2.0.1.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
960bb63d216ec746f7644b67c276b059fccdaa258ee7644aaa74058db659edac
|
|
| MD5 |
a926e9902f420079e6551abd128b8ee9
|
|
| BLAKE2b-256 |
3d64e83181af1004f64a3ce11c7b9770c3152832e02d00e9e7b4f01877325c5f
|
Provenance
The following attestation bundles were made for pytest_jubilant-2.0.1.tar.gz:
Publisher:
build-and-publish.yaml on canonical/pytest-jubilant
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_jubilant-2.0.1.tar.gz -
Subject digest:
960bb63d216ec746f7644b67c276b059fccdaa258ee7644aaa74058db659edac - Sigstore transparency entry: 1245492383
- Sigstore integration time:
-
Permalink:
canonical/pytest-jubilant@c16550ea5ecefcac77840b659bc037c8095dd1b9 -
Branch / Tag:
refs/tags/v2.0.1 - Owner: https://github.com/canonical
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-publish.yaml@c16550ea5ecefcac77840b659bc037c8095dd1b9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pytest_jubilant-2.0.1-py3-none-any.whl.
File metadata
- Download URL: pytest_jubilant-2.0.1-py3-none-any.whl
- Upload date:
- Size: 13.5 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 |
df498ee79d6db5ec32baa35a121ab75852dd50f61504a12550ba81cd63a7f1ef
|
|
| MD5 |
2c36def26bd776d2bf8efbd49bbb2bdb
|
|
| BLAKE2b-256 |
e43c5c0bc4f54f6905aa7515cf764c3f5d6c85711cdc1a928221f711d5b7532c
|
Provenance
The following attestation bundles were made for pytest_jubilant-2.0.1-py3-none-any.whl:
Publisher:
build-and-publish.yaml on canonical/pytest-jubilant
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_jubilant-2.0.1-py3-none-any.whl -
Subject digest:
df498ee79d6db5ec32baa35a121ab75852dd50f61504a12550ba81cd63a7f1ef - Sigstore transparency entry: 1245492417
- Sigstore integration time:
-
Permalink:
canonical/pytest-jubilant@c16550ea5ecefcac77840b659bc037c8095dd1b9 -
Branch / Tag:
refs/tags/v2.0.1 - Owner: https://github.com/canonical
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-publish.yaml@c16550ea5ecefcac77840b659bc037c8095dd1b9 -
Trigger Event:
push
-
Statement type: