2018-09-11 18:51:47 +02:00
|
|
|
:orphan:
|
|
|
|
|
2018-09-07 15:57:36 +02:00
|
|
|
.. _testing_units:
|
|
|
|
|
2017-04-28 10:08:26 +02:00
|
|
|
**********
|
|
|
|
Unit Tests
|
|
|
|
**********
|
|
|
|
|
2017-10-19 22:36:57 +02:00
|
|
|
Unit tests are small isolated tests that target a specific library or module. Unit tests
|
|
|
|
in Ansible are currently the only way of driving tests from python within Ansible's
|
|
|
|
continuous integration process. This means that in some circumstances the tests may be a
|
|
|
|
bit wider than just units.
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
.. contents:: Topics
|
|
|
|
|
|
|
|
Available Tests
|
|
|
|
===============
|
|
|
|
|
2017-10-19 22:36:57 +02:00
|
|
|
Unit tests can be found in `test/units
|
|
|
|
<https://github.com/ansible/ansible/tree/devel/test/units>`_. Notice that the directory
|
|
|
|
structure of the tests matches that of ``lib/ansible/``.
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
Running Tests
|
|
|
|
=============
|
|
|
|
|
2017-10-19 22:36:57 +02:00
|
|
|
The Ansible unit tests can be run across the whole code base by doing:
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
.. code:: shell
|
|
|
|
|
|
|
|
cd /path/to/ansible/source
|
|
|
|
source hacking/env-setup
|
2019-09-26 20:28:06 +02:00
|
|
|
ansible-test units --docker -v
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
Against a single file by doing:
|
|
|
|
|
|
|
|
.. code:: shell
|
|
|
|
|
2019-09-26 20:28:06 +02:00
|
|
|
ansible-test units --docker -v apt
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
Or against a specific Python version by doing:
|
|
|
|
|
|
|
|
.. code:: shell
|
|
|
|
|
2019-09-26 20:28:06 +02:00
|
|
|
ansible-test units --docker -v --python 2.7 apt
|
2017-04-28 10:08:26 +02:00
|
|
|
|
2019-04-15 23:20:14 +02:00
|
|
|
If you are running unit tests against things other than modules, such as module utilities, specify the whole file path:
|
|
|
|
|
|
|
|
.. code:: shell
|
|
|
|
|
2019-09-26 20:28:06 +02:00
|
|
|
ansible-test units --docker -v test/units/module_utils/basic/test_imports.py
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
For advanced usage see the online help::
|
|
|
|
|
|
|
|
ansible-test units --help
|
|
|
|
|
2017-10-19 22:36:57 +02:00
|
|
|
You can also run tests in Ansible's continuous integration system by opening a pull
|
|
|
|
request. This will automatically determine which tests to run based on the changes made
|
|
|
|
in your pull request.
|
|
|
|
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
Installing dependencies
|
|
|
|
=======================
|
|
|
|
|
2019-09-26 20:28:06 +02:00
|
|
|
If you are running ``ansible-test`` with the ``--docker`` or ``--venv`` option you do not need to install dependencies manually.
|
2017-04-28 10:08:26 +02:00
|
|
|
|
2019-09-26 20:28:06 +02:00
|
|
|
Otherwise you can install dependencies using the ``--requirements`` option, which will
|
2017-10-19 22:36:57 +02:00
|
|
|
install all the required dependencies needed for unit tests. For example:
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
.. code:: shell
|
|
|
|
|
2019-09-26 20:28:06 +02:00
|
|
|
ansible-test units --python 2.7 --requirements apache2_module
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
|
2019-09-26 20:28:06 +02:00
|
|
|
The list of unit test requirements can be found at `test/units/requirements.txt
|
|
|
|
<https://github.com/ansible/ansible/tree/devel/test/units/requirements.txt>`_.
|
2017-04-28 10:08:26 +02:00
|
|
|
|
2019-09-26 20:28:06 +02:00
|
|
|
This does not include the list of unit test requirements for ``ansible-test`` itself,
|
|
|
|
which can be found at `test/lib/ansible_test/_data/requirements/units.txt
|
|
|
|
<https://github.com/ansible/ansible/tree/devel/test/lib/ansible_test/_data/requirements/units.txt>`_.
|
2017-04-28 10:08:26 +02:00
|
|
|
|
2019-09-26 20:28:06 +02:00
|
|
|
See also the `constraints
|
2019-10-09 21:36:43 +02:00
|
|
|
<https://github.com/ansible/ansible/blob/devel/test/lib/ansible_test/_data/requirements/constraints.txt>`_
|
2019-09-26 20:28:06 +02:00
|
|
|
applicable to all test commands.
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
Extending unit tests
|
|
|
|
====================
|
|
|
|
|
|
|
|
|
|
|
|
.. warning:: What a unit test isn't
|
|
|
|
|
2017-10-19 22:36:57 +02:00
|
|
|
If you start writing a test that requires external services then
|
|
|
|
you may be writing an integration test, rather than a unit test.
|
|
|
|
|
|
|
|
|
|
|
|
Structuring Unit Tests
|
|
|
|
``````````````````````
|
|
|
|
|
|
|
|
Ansible drives unit tests through `pytest <https://docs.pytest.org/en/latest/>`_. This
|
|
|
|
means that tests can either be written a simple functions which are included in any file
|
2018-09-11 18:51:47 +02:00
|
|
|
name like ``test_<something>.py`` or as classes.
|
2017-10-19 22:36:57 +02:00
|
|
|
|
|
|
|
Here is an example of a function::
|
|
|
|
|
|
|
|
#this function will be called simply because it is called test_*()
|
|
|
|
|
|
|
|
def test_add()
|
|
|
|
a = 10
|
|
|
|
b = 23
|
|
|
|
c = 33
|
|
|
|
assert a + b = c
|
2018-09-11 18:51:47 +02:00
|
|
|
|
2017-10-19 22:36:57 +02:00
|
|
|
Here is an example of a class::
|
|
|
|
|
2018-07-06 14:07:57 +02:00
|
|
|
import unittest
|
2018-09-11 18:51:47 +02:00
|
|
|
|
2017-10-19 22:36:57 +02:00
|
|
|
class AddTester(unittest.TestCase)
|
2018-09-11 18:51:47 +02:00
|
|
|
|
2017-10-19 22:36:57 +02:00
|
|
|
def SetUp()
|
|
|
|
self.a = 10
|
|
|
|
self.b = 23
|
2018-09-11 18:51:47 +02:00
|
|
|
|
|
|
|
# this function will
|
2017-10-19 22:36:57 +02:00
|
|
|
def test_add()
|
|
|
|
c = 33
|
|
|
|
assert self.a + self.b = c
|
|
|
|
|
2018-09-11 18:51:47 +02:00
|
|
|
# this function will
|
2017-10-19 22:36:57 +02:00
|
|
|
def test_subtract()
|
|
|
|
c = -13
|
|
|
|
assert self.a - self.b = c
|
|
|
|
|
|
|
|
Both methods work fine in most circumstances; the function-based interface is simpler and
|
|
|
|
quicker and so that's probably where you should start when you are just trying to add a
|
|
|
|
few basic tests for a module. The class-based test allows more tidy set up and tear down
|
|
|
|
of pre-requisites, so if you have many test cases for your module you may want to refactor
|
2018-09-11 18:51:47 +02:00
|
|
|
to use that.
|
2017-10-19 22:36:57 +02:00
|
|
|
|
2018-07-06 14:07:57 +02:00
|
|
|
Assertions using the simple ``assert`` function inside the tests will give full
|
2017-10-19 22:36:57 +02:00
|
|
|
information on the cause of the failure with a trace-back of functions called during the
|
|
|
|
assertion. This means that plain asserts are recommended over other external assertion
|
|
|
|
libraries.
|
|
|
|
|
|
|
|
A number of the unit test suites include functions that are shared between several
|
|
|
|
modules, especially in the networking arena. In these cases a file is created in the same
|
|
|
|
directory, which is then included directly.
|
|
|
|
|
|
|
|
|
|
|
|
Module test case common code
|
|
|
|
````````````````````````````
|
|
|
|
|
|
|
|
Keep common code as specific as possible within the `test/units/` directory structure. For
|
|
|
|
example, if it's specific to testing Amazon modules, it should be in
|
|
|
|
`test/units/modules/cloud/amazon/`. Don't import common unit test code from directories
|
|
|
|
outside the current or parent directories.
|
|
|
|
|
|
|
|
Don't import other unit tests from a unit test. Any common code should be in dedicated
|
|
|
|
files that aren't themselves tests.
|
|
|
|
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
Fixtures files
|
|
|
|
``````````````
|
|
|
|
|
2020-02-21 11:56:12 +01:00
|
|
|
To mock out fetching results from devices, or provide other complex data structures that
|
2017-10-19 22:36:57 +02:00
|
|
|
come from external libraries, you can use ``fixtures`` to read in pre-generated data.
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
Text files live in ``test/units/modules/network/PLATFORM/fixtures/``
|
|
|
|
|
|
|
|
Data is loaded using the ``load_fixture`` method
|
|
|
|
|
2017-10-19 22:36:57 +02:00
|
|
|
See `eos_banner test
|
|
|
|
<https://github.com/ansible/ansible/blob/devel/test/units/modules/network/eos/test_eos_banner.py>`_
|
|
|
|
for a practical example.
|
|
|
|
|
|
|
|
If you are simulating APIs you may find that python placebo is useful. See
|
2018-04-25 20:18:52 +02:00
|
|
|
:ref:`testing_units_modules` for more information.
|
2017-10-19 22:36:57 +02:00
|
|
|
|
2017-04-28 10:08:26 +02:00
|
|
|
|
2017-10-19 22:36:57 +02:00
|
|
|
Code Coverage For New or Updated Unit Tests
|
|
|
|
```````````````````````````````````````````
|
2019-06-26 20:59:33 +02:00
|
|
|
New code will be missing from the codecov.io coverage reports (see :ref:`developing_testing`), so
|
2017-10-19 22:36:57 +02:00
|
|
|
local reporting is needed. Most ``ansible-test`` commands allow you to collect code
|
|
|
|
coverage; this is particularly useful when to indicate where to extend testing.
|
2017-04-28 10:08:26 +02:00
|
|
|
|
|
|
|
To collect coverage data add the ``--coverage`` argument to your ``ansible-test`` command line:
|
|
|
|
|
|
|
|
.. code:: shell
|
|
|
|
|
2017-04-28 12:58:38 +02:00
|
|
|
ansible-test units --coverage apt
|
|
|
|
ansible-test coverage html
|
|
|
|
|
|
|
|
Results will be written to ``test/results/reports/coverage/index.html``
|
|
|
|
|
|
|
|
Reports can be generated in several different formats:
|
|
|
|
|
|
|
|
* ``ansible-test coverage report`` - Console report.
|
|
|
|
* ``ansible-test coverage html`` - HTML report.
|
|
|
|
* ``ansible-test coverage xml`` - XML report.
|
|
|
|
|
2017-10-19 22:36:57 +02:00
|
|
|
To clear data between test runs, use the ``ansible-test coverage erase`` command. See
|
2018-04-25 20:18:52 +02:00
|
|
|
:ref:`testing_running_locally` for more information about generating coverage
|
2017-10-19 22:36:57 +02:00
|
|
|
reports.
|
|
|
|
|
|
|
|
|
|
|
|
.. seealso::
|
2017-04-28 12:58:38 +02:00
|
|
|
|
2018-04-25 20:18:52 +02:00
|
|
|
:ref:`testing_units_modules`
|
2017-10-19 22:36:57 +02:00
|
|
|
Special considerations for unit testing modules
|
2019-06-26 20:59:33 +02:00
|
|
|
:ref:`testing_running_locally`
|
2017-10-19 22:36:57 +02:00
|
|
|
Running tests locally including gathering and reporting coverage data
|
|
|
|
`Python 3 documentation - 26.4. unittest — Unit testing framework <https://docs.python.org/3/library/unittest.html>`_
|
2018-09-11 18:51:47 +02:00
|
|
|
The documentation of the unittest framework in python 3
|
2017-10-19 22:36:57 +02:00
|
|
|
`Python 2 documentation - 25.3. unittest — Unit testing framework <https://docs.python.org/3/library/unittest.html>`_
|
|
|
|
The documentation of the earliest supported unittest framework - from Python 2.6
|
|
|
|
`pytest: helps you write better programs <https://docs.pytest.org/en/latest/>`_
|
|
|
|
The documentation of pytest - the framework actually used to run Ansible unit tests
|