Skip to main content
PyPI version Python versions GitHub Workflow Status

A pytest plugin for use with Home Assistant custom components.


This pytest plugin provides several pytest fixtures and utils to make testing Home Assistant custom components easier.

The plugin allows you to use a subset of fixtures and helpers from the Home Assistant test code to allow you to test your own custom components.

Features

Fixtures

  • hass - Used to mock a hass instance. This is primarily useful in testing your config_flow code. Examples of it’s usage can be found in the Home Assistant tests.

from custom_components.steam_wishlist import config_flow

async def test_flow_init(hass):
    """Test the initial flow."""
    result = await hass.config_entries.flow.async_init(
        config_flow.DOMAIN, context={"source": "user"}
    )

    expected = {
        "data_schema": config_flow.DATA_SCHEMA,
        "description_placeholders": None,
        "errors": {},
        "flow_id": mock.ANY,
        "handler": "steam_wishlist",
        "step_id": "user",
        "type": "form",
    }
    assert expected == result
  • aioclient_mock - Used to mock responses from homeassistant.helpers.aiohttp_client.async_get_clientsession in your test code.

You can find example usage of both of these fixtures in the Home Assistant tests directory.

Helpers

  • pytest_homeassistant.async_mock - Contains all mock methods that can handle async calls.

from pytest_homeassistant.async_mock import AsyncMock
from custom_components.steam_wishlist import sensor_manager

async def test_sensormanager_async_register_component(
    hass, coordinator_mock
):
"""Test that we add listeners and referesh data if all platforms were registered."""
    manager = sensor_manager.SensorManager(hass, url="http://fake.com")
    mock_async_add_entities = AsyncMock()
    await manager.async_register_component("sensor", mock_async_add_entities)

    assert mock_async_add_entities.called is True

Requirements

  • homeassistant

Installation

You can install “pytest-homeassistant” via pip from PyPI:

$ pip install pytest-homeassistant

Contributing

Contributions are very welcome. I only incldued fixtures and test helpers that were useful to me when testing my custom components. If there are others that would be useful to you, pull requests are welcome!

License

Distributed under the terms of the MIT license, “pytest-homeassistant” is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Metadata

Release files for pytest-homeassistant 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-homeassistant 0.1.3
File Size Uploaded
pytest-homeassistant-0.1.3.tar.gz 12.2 kB Details

Release files / pytest-homeassistant-0.1.3.tar.gz

Download URL pytest-homeassistant-0.1.3.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
63a7b5065665f9c9898705209a05231db3fd5482d2c1607cd63ffc0f80b84d96
BLAKE2b-256 checksum
How to use checksums
68aaf17efbbed0cf3004d03a5c3d2519377284f7ee5eabfa7a7cb40a6bc255ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.5

Release history Release notifications | RSS feed

This release

0.1.3 This release

1 release file

0.1.2

1 release file

0.1.1

2 release files

0.1.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page