MiService icon indicating copy to clipboard operation
MiService copied to clipboard

feat: Add comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 5 months ago • 0 comments

Add Python Testing Infrastructure

Summary

This PR sets up a comprehensive testing infrastructure for the MiService Python project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Created pyproject.toml with Poetry configuration
  • Migrated dependencies from setup.py to Poetry format
  • Added development dependencies for testing

Testing Framework

  • pytest - Main testing framework
  • pytest-cov - Code coverage reporting with 80% threshold
  • pytest-mock - Mocking utilities for unit tests

Configuration

Added comprehensive pytest configuration in pyproject.toml:

  • Test discovery patterns for flexible test naming
  • Coverage reporting (terminal, HTML, XML formats)
  • Custom test markers: @pytest.mark.unit, @pytest.mark.integration, @pytest.mark.slow
  • Strict marker enforcement to prevent typos

Directory Structure

tests/
├── __init__.py
├── conftest.py          # Shared pytest fixtures
├── test_setup_validation.py  # Validation tests
├── unit/               # Unit tests directory
│   └── __init__.py
└── integration/        # Integration tests directory
    └── __init__.py

Shared Fixtures

Created reusable fixtures in conftest.py:

  • temp_dir - Temporary directory management
  • mock_config - Test configuration data
  • mock_token_store - Mock token file creation
  • mock_session - Mock aiohttp ClientSession
  • mock_response - Mock HTTP responses
  • mock_device_list - Sample device list data
  • mock_mi_account - Mock MiAccount instance
  • capture_logs - Log output capture for testing
  • reset_environment - Auto-reset environment variables
  • set_test_environment - Set test environment variables

Additional Updates

  • Created comprehensive .gitignore file including:
    • Python artifacts and virtual environments
    • Testing outputs (coverage reports, pytest cache)
    • IDE files
    • Claude settings directory (.claude/*)
    • Token files for security

Usage Instructions

Install Dependencies

poetry install

Run Tests

# Run all tests with coverage
poetry run pytest

# Run tests without coverage
poetry run pytest --no-cov

# Run specific test markers
poetry run pytest -m unit
poetry run pytest -m integration
poetry run pytest -m "not slow"

# Run with verbose output
poetry run pytest -v

View Coverage Reports

  • Terminal: Automatically shown after test run
  • HTML: Open htmlcov/index.html in browser
  • XML: Available at coverage.xml for CI integration

Notes

  • The coverage threshold is set to 80% but currently shows low coverage (13.70%) because no actual unit tests exist yet - only the testing infrastructure and validation tests
  • Poetry lock file (poetry.lock) is not gitignored and should be committed for reproducible builds
  • All pytest options remain available when using Poetry (e.g., poetry run pytest -v -k test_name)
  • The validation tests verify that the testing infrastructure is properly configured

Next Steps

With this infrastructure in place, 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 patterns
  4. Run tests with coverage to track code quality

llbbl avatar Jun 29 '25 19:06 llbbl