Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 28, 2025

Python Testing Infrastructure Setup

Summary

This PR establishes a comprehensive testing infrastructure for the Python trading algorithms project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Created pyproject.toml with Poetry configuration as the default package manager
  • Migrated project dependencies based on code analysis:
    • Core: pandas, numpy, matplotlib, pandas-datareader, scikit-learn
    • Chapter 8 specific: quickfix (optional), twisted, pyyaml
  • Added development dependencies: pytest, pytest-cov, pytest-mock, black, flake8

Testing Configuration

  • Configured pytest with:

    • Test discovery patterns for multiple naming conventions
    • Coverage reporting with 80% threshold requirement
    • HTML and XML coverage report generation
    • Custom markers: unit, integration, slow
    • Strict mode options for better test quality
  • Set up coverage exclusions for:

    • Test files themselves
    • Virtual environments
    • Cache directories
    • Standard Python boilerplate code

Directory Structure

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

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory for file operations
  • mock_config: Mock configuration objects
  • sample_dataframe: Sample pandas DataFrame for testing
  • mock_market_data: Mock market data provider
  • mock_order: Mock trading order object
  • mock_portfolio: Mock portfolio object
  • reset_random_seed: Ensures reproducible tests
  • captured_logs: Log capture helper
  • mock_file_system: File system mocking
  • clean_imports: Module import cleanup

Additional Setup

  • Updated .gitignore with comprehensive Python/testing patterns
  • Created Poetry script commands:
    • poetry run test - Run all tests
    • poetry run tests - Alternative command
  • Made quickfix an optional dependency due to compilation issues

Running Tests

  1. Install dependencies:

    poetry install
  2. Run all tests:

    poetry run test
  3. Run specific test markers:

    poetry run test -m unit
    poetry run test -m integration
    poetry run test -m "not slow"
  4. Run tests without coverage (useful during development):

    poetry run test --no-cov
  5. Generate coverage reports:

    poetry run test  # Generates terminal, HTML, and XML reports

Notes

  • The infrastructure is ready for developers to start writing tests immediately
  • Coverage threshold is set to 80% but won't apply until actual source code tests are added
  • Quickfix dependency is optional (poetry install -E fix) due to platform-specific compilation issues
  • All pytest standard options are available through the Poetry scripts

- 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 markers
- Create test directory structure (tests/, unit/, integration/)
- Add shared fixtures in conftest.py for common test needs
- Configure Poetry scripts for running tests
- Update .gitignore with Python/testing patterns
- Add validation tests to verify setup functionality
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