Pytest Home Assistant Custom Component

0.13.322 · active · verified Fri Apr 10

pytest-homeassistant-custom-component is an experimental pytest plugin designed to facilitate testing of Home Assistant custom components. It automatically extracts test plugins and provides fixtures tailored for Home Assistant's architecture. The current version is 0.13.322, with a frequent release cadence that closely tracks Home Assistant core versions.

Warnings

Install

Quickstart

This quickstart demonstrates how to write a basic asynchronous pytest test using the `hass` fixture provided by the plugin. Save this code as `test_example.py` in your project's `tests/` directory and run `pytest` from your terminal. For actual custom component testing, you'd typically use `enable_custom_integration` and interact with Home Assistant's state machine, services, and entities.

import pytest

# Assuming you have a 'custom_components/my_integration/' directory
# with a basic __init__.py and possibly sensor.py, etc.

# To make the test runnable without a full custom integration, 
# we'll just demonstrate the 'hass' fixture.
# For real tests, you'd enable your component like:
# await enable_custom_integration("my_integration")

async def test_basic_hass_fixture(hass):
    """Verify the hass fixture is available and functional."""
    assert hass is not None
    assert hass.loop is not None
    assert hass.config is not None
    
    # Example: Check that the event bus is active
    assert hass.bus.listeners is not None
    
    # In a real test, you'd interact with services, states, etc.
    # For example:
    # await hass.async_block_till_done()
    # assert hass.states.get("sensor.my_test_sensor") is not None

view raw JSON →