MiService
MiService copied to clipboard
feat: Add comprehensive Python testing infrastructure with Poetry
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.tomlwith Poetry configuration - Migrated dependencies from
setup.pyto 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 managementmock_config- Test configuration datamock_token_store- Mock token file creationmock_session- Mock aiohttp ClientSessionmock_response- Mock HTTP responsesmock_device_list- Sample device list datamock_mi_account- Mock MiAccount instancecapture_logs- Log output capture for testingreset_environment- Auto-reset environment variablesset_test_environment- Set test environment variables
Additional Updates
- Created comprehensive
.gitignorefile 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.htmlin browser - XML: Available at
coverage.xmlfor 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:
- Write unit tests in
tests/unit/ - Write integration tests in
tests/integration/ - Use the provided fixtures for common testing patterns
- Run tests with coverage to track code quality