Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 29, 2025

Add Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the 12306 ticket grabbing project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • ✅ Set up Poetry as the package manager
  • ✅ Created pyproject.toml with all project dependencies migrated from requirements.txt
  • ✅ Added testing dependencies as development dependencies

Testing Configuration

  • ✅ Configured pytest with comprehensive settings in pyproject.toml
  • ✅ Set coverage threshold to 80% with HTML and XML reporting
  • ✅ Added test markers: unit, integration, and slow
  • ✅ Configured test discovery patterns

Directory Structure

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

Testing Fixtures

Created comprehensive fixtures in conftest.py:

  • temp_dir - Temporary directory for file operations
  • mock_config - Mock configuration object
  • mock_session - Mock requests session
  • sample_ticket_data - Sample ticket data
  • mock_logger - Logger mocking
  • test_data_dir - Test data directory path
  • reset_environment - Environment variable cleanup
  • mock_file_operations - File operation mocking
  • capture_stdout - Stdout capture for testing

Development Commands

Configured Poetry scripts for running tests:

  • poetry run test - Run all tests
  • poetry run tests - Alternative command (both work)

Additional Updates

  • ✅ Updated .gitignore with testing artifacts and build files
  • ✅ Created CLAUDE.md for project documentation
  • ✅ Added validation tests to verify infrastructure

How to Use

  1. Install Poetry (if not already installed):

    curl -sSL https://install.python-poetry.org | python3 -
  2. Install dependencies:

    poetry install
  3. Run tests:

    poetry run test
    # or
    poetry run tests
  4. Run tests with specific markers:

    poetry run pytest -m unit      # Run only unit tests
    poetry run pytest -m integration  # Run only integration tests
    poetry run pytest -m "not slow"  # Skip slow tests
  5. View coverage report:

    poetry run pytest --cov
    # HTML report will be generated in htmlcov/

Dependencies Added

Testing Dependencies (Development)

  • pytest ^7.4.0 - Testing framework
  • pytest-cov ^4.1.0 - Coverage reporting
  • pytest-mock ^3.11.0 - Mocking utilities

Notes

  • The testing infrastructure is ready for immediate use
  • Coverage threshold is set to 80% and will fail builds if not met
  • All pytest standard options are available
  • The validation test file (test_setup_validation.py) can be used to verify the setup

Next Steps

Developers can now:

  1. Write unit tests in tests/unit/
  2. Write integration tests in tests/integration/
  3. Use the provided fixtures for common testing needs
  4. Run tests with coverage to ensure code quality

- Configure Poetry as package manager with pyproject.toml
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Set up pytest configuration with 80% coverage threshold
- Create test directory structure with unit/integration separation
- Configure test markers (unit, integration, slow)
- Add comprehensive test fixtures in conftest.py
- Update .gitignore with testing and build artifacts
- Create validation tests for infrastructure verification
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