Blog_mini
Blog_mini copied to clipboard
feat: Add comprehensive testing infrastructure with Poetry and pytest
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.tomlwith Poetry configuration for dependency management - Migrated existing dependencies from
requirements.txtto 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.txtfor pip-based installation
Configuration
- Configured pytest in
pyproject.tomlwith:- Test discovery patterns for
test_*.pyand*_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%
- Test discovery patterns for
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 testspoetry run tests: Alternative command (both work)- Standard pytest options are available (e.g.,
-v,-k,--maxfail)
Additional Updates
- Updated
.gitignorewith:- Testing artifacts:
.pytest_cache/,.coverage,htmlcov/,coverage.xml - Claude settings:
.claude/* - Virtual environments and IDE files
- Note:
poetry.lockis intentionally not ignored
- Testing artifacts:
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.pycan be used - Validation tests are included to verify the infrastructure works correctly
Next Steps
- Developers can immediately start writing unit and integration tests
- Consider updating the application dependencies to modern versions
- Add pre-commit hooks to run tests automatically
- Set up CI/CD pipelines to run tests on pull requests