Testing#
Tox#
We use tox as our test runner. We have three different test suites configured in tox:
test
Unit tests using pytest.
style
Style checks using pre-commit and friends (black, flake8, etc).
type
Type checks using mypy.
You can run the full suite of tests like so:
tox
Or target specific test suites with -e
:
tox -e style
Style checks#
All style checks are implemented through pre-commit. It is recommended to setup pre-commit locally so that style checks run automatically when making a commit, this can be done like so:
pre-commit install
Type checks#
Type checks are implemented with mypy and the pydantic mypy plugin.
Unit tests#
Unit tests are located in tests/
. Overall the directory structure mimics whatβs in src/certwrangler/
:
tests/
βββ files/
βΒ Β βββ certwrangler_config_dummy.yaml
βββ solvers/
βΒ Β βββ __init__.py
βΒ Β βββ test_dummy.py
βΒ Β βββ test_edgedns.py
βββ state_managers/
βΒ Β βββ __init__.py
βΒ Β βββ test_dummy.py
βΒ Β βββ test_local.py
βββ stores/
βΒ Β βββ __init__.py
βΒ Β βββ test_dummy.py
βΒ Β βββ test_local.py
βΒ Β βββ test_vault.py
βββ conftest.py
βββ __init__.py
βββ test_controllers.py
βββ test_daemon.py
βββ test_dns.py
βββ test_http.py
βββ test_metrics.py
βββ test_models.py
βββ test_reconcilers.py
βββ test_shell.py
When running through tox with tox -e test
youβll also get a coverage report at the end of the test run. This is useful when writing tests to get the line numbers of things you mightβve missed creating tests for. While 100% coverage is nice, it is sometimes not practical and not a hard requirement.
Common fixtures#
Fixtures are stored in tests/conftest.py
. A few of the more common ones are documented here.
click_ctx#
Creates a fake click context for tests. This is needed for any tests against code that uses the click context, directly or indirectly. If you see this message when running your test:
RuntimeError: There is no active click context.
then you just need to include this fixture to make it magically work.
By default this will set the config path on the CertwranglerState object to the dummy_config_path fixture. This can be changed by doing the following within your test:
click_ctx.obj.config_path = new_config_path
where new_config_path is a pathlib.Path object. Recommended to follow the pattern of dummy_config_path and setup a fixture to return that object.
config#
This returns an uninitialized Config object loaded from the dummy config in tests/files/certwrangler_config_dummy.yaml
. There are a few additional convenance fixtures that pluck objects off of this generated config tree, like account
, cert
, subject
, solver
, state_manager
, and store
.