Skip to main content

A pytest plugin for running unit tests within an ansible collection

Project description

pytest-ansible-units

VScode Overview VScode Debug

A 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:

  • ansible-core
  • pytest
  • pyyaml

pytest-ansible-units requires python 3.8 or greater.

Installing

Until released on pypi:

python -m pip install git+https://github.com/cidrblock/pytest-ansible-units.git

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/<namepspace>/<name>

Note:

  • pytest needs to be run in the root of the collection directory, adjacent to the collection's galaxy.yml file

Source 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 the galaxy.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-ansible-units-0.0.1.dev0.tar.gz (1.6 MB view details)

Uploaded Source

File details

Details for the file pytest-ansible-units-0.0.1.dev0.tar.gz.

File metadata

File hashes

Hashes for pytest-ansible-units-0.0.1.dev0.tar.gz
Algorithm Hash digest
SHA256 2ce3a12a3fa874296e46b3d3f080f3416795d8f90e1a00365008bade79689ce9
MD5 5582132386e142ddc3316a56dfa84b2d
BLAKE2b-256 b5741304b1240bc12725e1f57a60ee881473126cfcc968d0fe2578c6a82a6df7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page