Skip to content

Commit 2f8f2f3

Browse files
felixfonteinpatchback[bot]
authored andcommitted
Allow running sanity and unit tests with antsibull-nox; impove testing documentation (#10104)
* Whitespace fixes. * Allow to run unit and sanity tests with antsibull-nox. * Document installation of needed collections for tests. * Also mention other sanity tests. (cherry picked from commit 6008e6e)
1 parent c0b5119 commit 2f8f2f3

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

CONTRIBUTING.md

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,49 @@ If you want to test a PR locally, refer to [our testing guide](https://github.co
4444

4545
If you find any inconsistencies or places in this document which can be improved, feel free to raise an issue or pull request to fix it.
4646

47-
## Run sanity, unit or integration tests locally
47+
## Run sanity or unit locally (with antsibull-nox)
48+
49+
The easiest way to run sanity and unit tests locally is to use [antsibull-nox](https://ansible.readthedocs.io/projects/antsibull-nox/).
50+
(If you have [nox](https://nox.thea.codes/en/stable/) installed, it will automatically install antsibull-nox in a virtual environment for you.)
51+
52+
### Sanity tests
53+
54+
The following commands show how to run ansible-test sanity tests:
55+
56+
```.bash
57+
# Run basic sanity tests for all files in the collection:
58+
nox -Re ansible-test-sanity-devel
59+
60+
# Run basic sanity tests for the given files and directories:
61+
nox -Re ansible-test-sanity-devel -- plugins/modules/system/pids.py tests/integration/targets/pids/
62+
63+
# Run all other sanity tests for all files in the collection:
64+
nox -R
65+
```
66+
67+
If you replace `-Re` with `-e`, respectively. leave `-R` away, then the virtual environments will be re-created. The `-R` re-uses them (if they already exist).
68+
69+
### Unit tests
70+
71+
The following commands show how to run unit tests:
72+
73+
```.bash
74+
# Run all unit tests:
75+
nox -Re ansible-test-units-devel
76+
77+
# Run all unit tests for one Python version (a lot faster):
78+
nox -Re ansible-test-units-devel -- --python 3.13
79+
80+
# Run a specific unit test (for the nmcli module) for one Python version:
81+
nox -Re ansible-test-units-devel -- --python 3.13 tests/unit/plugins/modules/net_tools/test_nmcli.py
82+
```
83+
84+
If you replace `-Re` with `-e`, then the virtual environments will be re-created. The `-R` re-uses them (if they already exist).
85+
86+
## Run basic sanity, unit or integration tests locally (with ansible-test)
87+
88+
Instead of using antsibull-nox, you can also run sanity and unit tests with ansible-test directly.
89+
This also allows you to run integration tests.
4890

4991
You have to check out the repository into a specific path structure to be able to run `ansible-test`. The path to the git checkout must end with `.../ansible_collections/community/general`. Please see [our testing guide](https://github.com/ansible/community-docs/blob/main/test_pr_locally_guide.rst) for instructions on how to check out the repository into a correct path structure. The short version of these instructions is:
5092

@@ -56,20 +98,27 @@ cd ~/dev/ansible_collections/community/general
5698

5799
Then you can run `ansible-test` (which is a part of [ansible-core](https://pypi.org/project/ansible-core/)) inside the checkout. The following example commands expect that you have installed Docker or Podman. Note that Podman has only been supported by more recent ansible-core releases. If you are using Docker, the following will work with Ansible 2.9+.
58100

59-
### Sanity tests
101+
### Basic sanity tests
60102

61-
The following commands show how to run sanity tests:
103+
The following commands show how to run basic sanity tests:
62104

63105
```.bash
64-
# Run sanity tests for all files in the collection:
106+
# Run basic sanity tests for all files in the collection:
65107
ansible-test sanity --docker -v
66108

67-
# Run sanity tests for the given files and directories:
109+
# Run basic sanity tests for the given files and directories:
68110
ansible-test sanity --docker -v plugins/modules/system/pids.py tests/integration/targets/pids/
69111
```
70112

71113
### Unit tests
72114

115+
Note that for running unit tests, you need to install required collections in the same folder structure that `community.general` is checked out in.
116+
Right now, you need to install [`community.internal_test_tools`](https://github.com/ansible-collections/community.internal_test_tools).
117+
If you want to use the latest version from GitHub, you can run:
118+
```
119+
git clone https://github.com/ansible-collections/community.internal_test_tools.git ~/dev/ansible_collections/community/internal_test_tools
120+
```
121+
73122
The following commands show how to run unit tests:
74123

75124
```.bash
@@ -85,15 +134,25 @@ ansible-test units --docker -v --python 3.8 tests/unit/plugins/modules/net_tools
85134

86135
### Integration tests
87136

137+
Note that for running integration tests, you need to install required collections in the same folder structure that `community.general` is checked out in.
138+
Right now, depending on the test, you need to install [`ansible.posix`](https://github.com/ansible-collections/ansible.posix), [`community.crypto`](https://github.com/ansible-collections/community.crypto), and [`community.docker`](https://github.com/ansible-collections/community.docker):
139+
If you want to use the latest versions from GitHub, you can run:
140+
```
141+
mkdir -p ~/dev/ansible_collections/ansible
142+
git clone https://github.com/ansible-collections/ansible.posix.git ~/dev/ansible_collections/ansible/posix
143+
git clone https://github.com/ansible-collections/community.crypto.git ~/dev/ansible_collections/community/crypto
144+
git clone https://github.com/ansible-collections/community.docker.git ~/dev/ansible_collections/community/docker
145+
```
146+
88147
The following commands show how to run integration tests:
89148

90149
#### In Docker
91150

92151
Integration tests on Docker have the following parameters:
93152
- `image_name` (required): The name of the Docker image. To get the list of supported Docker images, run
94153
`ansible-test integration --help` and look for _target docker images_.
95-
- `test_name` (optional): The name of the integration test.
96-
For modules, this equals the short name of the module; for example, `pacman` in case of `community.general.pacman`.
154+
- `test_name` (optional): The name of the integration test.
155+
For modules, this equals the short name of the module; for example, `pacman` in case of `community.general.pacman`.
97156
For plugins, the plugin type is added before the plugin's short name, for example `callback_yaml` for the `community.general.yaml` callback.
98157
```.bash
99158
# Test all plugins/modules on fedora40

antsibull-nox.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,9 @@ doc_fragment = "community.general.proxmox.actiongroup_proxmox"
5252

5353
[sessions.build_import_check]
5454
run_galaxy_importer = true
55+
56+
[sessions.ansible_test_sanity]
57+
include_devel = true
58+
59+
[sessions.ansible_test_units]
60+
include_devel = true

0 commit comments

Comments
 (0)