write-module-tests

Generates pytest unit tests and ansible-test integration tests for Ansible modules.

8|7|Updated Feb 14, 2023
One-click install
npx skills add https://github.com/eclipse-slm/slm --skill write-module-tests-eclipse-slm
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: write-module-tests
Source: https://github.com/eclipse-slm/slm/tree/main/.agents/skills/write-module-tests
Command: npx skills add https://github.com/eclipse-slm/slm --skill write-module-tests-eclipse-slm

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing thorough tests for Ansible modules requires knowing pytest mocking patterns for AnsibleModule, exit_json/fail_json capture, check mode behavior, and ansible-test integration conventions. This Skill generates compliant unit and integration tests and audits existing ones against a quality checklist. ## Core Features & Use Cases - Unit Test Generation: Produces pytest test files at tests/unit/plugins/modules/ covering argument validation, success/failure paths, check mode, and idempotency with proper mocking of AnsibleModule. - Integration Test Generation: Creates ansible-test target playbooks following the check mode → create → idempotency → removal sequence with independent state verification. - Test Review Mode: Audits existing module tests against an 8-category checklist (coverage, naming, mocking, assertions, error paths) and scores them on a 1-10 scale with prioritized recommendations. - Use Case: After writing a custom Ansible module for managing cloud resources, invoke this Skill to generate a complete pytest suite that mocks the API client and validates changed status, plus an integration target runnable via ansible-test --docker. ## Quick Start Ask the AI to write unit and integration tests for your Ansible module at plugins/modules/my_module.py.

Frequently Asked Questions about write-module-tests

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write unit tests for an Ansible module with pytest?▼

Set module arguments via basic._ANSIBLE_ARGS, patch AnsibleModule exit_json and fail_json to raise exceptions, then invoke the module's main() and capture results with pytest.raises. Mock all API clients and run_command calls so tests never make real external requests.

How do I test Ansible modules with ansible-test integration?▼

Create a target directory at tests/integration/targets/<module_name>/ with tasks/main.yml containing test plays. Follow the sequence: check mode run, real creation, idempotency re-run, removal, and verify state independently with separate commands. Run with ansible-test integration <module_name> --docker.

Should Ansible module unit tests use pytest or unittest?▼

Pytest is the Ansible Engineering standard for module unit tests, not unittest. Use monkeypatch or unittest.mock.patch for mocking, pytest.raises for capturing exit_json/fail_json exceptions, and fixtures for shared setup like patching AnsibleModule methods.

How do I mock exit_json and fail_json in Ansible module tests?▼

Define custom AnsibleExitJson and AnsibleFailJson exception classes, then monkeypatch basic.AnsibleModule.exit_json and fail_json to raise them with their kwargs. This lets tests capture the result dictionary and assert on changed status and error messages.

When should I use Molecule instead of ansible-test for testing?▼

Use Molecule for testing Ansible roles and playbooks, not individual modules. Module unit and integration tests belong with ansible-test and pytest. This Skill explicitly excludes Molecule content tests, which are handled by a separate write-content-tests skill.

Why do my Ansible module tests fail to import the collection?▼

Imports like ansible_collections.<namespace>.<collection> require the collection to live in a standard ansible_collections/<namespace>/<name>/ directory layout so ansible-test can resolve it. Running tests outside this structure breaks the import path.