26bb114ccb
* Move 2.10.0rc1 release date a few days forward. (#71270) At yesterday's meeting it was decided to have ansible-2.10.0 depend on ansible-base-2.10.1 so that we can get several fixes for ansible-base's routing (including adding the gluster.gluster collection). ansible-base-2.10.1 will release on September 8th. So we will plan on releasing ansible-2.10.0rc1 on the 10th. https://meetbot.fedoraproject.org/ansible-community/2020-08-12/ansible_community_meeting.2020-08-12-18.00.html (cherry picked from commite507c127e5
) * a few writing style updates (#71212) (cherry picked from commit4f0bd5de38
) * Fix code markups and add link to CVE (#71082) (cherry picked from commit92d59a58c0
) * Fix 404 links (#71256) Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commitecea018506
) * Writing style updates to Developing dynamic inventory topic (#71245) * modified the writing style * incorporated peer feedback (cherry picked from commitecd3b52ad7
) * Fix roadmap formatting. (#71275) (cherry picked from commitee48e0b0ad
) * Update password.py (#71295) List md5_crypt, bcrypt, sha256_crypt, sha512_crypt as hash schemes in the password plugin. (cherry picked from commit1d1de2c6fd
) * Update ansible european IRC channel (#71326) Signed-off-by: Rémi VERCHERE <remi@verchere.fr> (cherry picked from commit824cd4cbeb
) * Add warning about copyright year change (#71251) To simplify project administration and avoid any legal issues, add a warning in the docs. This reflects - https://github.com/ansible/ansible/issues/45989#issuecomment-423635622 and fixes: #45989 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit606604bb97
) * subelements: Clarify parameter docs (#71177) skip_missing parameter in subelements lookup plugin is accepted from inside the dictionary. Fixes: #38182 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit6d17736ef4
) * Writing style updates to Using Variables topic (#71194) * updated topic title, underline length for headings, and incorporated peer feedback (cherry picked from commit4d68efbe24
) * cron module defaults to current user, not root (#71337) (cherry picked from commit4792d83e13
) * Update Network Getting Started for FQCN/collection world (#71188) * pull out network roles, cleanup, update first playbook examples, update gather facts section, some inventory conversion to .yml, update inventory and roles, simplify the navigation titles, fix tocs, feedback comments (cherry picked from commitf79a7c5585
) * Add documentation about info/facts module development (#71250) Fixes: #40151 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit4f993922c8
) * network: Correct documentation (#71246) ini-style inventory does not support Ansible Vault password. This fixes network_best_practices_2.5 doc. Fixes: #69039 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commita1257d75aa
) * tidies up vars page (#71339) (cherry picked from commit02ea80f6d7
) * base.yml: Fix typos (#71346) (cherry picked from commit41d7d53573
) * quick fix to change main back to devel (#71342) * quick fix to change main back to devel * Update docs/docsite/rst/dev_guide/developing_collections.rst Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit74f88c56a5
) * Add note about integration tests for new modules to the dev guide (#71345) (cherry picked from commitb82889eef5
) * update fest link (#71376) (cherry picked from commit80b8fde946
) * incorporate minimalism feedback on debugging page (#71272) Co-authored-by: bobjohnsrh <50667510+bobjohnsrh@users.noreply.github.com> (cherry picked from commit5073cfc8bc
) * fix header problem Co-authored-by: Toshio Kuratomi <a.badger@gmail.com> Co-authored-by: Sayee <57951841+sayee-jadhav@users.noreply.github.com> Co-authored-by: Baptiste Mille-Mathias <baptiste.millemathias@gmail.com> Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com> Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: rovshango <rovshan.go@gmail.com> Co-authored-by: Remi Verchere <rverchere@users.noreply.github.com> Co-authored-by: Jake Howard <RealOrangeOne@users.noreply.github.com> Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com> Co-authored-by: Per Lundberg <perlun@gmail.com> Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>
213 lines
7.2 KiB
ReStructuredText
213 lines
7.2 KiB
ReStructuredText
:orphan:
|
|
|
|
.. _testing_units:
|
|
|
|
**********
|
|
Unit Tests
|
|
**********
|
|
|
|
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.
|
|
|
|
.. contents:: Topics
|
|
|
|
Available Tests
|
|
===============
|
|
|
|
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/``.
|
|
|
|
Running Tests
|
|
=============
|
|
|
|
.. note::
|
|
To run unit tests using docker, always use the default docker image
|
|
by passing the ``--docker`` or ``--docker default`` argument.
|
|
|
|
The Ansible unit tests can be run across the whole code base by doing:
|
|
|
|
.. code:: shell
|
|
|
|
cd /path/to/ansible/source
|
|
source hacking/env-setup
|
|
ansible-test units --docker -v
|
|
|
|
Against a single file by doing:
|
|
|
|
.. code:: shell
|
|
|
|
ansible-test units --docker -v apt
|
|
|
|
Or against a specific Python version by doing:
|
|
|
|
.. code:: shell
|
|
|
|
ansible-test units --docker -v --python 2.7 apt
|
|
|
|
If you are running unit tests against things other than modules, such as module utilities, specify the whole file path:
|
|
|
|
.. code:: shell
|
|
|
|
ansible-test units --docker -v test/units/module_utils/basic/test_imports.py
|
|
|
|
For advanced usage see the online help::
|
|
|
|
ansible-test units --help
|
|
|
|
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.
|
|
|
|
|
|
Installing dependencies
|
|
=======================
|
|
|
|
If you are running ``ansible-test`` with the ``--docker`` or ``--venv`` option you do not need to install dependencies manually.
|
|
|
|
Otherwise you can install dependencies using the ``--requirements`` option, which will
|
|
install all the required dependencies needed for unit tests. For example:
|
|
|
|
.. code:: shell
|
|
|
|
ansible-test units --python 2.7 --requirements apache2_module
|
|
|
|
|
|
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>`_.
|
|
|
|
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>`_.
|
|
|
|
See also the `constraints
|
|
<https://github.com/ansible/ansible/blob/devel/test/lib/ansible_test/_data/requirements/constraints.txt>`_
|
|
applicable to all test commands.
|
|
|
|
|
|
Extending unit tests
|
|
====================
|
|
|
|
|
|
.. warning:: What a unit test isn't
|
|
|
|
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
|
|
name like ``test_<something>.py`` or as classes.
|
|
|
|
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
|
|
|
|
Here is an example of a class::
|
|
|
|
import unittest
|
|
|
|
class AddTester(unittest.TestCase)
|
|
|
|
def SetUp()
|
|
self.a = 10
|
|
self.b = 23
|
|
|
|
# this function will
|
|
def test_add()
|
|
c = 33
|
|
assert self.a + self.b = c
|
|
|
|
# this function will
|
|
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
|
|
to use that.
|
|
|
|
Assertions using the simple ``assert`` function inside the tests will give full
|
|
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.
|
|
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.
|
|
|
|
|
|
Fixtures files
|
|
``````````````
|
|
|
|
To mock out fetching results from devices, or provide other complex data structures that
|
|
come from external libraries, you can use ``fixtures`` to read in pre-generated data.
|
|
|
|
You can check how `fixtures <https://github.com/ansible/ansible/tree/devel/test/units/module_utils/facts/fixtures/cpuinfo>`_
|
|
are used in `cpuinfo fact tests <https://github.com/ansible/ansible/blob/9f72ff80e3fe173baac83d74748ad87cb6e20e64/test/units/module_utils/facts/hardware/linux_data.py#L384>`_
|
|
|
|
If you are simulating APIs you may find that Python placebo is useful. See
|
|
:ref:`testing_units_modules` for more information.
|
|
|
|
|
|
Code Coverage For New or Updated Unit Tests
|
|
```````````````````````````````````````````
|
|
New code will be missing from the codecov.io coverage reports (see :ref:`developing_testing`), so
|
|
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.
|
|
|
|
To collect coverage data add the ``--coverage`` argument to your ``ansible-test`` command line:
|
|
|
|
.. code:: shell
|
|
|
|
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.
|
|
|
|
To clear data between test runs, use the ``ansible-test coverage erase`` command. See
|
|
:ref:`testing_running_locally` for more information about generating coverage
|
|
reports.
|
|
|
|
|
|
.. seealso::
|
|
|
|
:ref:`testing_units_modules`
|
|
Special considerations for unit testing modules
|
|
:ref:`testing_running_locally`
|
|
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>`_
|
|
The documentation of the unittest framework in python 3
|
|
`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
|