To ease some of the difficulties of creating unit tests for existing charms based on charms.reactive, such as layers and other dependencies not being available, I created charms.unit_test (repo).
It does require pytest
, but by adding a single call to charms.unit_test.patch_reactive()
in your conftest.py
file, your unit tests can import and call your charm or layer’s handlers or library code, and any imports of charms.reactive
, charmhelpers
, or layer code (assuming it follows the lib/charms/layer/
convention) will be patched out with mocks. Those mocks also provide stubs for the flag functions to handle flags in memory so that they can be set and queried as expected. It also ensures that the decorators don’t get in the way of your handlers so that you can still easily access them to test (though it doesn’t provide any mechanism for testing the calling conditions / validating the logic implied by those decorators).
It’s certainly not a fully-fledged testing harness, but it can at least make it a bit easier to add some basic unit tests to charms and layers. Contributions certainly welcome, but obviously I’d prefer to see effort go into moving charms to the new framework with the much more fully featured testing harness instead.