Skip to content

feat: Set up complete Python testing infrastructure with Poetry#2

Open
llbbl wants to merge 1 commit intoyongxinz:masterfrom
UnitSeeker:add-testing-infrastructure
Open

feat: Set up complete Python testing infrastructure with Poetry#2
llbbl wants to merge 1 commit intoyongxinz:masterfrom
UnitSeeker:add-testing-infrastructure

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 14, 2025

Set Up Python Testing Infrastructure

Summary

This PR establishes a complete testing infrastructure for the Python interview questions project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry Setup: Initialized Poetry with pyproject.toml configuration
  • Dependencies: Added testing dependencies as development dependencies:
    • pytest (^8.3.3) - Core testing framework
    • pytest-cov (^5.0.0) - Coverage reporting
    • pytest-mock (^3.14.0) - Mocking utilities

Testing Configuration

  • pytest Configuration:

    • Test discovery patterns for test_*.py files
    • Coverage settings with 80% threshold
    • HTML and XML coverage report generation
    • Custom markers: unit, integration, slow
    • Strict mode with detailed output
  • Coverage Configuration:

    • Source directories: src, 编程题, 数据结构与算法
    • Exclusions for test files and virtual environments
    • Branch coverage enabled
    • Multiple report formats

Project Structure

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

Development Commands

  • poetry run test - Run all tests
  • poetry run tests - Alternative command (both work)
  • poetry run pytest [options] - Direct pytest access with all options

Shared Fixtures (in conftest.py)

  • temp_dir - Temporary directory for file operations
  • temp_file - Temporary file with content
  • mock_env_vars - Environment variable management
  • sample_data - Common test data structures
  • sample_tree_node - Binary tree node for algorithm tests
  • sample_linked_list_node - Linked list node for algorithm tests
  • capture_stdout - Stdout capture for testing print statements
  • mock_input - Mock user input for interactive code

Testing the Infrastructure

Install Dependencies

poetry install

Run Tests

# Run all tests
poetry run test

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

# Run validation tests
poetry run pytest tests/test_validation.py -v

View Coverage Reports

  • HTML Report: htmlcov/index.html
  • XML Report: coverage.xml
  • Terminal Report: Automatically displayed after test run

Notes

  1. Poetry Lock File: The poetry.lock file is NOT gitignored and should be committed to ensure reproducible builds.

  2. Coverage Threshold: Set to 80% - tests will fail if coverage drops below this threshold.

  3. Python Version: Configured for Python 3.8+ compatibility.

  4. Test Organization: Tests are organized into unit/ and integration/ directories with automatic marker assignment based on location.

  5. Project Packages: The src directory is configured as a package, and all Python files in the project are included in coverage analysis.

Next Steps

Developers can now immediately start writing tests for the existing codebase:

  1. Create test files following the test_*.py naming convention
  2. Use the provided fixtures from conftest.py
  3. Mark tests appropriately with @pytest.mark.unit, @pytest.mark.integration, or @pytest.mark.slow
  4. Run tests with coverage to ensure code quality

- Initialize Poetry as package manager with pyproject.toml
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Configure comprehensive pytest and coverage settings
- Create test directory structure with shared fixtures
- Set up custom markers for unit, integration, and slow tests
- Add validation tests to verify infrastructure setup
- Update .gitignore with testing and Poetry-related entries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant