feat: Add complete Python testing infrastructure with Poetry
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.tomlwith Poetry configuration - Dependency Migration: Migrated existing dependencies from
requirements.txtto Poetry - Python Version: Set appropriate Python version constraint (>=3.8,<3.12) for PySide6 compatibility
- Lock File: Generated
poetry.lockfor reproducible installations
Testing Dependencies
Added as development dependencies:
pytest ^7.4.0- Main testing frameworkpytest-cov ^4.1.0- Coverage reportingpytest-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, andslowtest 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 managementmock_config- Application configuration mockingmock_cookie- Authentication cookie mockingmock_book_info,mock_chapter_list,mock_bookmark- Domain object mocksmock_requests- HTTP requests mockingmock_qt_app- PySide6 QApplication mockingcreate_test_file- Test file factorymock_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
-
Install dependencies:
poetry install -
Run all tests:
poetry run test -
Run specific tests:
poetry run test tests/unit/ poetry run test -k "test_name" poetry run test -m unit -
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 functionalityclient.py- Command-line interfacecookie.py- Cookie management- Business logic in
main.py