Running Tests
Prerequisites
Ensure you have installed test dependencies:Run All Tests
Run Specific Tests
Useful pytest Options
Test Configuration
Test configuration is defined inpyproject.toml:
Key Settings
- testpaths - Directory containing tests
- asyncio_mode = “auto” - Automatically detect and run async tests
- asyncio_debug = true - Enable asyncio debug mode to catch issues
- filterwarnings - Configure which warnings to show or ignore
Test Plugins
Home Assistant’s test suite uses several pytest plugins:- pytest-asyncio (1.3.0) - Support for async test functions
- pytest-aiohttp (1.1.0) - Fixtures for testing aiohttp applications
- pytest-cov (7.0.0) - Code coverage measurement
- pytest-freezer (0.4.9) - Freeze time in tests
- pytest-socket (0.7.0) - Disable or restrict socket calls
- pytest-timeout (2.4.0) - Set timeouts for tests
- pytest-sugar (1.0.0) - Better test output formatting
Writing Tests
Test Structure
Tests are organized to mirror the source code structure:Basic Test Example
Common Fixtures
Home Assistant provides useful fixtures:- hass - Home Assistant instance
- aiohttp_client - Test client for HTTP requests
- hass_ws_client - WebSocket client
- snapshot - Snapshot testing for data structures
Async Tests
Most Home Assistant code is async, so tests should be too:Freezing Time
Usefreezegun or pytest-freezer to control time in tests:
Mocking
Useunittest.mock for mocking:
Testing Config Flows
Code Coverage
Home Assistant usescoverage.py to measure test coverage.
Run Tests with Coverage
Coverage Configuration
Coverage is configured inpyproject.toml:
Coverage Requirements
- New code should have high test coverage (aim for 90%+)
- All public APIs must be tested
- Edge cases and error conditions should be covered
Test Best Practices
Do’s
- Write tests first - Consider TDD (Test-Driven Development)
- Test behavior, not implementation - Tests should verify what code does, not how it does it
- Use descriptive names - Test names should clearly describe what they test
- Keep tests isolated - Each test should be independent
- Test error cases - Don’t just test the happy path
- Use fixtures appropriately - Leverage pytest fixtures for setup/teardown
Don’ts
- Don’t test external services - Mock external dependencies
- Don’t write flaky tests - Tests should be deterministic
- Don’t share state - Avoid global variables or shared mutable objects
- Don’t test private methods - Test public interfaces instead
- Don’t make network calls - Use mocks or pytest-socket will catch them
Debugging Tests
Using pdb
Logging in Tests
-s to see log output:
Continuous Integration
Home Assistant uses GitHub Actions for CI. Tests run automatically on:- Pull requests
- Commits to the
devbranch - Commits to the
masterbranch
Component-Specific Testing
Testing Integrations
Integrations typically need tests for:- Config flow - Setup and configuration
- Entity creation - Entities are created correctly
- State updates - Entities reflect correct states
- Services - Service calls work as expected
- Unloading - Clean shutdown and resource cleanup
Testing Helpers
Helper modules should have:- Unit tests for all public functions
- Tests for edge cases
- Tests for error handling