estoque icon indicating copy to clipboard operation
estoque copied to clipboard

feat: Add complete Python testing infrastructure with Poetry and pytest

Open llbbl opened this issue 5 months ago • 0 comments

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.toml with complete Poetry configuration
  • Dependency Migration: Migrated all dependencies from requirements.txt to Poetry
  • Lock File: Generated poetry.lock for 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, and slow markers for test categorization
  • Poetry Scripts: poetry run test and poetry run tests commands available

Shared Fixtures

Created comprehensive fixtures in conftest.py:

  • temp_dir: Temporary directory management
  • mock_datetime: Consistent datetime mocking
  • test_user / admin_user: User creation utilities
  • authenticated_client / admin_client: Pre-authenticated test clients
  • sample_product_data: Test data generation
  • mock_file: File operation mocking
  • mock_external_api: External API mocking
  • capture_logs: Log capture for testing

Environment Setup

  • Created .env file with required Django settings for testing
  • Updated .gitignore with 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

  1. Create test files following the naming pattern test_*.py or *_test.py
  2. Place unit tests in tests/unit/ and integration tests in tests/integration/
  3. Use the provided fixtures from conftest.py
  4. 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

  1. Start writing unit tests for models and utilities
  2. Add integration tests for views and APIs
  3. Configure CI/CD to run tests automatically
  4. Consider adding additional testing tools (e.g., factory_boy, hypothesis)

llbbl avatar Jun 29 '25 22:06 llbbl