Testing¶
HAFO uses pytest for testing with the pytest-homeassistant-custom-component plugin.
Running Tests¶
Run all tests:
Run with coverage:
Run a specific test file:
Run a specific test:
Test Structure¶
tests/
├── conftest.py # Shared fixtures
├── test_init.py # Integration setup/unload tests
├── test_config_flow.py # Config flow tests
├── test_coordinator.py # Coordinator tests
├── test_sensor.py # Sensor entity tests
└── forecasters/
└── test_historical_shift.py # Forecaster algorithm tests
Writing Tests¶
Fixtures¶
Use the hass fixture from pytest-homeassistant-custom-component:
Mocking the Recorder¶
Mock the recorder statistics for testing:
from unittest.mock import patch
async def test_forecast(hass: HomeAssistant) -> None:
mock_stats = [
{"start": datetime(2024, 1, 1, 10, 0, 0, tzinfo=UTC), "mean": 2.0},
]
with patch(
"custom_components.hafo.forecasters.historical_shift.get_statistics_for_sensor",
return_value=mock_stats,
):
# Test code here
pass
Test Categories¶
- Unit Tests: Test individual functions and classes
- Integration Tests: Test component setup and teardown
- Config Flow Tests: Test the configuration UI
Coverage¶
Coverage is enforced on changed lines via Codecov. Aim for comprehensive test coverage of new code.