Testing

The Charmed Operator Framework provides a testing harness, so you can check your charm does the right thing in different scenarios without having to create a full deployment. When you run charmcraft init, the template charm it creates includes some sample tests, along with a means to run them (see the run_tests shell script) and provide a short report of unit test coverage.

Testing basics

Here’s a minimal example, taken from the charmcraft init template with some additional comments:

import unittest
# Import the Charmed Operator Framework testing harness
from ops.testing import Harness
# Import your charm class
from charm import TestCharmCharm

class TestCharm(unittest.TestCase):
    def test_config_changed(self):
        # Instantiate the Charmed Operator Framework test harness
        harness = Harness(TestCharmCharm)
        # Set a name for the testing model created by Harness (optional).
        # Cannot be called after harness.begin()
        harness.set_model_name("testing")
        # Instruct unittest to use Harness' cleanup method on tear down
        self.addCleanup(harness.cleanup)
        # Instantiate an instance of the charm (harness.charm)
        harness.begin()
        # Test initialisation of shared state in the charm
        self.assertEqual(list(harness.charm._stored.things), [])
        # Simulates the update of config, triggers a config-changed event
        harness.update_config({"thing": "foo"})
        # Test the config-changed method stored the update in state
        self.assertEqual(list(harness.charm._stored.things), ["foo"])

We use Python’s standard unit testing framework, augmenting it with Harness, the Charmed Operator Framework’s testing harness. Harness provides some convenient mechanisms for mocking charm events and processes.

In some cases it may be useful to start the test harness and fire the same hooks that Juju would fire on deployment. This can be achieved using the begin_with_initial_hooks() method , to be used in place of the begin() method. This method will trigger the events: install -> relation-created -> config-changed -> start -> relation-joined depending on whether any relations have been created prior calling begin_with_initial_hooks(). An example of this is shown in the testing relations section.

Using the harness variable, we can simulate various events in the charm’s lifecycle:

# Update the harness to set the active unit as a "leader" (the default value is False).
# This will trigger a leader-elected event
harness.set_leader(True)
# Update config.
harness.update_config({"foo": "bar", "baz": "qux"})
# Disable hooks if we're about to do something that would otherwise cause a hook
# to fire such as changing configuration or setting a leader, but you don't want
# those hooks to fire.
harness.disable_hooks()
# Update config
harness.update_config({"foo": "quux"})
# Re-enable hooks
harness.enable_hooks()
# Set the status of the active unit. We'd need "from ops.model import BlockedStatus".
harness.charm.unit.status = BlockedStatus("Testing")

Any of your charm’s properties and methods (including event callbacks) can be accessed using harness.charm.

Testing log output

Charm authors can also test for desired log output. Should a charm author create log messages in the standard form:

# ...
logger = logging.getLogger(__name__)


class SomeCharm(CharmBase):
# ...
    def _some_method(self):
        logger.info("some message")
# ...

The above logging output could be tested like so:

with self.assertLogs(level="INFO") as logger:
    harness.charm._some_method()
    self.assertEqual(sorted(logger.output), ["some message"])

Last updated 21 days ago.