Skip to content

Conversation

llbbl
Copy link

@llbbl llbbl commented Jun 17, 2025

Set up Python Testing Infrastructure

Summary

This PR establishes a complete testing infrastructure for the "40 Algorithms Every Programmer Should Know" Python project. The setup uses Poetry for dependency management and pytest as the testing framework, providing a solid foundation for test-driven development.

Changes Made

Package Management

  • ✅ Set up Poetry as the package manager
  • ✅ Created pyproject.toml with project configuration
  • ✅ Generated poetry.lock for reproducible builds

Testing Dependencies

Added the following development dependencies:

  • pytest (^7.4.3) - Core testing framework
  • pytest-cov (^4.1.0) - Coverage reporting integration
  • pytest-mock (^3.12.0) - Mocking utilities

Testing Configuration

Configured pytest in pyproject.toml with:

  • Test discovery patterns for test_*.py and *_test.py files
  • Coverage settings with HTML and XML report generation
  • Coverage threshold set to 0% initially (adjust as needed)
  • Custom markers: unit, integration, and slow
  • Strict marker enforcement and verbose output

Directory Structure

tests/
├── __init__.py
├── conftest.py          # Shared fixtures and configuration
├── test_validation.py   # Infrastructure validation tests
├── unit/
│   └── __init__.py
└── integration/
    └── __init__.py

Shared Fixtures (conftest.py)

Created comprehensive fixtures including:

  • temp_dir - Temporary directory management
  • temp_file - Temporary file creation
  • sample_data - Common test data structures
  • mock_config - Configuration mocking
  • sample_csv_data - CSV file generation
  • sample_json_data - JSON file generation
  • reset_random_seed - Reproducible test runs
  • helpers - Custom assertion utilities

Additional Setup

  • Updated .gitignore with Python and testing-related patterns
  • Created validation tests to verify the infrastructure works correctly

How to Use

Install Dependencies

poetry install --no-root

Run Tests

# Run all tests
poetry run pytest

# Run with specific markers
poetry run pytest -m unit
poetry run pytest -m integration
poetry run pytest -m "not slow"

# Run with coverage report
poetry run pytest --cov

# Run specific test file
poetry run pytest tests/test_validation.py

Writing Tests

  1. Place unit tests in tests/unit/
  2. Place integration tests in tests/integration/
  3. Use fixtures from conftest.py for common operations
  4. Mark tests appropriately: @pytest.mark.unit, @pytest.mark.integration, @pytest.mark.slow

Notes

  • Python version requirement updated to ^3.8 for compatibility with testing dependencies
  • Coverage threshold initially set to 0% to allow gradual improvement
  • The project uses --no-root installation as it's a code examples repository without a package structure
  • All 10 validation tests pass successfully, confirming the infrastructure is working correctly

Next Steps

  1. Adjust coverage threshold in pyproject.toml as more tests are added
  2. Add pre-commit hooks for automatic test running
  3. Configure CI/CD pipeline to run tests automatically
  4. Begin writing actual unit tests for the algorithm implementations

- Configure Poetry as package manager with pyproject.toml
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Set up pytest configuration with coverage thresholds and custom markers
- Create tests/ directory structure with unit and integration subdirectories
- Add comprehensive conftest.py with reusable fixtures
- Update .gitignore with testing and Python-related patterns
- Create validation tests to verify infrastructure setup
- Configure test commands accessible via `poetry run pytest`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant