achoz icon indicating copy to clipboard operation
achoz copied to clipboard

feat: Set up comprehensive Python testing infrastructure

Open llbbl opened this issue 3 months ago • 0 comments

Set up comprehensive Python testing infrastructure

Summary

This PR establishes a complete testing infrastructure for the achoz project, migrating from the old setup.py based configuration to a modern Poetry-based development environment with comprehensive testing capabilities.

Changes Made

Package Management Migration

  • Migrated from setup.py to Poetry: Created comprehensive pyproject.toml with all project metadata
  • Dependency Management: Moved all production dependencies from setup.py to Poetry configuration
  • Development Dependencies: Added testing dependencies as a separate development group

Testing Infrastructure

  • Testing Framework: Set up pytest with comprehensive configuration
  • Coverage Reporting: Configured pytest-cov with 80% coverage threshold
  • Test Organization: Created structured test directories (tests/unit/ and tests/integration/)
  • Shared Fixtures: Comprehensive conftest.py with reusable testing utilities
  • Custom Markers: Added unit, integration, and slow test markers for test categorization

Configuration Details

  • pytest configuration: Strict markers, verbose output, coverage reporting (HTML/XML)
  • Coverage settings: Source code tracking, exclusion patterns, reporting thresholds
  • Test discovery: Automatic test file pattern matching and collection
  • Isolation: Automatic test isolation with temporary directories and mocked global state

Code Quality Improvements

  • Import fixes: Updated all internal imports to use relative imports for proper package structure
  • Module resolution: Fixed import issues that prevented proper testing
  • Validation tests: Created comprehensive validation test suite to ensure infrastructure works

Development Environment

  • Git integration: Updated .gitignore with testing artifacts, IDE files, and environment exclusions
  • Ready-to-use setup: Complete environment that allows immediate test development
  • Documentation: Comprehensive fixtures and examples for common testing scenarios

Testing Infrastructure Components

Core Testing Dependencies

pytest = "^7.4.0"           # Main testing framework
pytest-cov = "^4.1.0"       # Coverage reporting
pytest-mock = "^3.11.1"     # Mocking utilities

Available Fixtures

  • temp_dir - Temporary directory for file operations
  • temp_file - Pre-created temporary file for testing
  • mock_config - Mock configuration dictionary
  • mock_global_vars - Mocked global variables with automatic cleanup
  • mock_meilisearch_client - Mock MeiliSearch client for testing
  • sample_documents - Sample document data for indexing tests
  • sample_test_files - Pre-created test files with various extensions
  • mock_logger - Mock logger to suppress output during tests
  • clean_environment - Environment variable isolation
  • mock_requests - Mock HTTP requests library

Test Markers

  • @pytest.mark.unit - Unit tests
  • @pytest.mark.integration - Integration tests
  • @pytest.mark.slow - Slow-running tests

How to Run Tests

Install Dependencies

poetry install

Run All Tests

poetry run pytest

Run with Coverage Report

poetry run pytest --cov=achoz --cov-report=html

Run Specific Test Categories

poetry run pytest -m unit          # Unit tests only
poetry run pytest -m integration   # Integration tests only
poetry run pytest -m "not slow"    # Skip slow tests

Coverage Reports

  • Terminal: Real-time coverage shown during test runs
  • HTML Report: Generated in htmlcov/ directory
  • XML Report: Generated as coverage.xml for CI/CD integration

Validation

The infrastructure includes comprehensive validation tests that verify:

  • All testing fixtures work correctly
  • Module imports function properly
  • Configuration is valid and complete
  • Coverage reporting generates properly
  • Test markers and categorization work

Current validation status: ✅ 18 passed, 1 skipped (OpenCV dependency optional)

Notes for Developers

Writing Tests

  1. Place unit tests in tests/unit/
  2. Place integration tests in tests/integration/
  3. Use the provided fixtures from conftest.py
  4. Mark tests appropriately with @pytest.mark.unit, @pytest.mark.integration, etc.
  5. Follow the 80% coverage threshold requirement

Development Workflow

  1. Write tests alongside code development
  2. Run tests frequently: poetry run pytest
  3. Check coverage: poetry run pytest --cov-report=html
  4. Use appropriate fixtures to mock external dependencies
  5. Ensure all tests pass before committing

Dependencies Management

  • Lock file (poetry.lock) is tracked in git
  • Use poetry add <package> for production dependencies
  • Use poetry add --group dev <package> for development dependencies
  • Use poetry install to set up environment from scratch

This testing infrastructure provides a solid foundation for maintaining code quality and enabling confident development and refactoring of the achoz codebase.

llbbl avatar Sep 03 '25 22:09 llbbl