V2RSS icon indicating copy to clipboard operation
V2RSS copied to clipboard

feat: Set up 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 Python project using Poetry as the package manager and pytest as the testing framework. The setup provides a ready-to-use testing environment where developers can immediately start writing tests.

Changes Made

Package Management

  • Initialized Poetry as the package manager by creating pyproject.toml
  • Migrated all dependencies from requirements.txt to Poetry format
  • Added testing dependencies as development dependencies:
    • pytest (^7.4.0) - Main testing framework
    • pytest-cov (^4.1.0) - Coverage reporting
    • pytest-mock (^3.11.1) - Mocking utilities

Testing Configuration

  • Configured pytest in pyproject.toml with:
    • Test discovery patterns for test_*.py and *_test.py files
    • Coverage reporting with HTML and XML outputs
    • Strict markers and configuration
    • Custom markers: unit, integration, slow
    • Coverage source set to src directory

Directory Structure

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

Shared Fixtures (conftest.py)

  • temp_dir - Creates temporary directories for tests
  • temp_file - Creates temporary files
  • mock_config - Provides mock configuration dictionary
  • mock_yaml_config - Creates temporary YAML config files
  • mock_env_vars - Sets up mock environment variables
  • clean_env - Removes specific environment variables
  • mock_request_data - Provides mock HTTP request data
  • sample_data_list - Sample test data
  • capture_logs - Helper for log assertions
  • mock_datetime - Mock datetime for consistent testing
  • Custom pytest options to handle slow tests with --run-slow flag

Build Configuration

  • Updated .gitignore with:
    • Testing artifacts (.pytest_cache/, coverage.xml, htmlcov/)
    • Claude settings (.claude/)
    • Note to commit poetry.lock file
    • IDE and OS-specific files

How to Use

Install Dependencies

poetry install

Run Tests

Both commands work and run pytest:

poetry run test
poetry run tests

Run Specific Tests

# Run only unit tests
poetry run pytest -m unit

# Run integration tests
poetry run pytest -m integration

# Run slow tests (normally skipped)
poetry run pytest --run-slow

# Run with verbose output
poetry run pytest -v

Coverage Reports

  • Coverage is automatically calculated when running tests
  • HTML report: htmlcov/index.html
  • XML report: coverage.xml
  • Terminal report shows uncovered lines

Writing New Tests

  1. Create test files in tests/unit/ or tests/integration/
  2. Name files as test_*.py or *_test.py
  3. Use fixtures from conftest.py for common needs
  4. Mark tests appropriately:
    @pytest.mark.unit
    def test_example():
        assert True
    

Notes

  • Coverage threshold is currently set to 0% since this PR only sets up infrastructure
  • The validation test file (test_setup_validation.py) verifies the setup works correctly
  • All Poetry commands should be run from the project root
  • The poetry.lock file should be committed to ensure reproducible builds

llbbl avatar Jun 14 '25 16:06 llbbl