feat: Set up complete Python testing infrastructure with Poetry
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.tomlconfiguration - Dependencies: Added testing dependencies as development dependencies:
pytest(^8.3.3) - Core testing frameworkpytest-cov(^5.0.0) - Coverage reportingpytest-mock(^3.14.0) - Mocking utilities
Testing Configuration
-
pytest Configuration:
- Test discovery patterns for
test_*.pyfiles - Coverage settings with 80% threshold
- HTML and XML coverage report generation
- Custom markers:
unit,integration,slow - Strict mode with detailed output
- Test discovery patterns for
-
Coverage Configuration:
- Source directories:
src,编程题,数据结构与算法 - Exclusions for test files and virtual environments
- Branch coverage enabled
- Multiple report formats
- Source directories:
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 testspoetry 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 operationstemp_file- Temporary file with contentmock_env_vars- Environment variable managementsample_data- Common test data structuressample_tree_node- Binary tree node for algorithm testssample_linked_list_node- Linked list node for algorithm testscapture_stdout- Stdout capture for testing print statementsmock_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
-
Poetry Lock File: The
poetry.lockfile is NOT gitignored and should be committed to ensure reproducible builds. -
Coverage Threshold: Set to 80% - tests will fail if coverage drops below this threshold.
-
Python Version: Configured for Python 3.8+ compatibility.
-
Test Organization: Tests are organized into
unit/andintegration/directories with automatic marker assignment based on location. -
Project Packages: The
srcdirectory 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:
- Create test files following the
test_*.pynaming convention - Use the provided fixtures from
conftest.py - Mark tests appropriately with
@pytest.mark.unit,@pytest.mark.integration, or@pytest.mark.slow - Run tests with coverage to ensure code quality