WPForce icon indicating copy to clipboard operation
WPForce copied to clipboard

feat: Add comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 6 months ago • 0 comments

Add Python Testing Infrastructure

Summary

This PR sets up a comprehensive testing infrastructure for the WPForce project using modern Python development tools and practices. The infrastructure provides a solid foundation for writing and maintaining tests, with proper dependency management, test organization, and coverage reporting.

Changes Made

Package Management

  • Poetry Configuration: Added pyproject.toml with Poetry as the package manager
  • Dependency Migration: Migrated the existing requests dependency to Poetry
  • Development Dependencies: Added pytest ecosystem tools as dev dependencies

Testing Framework

  • pytest: Main testing framework with comprehensive configuration
  • pytest-cov: Coverage reporting with HTML and XML output formats
  • pytest-mock: Mocking utilities for isolated unit tests

Project Structure

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

Configuration Details

pytest Configuration

  • Test discovery patterns for flexible file naming
  • Custom markers: @pytest.mark.unit, @pytest.mark.integration, @pytest.mark.slow
  • Coverage reporting with multiple formats
  • Strict mode for better error detection

Coverage Settings

  • Source tracking configured for the tests directory
  • Exclusions for virtual environments and cache files
  • HTML reports generated in htmlcov/
  • XML reports for CI integration

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory management
  • mock_config: Configuration object mocking
  • mock_wordpress_site: WordPress response mocking
  • mock_http_session: HTTP request mocking
  • sample_passwords_file / sample_usernames_file: Test file generation
  • capture_stdout: Output capture for CLI testing
  • Additional utilities for testing

Version Control

  • Comprehensive .gitignore for Python projects
  • Excludes test artifacts, coverage reports, and virtual environments
  • Includes Claude-specific exclusions

How to Use

Installation

# Install Poetry (if not already installed)
pip install poetry

# Install project dependencies
poetry install

Running Tests

# Run all tests
poetry run test

# Alternative command (both work)
poetry run tests

# Run with specific options
poetry run pytest -v  # Verbose output
poetry run pytest -k "test_name"  # Run specific test
poetry run pytest -m unit  # Run only unit tests
poetry run pytest -m integration  # Run only integration tests

Coverage Reports

After running tests, coverage reports are available in:

  • Terminal output (with missing lines)
  • htmlcov/index.html (detailed HTML report)
  • coverage.xml (for CI tools)

Notes

  1. Python Version: The project is configured for Python 3.8+ (the original code is Python 2.7, which needs migration)
  2. Coverage Threshold: Currently disabled due to legacy Python 2 code parsing issues
  3. Poetry Lock: The poetry.lock file should be committed for reproducible builds
  4. Test Organization: Tests are organized into unit and integration directories for better structure

Next Steps

  1. Migrate the existing Python 2.7 code to Python 3.8+
  2. Write unit tests for wpforce.py functionality
  3. Write unit tests for yertle.py functionality
  4. Add integration tests for the complete workflows
  5. Set up CI/CD pipeline with the coverage reports
  6. Re-enable coverage threshold once tests are written

Validation

The setup includes validation tests that verify:

  • Python version requirements
  • Project structure integrity
  • Testing framework installation
  • Plugin availability
  • Fixture functionality
  • Configuration correctness

All validation tests pass successfully, confirming the infrastructure is ready for use.

llbbl avatar Jun 20 '25 19:06 llbbl