My-lecture-slides-and-code
My-lecture-slides-and-code copied to clipboard
feat: Set up comprehensive Python testing infrastructure with Poetry
Set up Python Testing Infrastructure
Summary
This PR establishes a comprehensive testing infrastructure for the Python project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for test-driven development with proper configuration, coverage reporting, and shared fixtures.
Changes Made
Package Management
-
Poetry Configuration: Created
pyproject.tomlwith Poetry as the package manager - Dependencies Migration: Migrated dependencies from the codebase (pandas, numpy, scikit-learn, matplotlib, xlrd, scipy)
- Development Dependencies: Added pytest (^7.4.0), pytest-cov (^4.1.0), and pytest-mock (^3.11.0)
Testing Configuration
-
pytest Settings: Configured in
pyproject.tomlwith:- Test discovery patterns for
test_*.pyand*_test.py - Coverage settings with 80% threshold
- HTML and XML coverage report generation
- Custom markers:
unit,integration,slow - Strict mode and detailed output options
- Test discovery patterns for
Directory Structure
tests/
├── __init__.py
├── conftest.py # Shared fixtures
├── test_validation.py # Infrastructure validation
├── unit/
│ └── __init__.py
└── integration/
└── __init__.py
Shared Fixtures (conftest.py)
-
temp_dir: Temporary directory creation and cleanup -
sample_csv_file: CSV file generation for testing -
sample_excel_file: Excel file generation for testing -
sample_iris_data: Mock iris dataset -
sample_boston_data: Mock Boston housing dataset -
mock_sklearn_model: Mock scikit-learn model -
sample_config: Configuration dictionary -
capture_stdout: Stdout capture utility -
mock_file_operations: File operation tracking -
reset_random_seed: Reproducibility helper
Additional Setup
-
Code Package: Added
__init__.pyto Code directory for proper packaging - .gitignore: Comprehensive Python gitignore with testing artifacts and Claude settings
-
Poetry Scripts: Configured
poetry run testandpoetry run testscommands
Testing the Infrastructure
Installation
poetry install
Running Tests
# Run all tests
poetry run test
# Run with coverage
poetry run pytest --cov
# Run specific markers
poetry run pytest -m unit
poetry run pytest -m integration
poetry run pytest -m slow
# Run validation tests
poetry run pytest tests/test_validation.py -v
Validation Results
All 17 validation tests pass successfully, confirming:
- pytest installation and functionality
- All dependencies are properly installed
- Fixtures are accessible and working
- Custom markers are registered
- Coverage configuration is active
- Test discovery is functioning
Notes
- Python version requirement set to ^3.11 for compatibility with latest scipy
- The existing
random_forest.pyfile has some syntax issues causing coverage warnings, but this doesn't affect the testing infrastructure - Coverage is currently at 0% as no production code is being tested (only infrastructure validation)
- The poetry.lock file is committed to ensure reproducible builds
Next Steps
Developers can now:
- Write unit tests in
tests/unit/ - Write integration tests in
tests/integration/ - Use the provided fixtures for common testing scenarios
- Run tests with coverage to ensure code quality
- Use custom markers to organize and run specific test suites