Skip to content

Conversation

llbbl
Copy link

@llbbl llbbl commented Jun 29, 2025

Python Testing Infrastructure Setup

Summary

This PR sets up a complete testing infrastructure for the Python scripts in this Ansible playbooks repository. The infrastructure uses Poetry as the package manager and pytest as the testing framework, providing a modern and efficient testing environment.

Changes Made

Package Management

  • Poetry Setup: Introduced Poetry as the package manager through pyproject.toml
  • Dependencies: Added testing dependencies as development dependencies:
    • pytest (^8.0.0) - Core testing framework
    • pytest-cov (^5.0.0) - Coverage reporting
    • pytest-mock (^3.14.0) - Mocking utilities

Testing Configuration

  • pytest Configuration: Comprehensive pytest settings in pyproject.toml including:

    • Test discovery patterns for files, classes, and functions
    • Coverage reporting with HTML and XML output formats
    • Custom markers: unit, integration, and slow
    • Strict mode enabled for better test quality
    • Coverage threshold currently set to 0% (should be updated to 80% when adding actual code tests)
  • Coverage Configuration:

    • Source directory set to scripts/python
    • Branch coverage enabled
    • Exclusions for test files and common patterns
    • Multiple report formats (terminal, HTML, XML)

Directory Structure

tests/
├── __init__.py
├── conftest.py           # Shared pytest fixtures
├── test_setup_validation.py  # Infrastructure validation tests
├── unit/
│   └── __init__.py
└── integration/
    └── __init__.py

Testing Fixtures

Created comprehensive fixtures in conftest.py:

  • temp_dir: Temporary directory management
  • mock_config: Mock configuration data
  • sample_yaml_content: Sample YAML for Ansible testing
  • sample_json_data: Sample JSON data
  • mock_env_vars: Environment variable mocking
  • clean_environment: Clean test environment
  • mock_file_system: Mock file system structure
  • Auto-hooks for test organization and marking

Development Commands

Poetry script commands configured:

  • poetry run test - Run all tests
  • poetry run tests - Alternative command (both work)

Git Configuration

Updated .gitignore with:

  • Python artifacts (__pycache__/, *.pyc, etc.)
  • Testing artifacts (.pytest_cache/, .coverage, htmlcov/)
  • Virtual environments
  • IDE files
  • Claude settings (.claude/*)
  • Note: poetry.lock is NOT ignored (as per best practices)

How to Use

  1. Install Poetry (if not already installed):

    curl -sSL https://install.python-poetry.org | python3 -
  2. Install Dependencies:

    poetry install
  3. Run Tests:

    poetry run test
    # or
    poetry run tests
  4. Run Tests with Specific Markers:

    poetry run pytest -m unit      # Run only unit tests
    poetry run pytest -m integration  # Run only integration tests
    poetry run pytest -m "not slow"   # Skip slow tests
  5. View Coverage Report:

    # After running tests, open the HTML report
    open htmlcov/index.html  # macOS
    xdg-open htmlcov/index.html  # Linux

Notes

  • The coverage threshold is currently set to 0% to allow the infrastructure setup to pass. This should be updated to 80% (as commented in the code) once actual Python code tests are added.
  • All validation tests are passing, confirming the infrastructure is properly set up.
  • The infrastructure is ready for developers to immediately start writing tests for the Python scripts in the repository.

Future Recommendations

  1. Update coverage threshold to 80% when adding actual code tests
  2. Consider adding pre-commit hooks for running tests
  3. Add CI/CD integration for automated testing
  4. Consider adding additional testing tools like pytest-xdist for parallel test execution

- Added Poetry as the package manager with pyproject.toml configuration
- Configured pytest with coverage reporting and custom markers
- Created testing directory structure (tests/, unit/, integration/)
- Added comprehensive pytest fixtures in conftest.py
- Set up Poetry script commands for running tests
- Updated .gitignore with Python and testing-related entries
- Added validation tests to verify infrastructure setup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant