stocktalk icon indicating copy to clipboard operation
stocktalk copied to clipboard

feat: Add comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 5 months ago • 0 comments

Add Python Testing Infrastructure

Summary

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

Changes Made

Package Management

  • Poetry Configuration: Created pyproject.toml with Poetry configuration
  • Dependency Migration: Migrated existing dependencies from requirements.txt to Poetry
  • Development Dependencies: Added pytest, pytest-cov, and pytest-mock as development dependencies

Testing Configuration

  • Pytest Settings: Configured pytest with:

    • Test discovery patterns for test_*.py and *_test.py files
    • Coverage reporting with HTML and XML outputs
    • Custom markers for unit, integration, and slow tests
    • Strict configuration options
  • Coverage Settings: Set up coverage.py with:

    • Source directory configuration
    • Exclusion patterns for test files and virtual environments
    • Multiple report formats (terminal, HTML, XML)
    • Currently set to 0% threshold (should be updated to 80% when adding actual tests)

Project Structure

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

Testing Fixtures

Created comprehensive fixtures in conftest.py:

  • temp_dir: Temporary directory management
  • mock_config: Mock configuration object
  • mock_mongo_client: Mock MongoDB client
  • mock_flask_app: Mock Flask application
  • mock_twitter_api: Mock Twitter API client
  • sample_tweet: Sample tweet data
  • sample_stock_data: Sample stock data
  • reset_environment: Environment variable management
  • mock_nltk_sentiment: Mock sentiment analyzer

Additional Setup

  • Git Ignore: Added comprehensive .gitignore with testing artifacts and Claude settings
  • Validation Tests: Created validation tests to verify the infrastructure works correctly

How to Use

Install Dependencies

poetry install

Run Tests

# Run all tests
poetry run test
# or
poetry run tests

# 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-report=html

Writing Tests

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

Notes

  • The coverage threshold is currently set to 0% to allow the infrastructure setup to complete. This should be increased to 80% as actual tests are added.
  • All pytest options are available when using the poetry run commands
  • The project uses Poetry's lock file for reproducible builds (poetry.lock should be committed)

llbbl avatar Jun 28 '25 20:06 llbbl