Blog_mini icon indicating copy to clipboard operation
Blog_mini copied to clipboard

feat: Add comprehensive testing infrastructure with Poetry and pytest

Open llbbl opened this issue 6 months ago • 0 comments

Add Testing Infrastructure to Flask Application

Summary

This PR adds a comprehensive testing infrastructure to the Flask blog application, providing all the necessary tools and configuration for developers to write and run tests effectively.

Changes Made

Package Management

  • Added pyproject.toml with Poetry configuration for dependency management
  • Migrated existing dependencies from requirements.txt to Poetry format
  • Added development dependencies group for testing tools

Testing Dependencies

  • pytest: Core testing framework (v6.2.0)
  • pytest-cov: Coverage reporting with 80% threshold
  • pytest-mock: Mocking utilities for unit tests
  • pytest-flask: Flask-specific testing helpers
  • Also created requirements/test.txt for pip-based installation

Configuration

  • Configured pytest in pyproject.toml with:
    • Test discovery patterns for test_*.py and *_test.py
    • Coverage settings with HTML and XML reports
    • Custom markers: @pytest.mark.unit, @pytest.mark.integration, @pytest.mark.slow
    • Strict mode and verbose output
    • Coverage failure threshold at 80%

Directory Structure

tests/
├── __init__.py
├── conftest.py          # Shared fixtures
├── conftest_full.py     # Full fixtures (for when deps are updated)
├── unit/
│   └── __init__.py
├── integration/
│   └── __init__.py
└── test_infrastructure_validation.py  # Validation tests

Test Fixtures (in conftest.py)

  • app: Flask application configured for testing
  • client: Test client for making requests
  • _db: Test database with automatic cleanup
  • session: Database session with transaction rollback
  • user/admin_user: Pre-configured test users
  • post/comment: Sample content fixtures
  • auth: Authentication helper actions
  • temp_dir: Temporary directory management
  • mock_config: Configuration mocking
  • captured_templates: Template rendering capture
  • mock_mail: Email sending mock

Development Commands

  • poetry run test: Run all tests
  • poetry run tests: Alternative command (both work)
  • Standard pytest options are available (e.g., -v, -k, --maxfail)

Additional Updates

  • Updated .gitignore with:
    • Testing artifacts: .pytest_cache/, .coverage, htmlcov/, coverage.xml
    • Claude settings: .claude/*
    • Virtual environments and IDE files
    • Note: poetry.lock is intentionally not ignored

Running Tests

Using Poetry (Recommended)

# Install dependencies
poetry install

# Run all tests
poetry run test

# Run with specific options
poetry run pytest -v -k "unit"

Using pip (Alternative)

# Create virtual environment
python3 -m venv venv
source venv/bin/activate

# Install dependencies
pip install -r requirements/test.txt
pip install -r requirements.txt

# Run tests
pytest

Notes

  • The current Flask application uses very old dependencies (Flask 0.10.1 from 2013)
  • These old dependencies have compatibility issues with modern Python versions
  • The testing infrastructure is fully set up and ready to use
  • Once the application dependencies are modernized, the full conftest_full.py can be used
  • Validation tests are included to verify the infrastructure works correctly

Next Steps

  1. Developers can immediately start writing unit and integration tests
  2. Consider updating the application dependencies to modern versions
  3. Add pre-commit hooks to run tests automatically
  4. Set up CI/CD pipelines to run tests on pull requests

llbbl avatar Jun 15 '25 17:06 llbbl