wereader icon indicating copy to clipboard operation
wereader copied to clipboard

feat: Add complete Python testing infrastructure with Poetry

Open llbbl opened this issue 6 months ago • 0 comments

Add Complete Python Testing Infrastructure

Summary

This PR sets up a comprehensive testing infrastructure for the WeReader (微信读书助手) project 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.

Changes Made

Package Management

  • Poetry Setup: Created pyproject.toml with Poetry configuration
  • Dependency Migration: Migrated existing dependencies from requirements.txt to Poetry
  • Python Version: Set appropriate Python version constraint (>=3.8,<3.12) for PySide6 compatibility
  • Lock File: Generated poetry.lock for reproducible installations

Testing Dependencies

Added as development dependencies:

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

Testing Configuration

Comprehensive pytest configuration in pyproject.toml:

  • Test Discovery: Configured patterns for finding test files
  • Coverage Settings:
    • Source tracking with appropriate exclusions
    • Multiple report formats (terminal, HTML, XML)
    • Detailed coverage reporting options
  • Custom Markers: Added unit, integration, and slow test markers
  • Strict Mode: Enabled strict marker validation

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)

Created comprehensive fixtures for common testing needs:

  • temp_dir - Temporary directory management
  • mock_config - Application configuration mocking
  • mock_cookie - Authentication cookie mocking
  • mock_book_info, mock_chapter_list, mock_bookmark - Domain object mocks
  • mock_requests - HTTP requests mocking
  • mock_qt_app - PySide6 QApplication mocking
  • create_test_file - Test file factory
  • mock_logger - Loguru logger mocking
  • And more utility fixtures

Development Commands

Poetry scripts configured for running tests:

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

All standard pytest options are available (e.g., -v, -k, specific test selection).

Additional Setup

  • Updated .gitignore: Added comprehensive testing, Python, and development-related entries
  • Validation Tests: Created tests to verify the infrastructure works correctly

Running Tests

  1. Install dependencies:

    poetry install
    
  2. Run all tests:

    poetry run test
    
  3. Run specific tests:

    poetry run test tests/unit/
    poetry run test -k "test_name"
    poetry run test -m unit
    
  4. View coverage report:

    # HTML report will be in htmlcov/index.html
    # XML report will be in coverage.xml
    

Notes

  • The infrastructure is set up but no actual unit tests for the codebase have been written yet
  • Coverage reporting is configured but will show 0% until actual tests are added
  • The Python version is constrained to <3.12 due to PySide6 compatibility requirements
  • All validation tests pass, confirming the infrastructure is properly configured

Next Steps

Developers can now immediately start writing tests for the codebase using the established infrastructure. Suggested areas for initial test coverage:

  • wereader.py - Core API functionality
  • client.py - Command-line interface
  • cookie.py - Cookie management
  • Business logic in main.py

llbbl avatar Jun 26 '25 19:06 llbbl