cloud-init has both unit tests and integration tests. Unit tests can
be found in-tree alongside the source code, as well as
tests/unittests. Integration tests can be found at
tests/integration_tests. Documentation specifically for integration
tests can be found on the Integration Testing page, but
the guidelines specified below apply to both types of tests.
cloud-init uses pytest to run its tests, and has tests written both
unittest.TestCase sub-classes and as un-subclassed pytest tests.
The following guidelines should be followed.
- For ease of organisation and greater accessibility for developers not
familiar with pytest, all cloud-init unit tests must be contained
within test classes
- Put another way, module-level test functions should not be used
- As all tests are contained within classes, it is acceptable to mix
TestCasetest classes and pytest test classes within the same test file
- pytest test classes should use pytest fixtures to share functionality instead of inheritance
- pytest tests should use bare
assertstatements, to take advantage of pytest’s assertion introspection
==and other commutative assertions, the expected value should be placed before the value under test:
assert expected_value == function_under_test()
pytest Version Gotchas¶
As we still support Ubuntu 16.04 (Xenial Xerus), we can only use pytest features that are available in v2.8.7. This is an inexhaustive list of ways in which this may catch you out:
- Support for using
pytest.fixturefunctions was only introduced in pytest 3.0. Such functions must instead use the
- Only the following built-in fixtures are available :
- On xenial, the objects returned by the
tmpdirfixture cannot be used where paths are required; they are rejected as invalid paths. You must instead use their
- For example, instead of
util.write_file(tmpdir.join("some_file"), ...), you should write
- For example, instead of
- The pytest.param function cannot be used. It was introduced in pytest 3.1, which means it is not available on xenial. The more limited mechanism it replaced was removed in pytest 4.0, so is not available in focal or later. The only available alternatives are to write mark-requiring test instances as completely separate tests, without utilising parameterisation, or to apply the mark to the entire parameterized test (and therefore every test instance).
Mocking and Assertions¶
Variables/parameter names for
MagicMockinstances should start with
m_to clearly distinguish them from non-mock variables
- For example,
m_readurl(which would be a mock for
- For example,
assert_*methods that are available on
MagicMockobjects should be avoided, as typos in these method names may not raise
AttributeError(and so can cause tests to silently pass). An important exception: if a
Mockis autospecced then misspelled assertion methods will raise an
AttributeError, so these assertion methods may be used on autospecced
Mocks, these substitutions can be used (
mis assumed to be a
assert mock.call(*args, **kwargs) in m.call_args_list
assert 0 != m.call_count
assert 1 == m.call_count
assert [mock.call(*args, **kwargs)] == m.call_args_list
assert mock.call(*args, **kwargs) == m.call_args_list[-1]
for call in call_list: assert call in m.call_args_list
m.assert_has_calls(..., any_order=False)are not easily replicated in a single statement, so their use when appropriate is acceptable.
assert 0 == m.call_count
When there are multiple patch calls in a test file for the module it is testing, it may be desirable to capture the shared string prefix for these patch calls in a module-level variable. If used, such variables should be named
M_PATHor, for datasource tests,
Test Argument Ordering¶
- Test arguments should be ordered as follows:
mock.patcharguments. When used as a decorator,
mock.patchpartially applies its generated
Mockobject as the first argument, so these arguments must go first.
pytest.mark.parametrizearguments, in the order specified to the
parametrizedecorator. These arguments are also provided by a decorator, so it’s natural that they sit next to the
- Fixture arguments, alphabetically. These are not provided by a decorator, so they are last, and their order has no defined meaning, so we default to alphabetical.
- It follows from this ordering of test arguments (so that we retain
the property that arguments left-to-right correspond to decorators
bottom-to-top) that test decorators should be ordered as follows:
This list of fixtures (with markup) can be reproduced by running:
py.test-3 --fixtures -q | grep "^[^ -]" | grep -v '\(no\|capturelog\)' | sort | sed 's/.*/* ``\0``/'
in a xenial lxd container with python3-pytest-catchlog installed.