2018-09-11 18:51:47 +02:00
:orphan:
2018-04-25 20:18:52 +02:00
.. _testing_running_locally:
2017-04-28 10:08:26 +02:00
***** ***** *****
2017-04-14 02:46:21 +02:00
Testing Ansible
2017-04-28 10:08:26 +02:00
***** ***** *****
2017-04-14 02:46:21 +02:00
2017-04-28 12:58:38 +02:00
This document describes how to:
2017-04-28 10:08:26 +02:00
* Run tests locally using `` ansible-test ``
* Extend
2017-04-14 02:46:21 +02:00
2020-02-24 20:29:22 +01:00
.. contents ::
:local:
2017-04-14 02:46:21 +02:00
Requirements
============
There are no special requirements for running `` ansible-test `` on Python 2.7 or later.
The `` argparse `` package is required for Python 2.6.
The requirements for each `` ansible-test `` command are covered later.
Test Environments
=================
Most `` ansible-test `` commands support running in one or more isolated test environments to simplify testing.
Remote
------
The `` --remote `` option runs tests in a cloud hosted environment.
An API key is required to use this feature.
Recommended for integration tests.
2020-02-24 20:29:22 +01:00
See the `list of supported platforms and versions <https://github.com/ansible/ansible/blob/devel/test/lib/ansible_test/_data/completion/remote.txt> `_ for additional details.
2017-04-14 02:46:21 +02:00
2018-03-07 01:28:06 +01:00
Environment Variables
---------------------
When using environment variables to manipulate tests there some limitations to keep in mind. Environment variables are:
2018-09-11 18:51:47 +02:00
* Not propagated from the host to the test environment when using the `` --docker `` or `` --remote `` options.
2019-09-26 20:28:06 +02:00
* Not exposed to the test environment unless whitelisted in `` test/lib/ansible_test/_internal/util.py `` in the `` common_environment `` function.
2018-03-07 01:28:06 +01:00
2019-09-26 20:28:06 +02:00
Example: `` ANSIBLE_KEEP_REMOTE_FILES=1 `` can be set when running `` ansible-test integration --venv `` . However, using the `` --docker `` option would
2018-09-11 18:51:47 +02:00
require running `` ansible-test shell `` to gain access to the Docker environment. Once at the shell prompt, the environment variable could be set
and the tests executed. This is useful for debugging tests inside a container by following the
2018-03-07 01:28:06 +01:00
:ref: `Debugging AnsibleModule-based modules <debugging_ansiblemodule_based_modules>` instructions.
2017-04-14 02:46:21 +02:00
Interactive Shell
=================
Use the `` ansible-test shell `` command to get an interactive shell in the same environment used to run tests. Examples:
* `` ansible-test shell --docker `` - Open a shell in the default docker container.
2019-09-26 20:28:06 +02:00
* `` ansible-test shell --venv --python 3.6 `` - Open a shell in a Python 3.6 virtual environment.
2017-04-14 02:46:21 +02:00
2018-03-07 01:28:06 +01:00
2017-04-14 02:46:21 +02:00
Code Coverage
=============
2017-10-19 22:36:57 +02:00
Code coverage reports make it easy to identify untested code for which more tests should
be written. Online reports are available but only cover the `` devel `` branch (see
2019-06-26 20:59:33 +02:00
:ref: `developing_testing` ). For new code local reports are needed.
2017-10-19 22:36:57 +02:00
Add the `` --coverage `` option to any test command to collect code coverage data. If you
2019-09-26 20:28:06 +02:00
aren't using the `` --venv `` or `` --docker `` options which create an isolated python
2017-10-19 22:36:57 +02:00
environment then you may have to use the `` --requirements `` option to ensure that the
2019-03-06 19:45:22 +01:00
correct version of the coverage module is installed::
2017-10-19 22:36:57 +02:00
2019-09-26 20:28:06 +02:00
ansible-test coverage erase
2017-10-19 22:36:57 +02:00
ansible-test units --coverage apt
2019-09-26 20:28:06 +02:00
ansible-test integration --coverage aws_lambda
2017-10-19 22:36:57 +02:00
ansible-test coverage html
2017-04-14 02:46:21 +02:00
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. For a full list of features see the online help::
ansible-test coverage --help