Metadata-Version: 2.1
Name: pytest-ansible-units
Version: 0.1.dev50
Summary: A pytest plugin for running unit tests within an ansible collection
Home-page: https://github.com/cidrblock/pytest-ansible-units
Author: Bradley A. Thornton
Author-email: bthornto@redhat.com
License: GNU GPL v3.0
Platform: UNKNOWN
Classifier: Framework :: Pytest
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Provides-Extra: ansible
Provides-Extra: ansible-base
Provides-Extra: ansible-core
Provides-Extra: test
License-File: LICENSE

# pytest-ansible-units

![VScode Overview](images/vscode_overview.png)
![VScode Debug](images/vscode_debug.png)

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


