Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 14, 2025

Add Python Testing Infrastructure

Summary

This PR sets up a comprehensive testing infrastructure for the cs-books repository using Poetry as the package manager and pytest as the testing framework. The setup provides a ready-to-use testing environment where developers can immediately start writing tests for the existing Python scripts.

Changes Made

Package Management

  • Poetry Configuration: Created pyproject.toml with Poetry as the package manager
  • Dependencies: Added testing dependencies as development requirements:
    • pytest (^7.4.0) - Main testing framework
    • pytest-cov (^4.1.0) - Coverage reporting
    • pytest-mock (^3.11.0) - Mocking utilities
  • Existing Dependencies: Migrated PyGithub dependency from implicit imports

Testing Configuration

  • pytest Settings: Configured comprehensive pytest options including:
    • Strict markers and configuration
    • Coverage reporting with HTML and XML output formats
    • Test discovery patterns for test_*.py and *_test.py files
    • Custom markers: unit, integration, and slow
  • Coverage Settings:
    • Source directories: src, add_commit_git_push, update_readme
    • Exclusions for test files and common patterns
    • Multiple report formats (terminal, HTML, XML)
    • Coverage threshold initially set to 0% (can be increased as tests are added)

Directory Structure

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

Fixtures and Utilities

Created comprehensive fixtures in conftest.py:

  • temp_dir: Creates temporary directories for testing
  • sample_pdf_list: Provides sample PDF filenames
  • mock_github_config: Mock GitHub configuration
  • sample_readme_content: Sample README content
  • temp_workspace: Creates a temporary workspace with sample files
  • mock_os_system: Mocks os.system calls
  • mock_github: Mocks GitHub API
  • capture_stdout: Captures stdout for testing print statements
  • change_test_dir: Automatically changes to test directory

Development Commands

Added Poetry script commands for convenience:

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

Both commands support all standard pytest options.

Additional Updates

  • Updated .gitignore with:
    • Testing-related entries (.pytest_cache/, .coverage, htmlcov/, etc.)
    • Claude-specific entries (.claude/*)
    • Note about committing poetry.lock for applications

How to Use

  1. Install dependencies:

    poetry install
  2. Run tests:

    poetry run test
    # or
    poetry run tests
    # or with options
    poetry run pytest -v -m unit
  3. Run specific test 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
  4. View coverage report:

    # After running tests, open the HTML report
    open htmlcov/index.html

Notes

  • The infrastructure is set up but no actual unit tests for the existing code have been written
  • Coverage threshold is currently set to 0% to allow the infrastructure to be merged
  • The validation tests confirm that all testing components are properly configured
  • Poetry was chosen as the default package manager since no existing package manager was detected
  • The src/ directory was created to follow Python packaging best practices

Next Steps

  1. Write unit tests for update_readme.py and add_commit_git_push.py
  2. Increase coverage threshold as tests are added
  3. Set up CI/CD pipeline to run tests automatically
  4. Consider adding additional testing tools (e.g., tox for multi-version testing)

- Set up Poetry as package manager with pyproject.toml configuration
- Add pytest, pytest-cov, and pytest-mock as development dependencies
- Configure comprehensive pytest and coverage settings
- Create test directory structure with unit/integration separation
- Add shared pytest fixtures in conftest.py
- Include validation tests to verify infrastructure setup
- Update .gitignore with testing and Claude-specific entries
- Add poetry script commands for running tests (test/tests)
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