A pytest plugin for running unit tests within an ansible collection
Project description
pytest-ansible-units
An experimental pytest
plugin to run an ansible collection's unit tests with pytest
.
Description
pytest-ansible-units
is a pytest
plugin that allows an ansible collection's unit tests to be run with only pytest
. pytest
can be used from the command line or from the IDE.
Getting Started
Dependencies
Installing pytest-ansible-units
will install the following:
pytest
pytest-ansible-units
requires python 3.8 or greater, ansible-core and pyyaml.
Installing
python -m pip install pytest-ansible-units
ansible-core
and pyyaml
need to be installed to activate the plugin.
2 directory structures are supported, with either approach collection dependencies need to be installed. Either in the default user location or in the collection tree structure in option 1.
Collection tree
The preferred approach is to clone the collections being developed into it's proper collection tree path. This eliminates the need for any symlinks and other collections being developed can be cloned into the same tree structure.
git clone <repo> collections/ansible_collections/<namespace>/<name>
Note:
pytest
needs to be run in the root of the collection directory, adjacent to the collection's galaxy.yml file
Shallow tree
The alternative approach allow for a shallow directory structure.
git clone <repo>
Notes:
pytest
needs to be run in the root of the collection directory, adjacent to the collection's galaxy.yml file- A collections directory will be created in the repository directory and the collections content linked into it.
- Add
/collections/
to the .gitignore, since there is no need for this to be checked in. ansible-test sanity
will fail due to the symlinks, with this approach.
Executing program
From the command line, from the collection's root directory:
pytest tests
Help
The following may be added to the collections' pyproject.toml
file to limit warnings and set the default path for the collection's tests
[tool.pytest.ini_options]
testpaths = [
"tests",
]
filterwarnings = [
'ignore:AnsibleCollectionFinder has already been configured',
]
Information from the galaxy.yml
file is used to build the collections
directory structure and link the contents. The galaxy.yml
file should reflect the correct collection namespace and name.
One way to detect issues without running the tests is to run:
pytest --collect-only
The follow errors may be seen:
E ModuleNotFoundError: No module named 'ansible_collections'
- Check the
galaxy.yml
file for an accurate namespace and name - Ensure
pytest
is being run from the collection's root directory, adjacent to thegalaxy.yml
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
- Ensure each test directory has an
__init__.py
Authors
- cidrblock
License
This project is licensed under the GPL-v3 License - see the LICENSE file for details
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pytest-ansible-units-0.1.dev50.tar.gz
.
File metadata
- Download URL: pytest-ansible-units-0.1.dev50.tar.gz
- Upload date:
- Size: 1.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 121a76284a0b5c5889c66c88610ff2a38724465ca5d5c801196eea85be369801 |
|
MD5 | 5404d6ae82903945aeae9759b61f4758 |
|
BLAKE2b-256 | afea20b98f36f839e9dcbea833042d641ca91d8b2da8bbc8c98ae2e33de84ca8 |