estoque
estoque copied to clipboard
feat: Add complete Python testing infrastructure with Poetry and pytest
Add Python Testing Infrastructure
Summary
This PR sets up a complete testing infrastructure for the Django project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for writing and running tests with comprehensive coverage reporting.
Changes Made
Package Management
- Poetry Setup: Created
pyproject.tomlwith complete Poetry configuration - Dependency Migration: Migrated all dependencies from
requirements.txtto Poetry - Lock File: Generated
poetry.lockfor reproducible builds
Testing Framework
- pytest: Main testing framework with Django integration
- pytest-cov: Coverage reporting with 80% threshold requirement
- pytest-mock: Mocking utilities for isolated unit tests
- pytest-django: Django-specific testing utilities
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 Features
- Test Discovery: Configured to find tests in multiple patterns
- Coverage Settings:
- 80% coverage threshold
- HTML and XML report generation
- Exclusions for migrations, admin, and boilerplate code
- Test Markers: Added
unit,integration, andslowmarkers for test categorization - Poetry Scripts:
poetry run testandpoetry run testscommands available
Shared Fixtures
Created comprehensive fixtures in conftest.py:
temp_dir: Temporary directory managementmock_datetime: Consistent datetime mockingtest_user/admin_user: User creation utilitiesauthenticated_client/admin_client: Pre-authenticated test clientssample_product_data: Test data generationmock_file: File operation mockingmock_external_api: External API mockingcapture_logs: Log capture for testing
Environment Setup
- Created
.envfile with required Django settings for testing - Updated
.gitignorewith comprehensive exclusions for testing artifacts
How to Use
Installation
# Install Poetry (if not already installed)
curl -sSL https://install.python-poetry.org | python3 -
# Install dependencies
poetry install
Running Tests
# Run all tests
poetry run test
# Run with specific markers
poetry run pytest -m unit
poetry run pytest -m "not slow"
# Run specific test files
poetry run pytest tests/test_setup_validation.py
# Run with coverage report
poetry run pytest --cov-report=html
Writing New Tests
- Create test files following the naming pattern
test_*.pyor*_test.py - Place unit tests in
tests/unit/and integration tests intests/integration/ - Use the provided fixtures from
conftest.py - Mark tests appropriately with
@pytest.mark.unit,@pytest.mark.integration, or@pytest.mark.slow
Notes
- The infrastructure is ready for immediate use
- No actual application tests were written - only infrastructure validation
- Coverage threshold is set to 80% but can be adjusted in
pyproject.toml - The pandas dependency may require additional system packages for compilation
Next Steps
- Start writing unit tests for models and utilities
- Add integration tests for views and APIs
- Configure CI/CD to run tests automatically
- Consider adding additional testing tools (e.g., factory_boy, hypothesis)