This document explains how to use pytest markers to configure Ansible Playtest options for your tests.
The Ansible Playtest plugin provides the following markers:
@pytest.mark.scenarios_dir(path)- Specifies the directory containing scenario files@pytest.mark.playbooks_dir(path)- Specifies the directory containing playbooks to test@pytest.mark.inventory_path(path)- Specifies the path to the inventory file@pytest.mark.mock_collections_dir(path)- Specifies path to mock collections directory
@pytest.mark.use_virtualenv(requirements=None)- Run the playbook in a virtual environment@pytest.mark.ansible_cfg_path(path)- Specifies a custom ansible.cfg file
@pytest.mark.keep_artifacts- Keep test artifacts after test completion@pytest.mark.ansible_scenario(name)- Identify a test as an Ansible scenario test
@pytest.mark.mock_modules(modules)- Mock the specified list of Ansible modules@pytest.mark.smtp_mock_server(port=1025)- Enable SMTP mock server
Markers can be applied at different levels:
Apply a marker to a specific test function:
@pytest.mark.scenarios_dir("./specific_scenarios")
def test_playbook(playbook_path, scenario_path, playbook_runner):
assert playbook_runner.successApply markers to all test methods in a class:
@pytest.mark.scenarios_dir("./class_scenarios")
@pytest.mark.playbooks_dir("./class_playbooks")
class TestAnsiblePlaybooks:
def test_playbook(self, playbook_path, scenario_path, playbook_runner):
assert playbook_runner.successApply markers to all tests in a module using pytestmark:
import pytest
pytestmark = [
pytest.mark.scenarios_dir("./module_scenarios"),
pytest.mark.playbooks_dir("./module_playbooks")
]Markers have higher precedence than command-line options. The resolution order is:
- Function-level markers
- Class-level markers
- Module-level markers
- Command-line options
- Default values
import pytest
@pytest.mark.scenarios_dir("./custom_scenarios")
@pytest.mark.playbooks_dir("./custom_playbooks")
def test_ansible_playbook(playbook_path, scenario_path, playbook_runner):
assert playbook_runner.success@pytest.mark.use_virtualenv(requirements="requirements.txt")
def test_with_virtualenv(playbook_path, scenario_path, playbook_runner):
assert playbook_runner.success@pytest.mark.smtp_mock_server(port=2025)
def test_with_smtp_mock(playbook_path, scenario_path, smtp_mock_server, playbook_runner):
# Test will have access to the smtp_mock_server fixture
assert len(smtp_mock_server.messages) > 0
assert playbook_runner.success@pytest.mark.scenarios_dir("./email_scenarios")
@pytest.mark.smtp_mock_server() # Use default port
@pytest.mark.keep_artifacts # Keep test artifacts for debugging
def test_email_playbook(playbook_path, scenario_path, smtp_mock_server, playbook_runner):
assert playbook_runner.success
assert len(smtp_mock_server.messages) > 0- Configuration Guide - More information about Ansible Playtest configuration
- Using Markers Example - Example code using markers