Skip to content

Conversation

llbbl
Copy link

@llbbl llbbl commented Jun 25, 2025

Set up Python Testing Infrastructure for WeChat Alfred Workflow

Summary

This PR establishes a comprehensive testing infrastructure for the WeChat Alfred workflow project using modern Python tooling. The setup provides a solid foundation for adding unit and integration tests to improve code quality and maintainability.

Changes Made

Package Management

  • Poetry Configuration: Added pyproject.toml with Poetry as the package manager
  • Dependencies: Migrated from manual dependency management to Poetry
    • Production: requests (existing dependency)
    • Development: pytest, pytest-cov, pytest-mock

Testing Framework

  • pytest Configuration:
    • Test discovery patterns for test_*.py and *_test.py
    • Custom markers: unit, integration, slow
    • Strict mode enabled for better error detection
    • Coverage integration with HTML and XML reporting

Directory Structure

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

Testing Utilities

Created comprehensive fixtures in conftest.py:

  • temp_dir: Temporary directory for test files
  • mock_alfred_env: Mock Alfred workflow environment variables
  • mock_wechat_response: Sample WeChat API responses
  • mock_requests: Mocked HTTP requests
  • mock_workflow: Mocked Alfred Workflow object
  • captured_output: Stdout/stderr capture utility
  • Additional utility fixtures for testing

Configuration Files

  • pyproject.toml: Complete Poetry and pytest configuration
  • .gitignore: Updated with Python testing patterns and Claude settings
  • CLAUDE.md: Documentation for development commands

How to Use

Initial Setup

# Install Poetry if not already installed
curl -sSL https://install.python-poetry.org | python3 -

# Install project dependencies
poetry install

Running Tests

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

# Run with verbose output
poetry run pytest -v

# Run only unit tests
poetry run pytest -m unit

# Run only integration tests
poetry run pytest -m integration

# 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 the provided fixtures from conftest.py
  4. Mark tests appropriately: @pytest.mark.unit, @pytest.mark.integration, @pytest.mark.slow

Coverage Configuration

  • Coverage threshold currently set to 0% (to be increased as tests are added)
  • HTML reports generated in htmlcov/
  • XML reports generated as coverage.xml
  • Excludes bundled workflow library from coverage

Next Steps

  1. Start writing unit tests for individual modules (convert.py, search.py, etc.)
  2. Add integration tests for workflow functionality
  3. Gradually increase coverage threshold as tests are added
  4. Consider adding linting (flake8/black/ruff) and type checking (mypy)

Notes

  • The project uses the bundled Alfred-Workflow library which is excluded from coverage
  • Test infrastructure is compatible with both local development and CI/CD pipelines
  • All tests pass in the current setup, validating the infrastructure is working correctly

- Add Poetry configuration with pytest, pytest-cov, and pytest-mock
- Configure pytest with coverage reporting and custom markers
- Create test directory structure with unit and integration subdirectories
- Add comprehensive shared fixtures in conftest.py
- Create validation tests to verify setup functionality
- Update .gitignore with Python and testing related entries
- Configure coverage thresholds and reporting formats
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