Developer Help: Pytest testing

Is there any documentation for things like how to mock other entities in unit test cases? Or how to set states using hass object.The developer docs are not very comprehensive. I’ve been reading existing test cases which use a mix of pytest, unittest as well as synchronous or asynchronous test suites. (Doing my head in).

I am trying to write pytest cases for a component I’m working on. Started off with the test_input_number.py file as a basis.

import asyncio
import logging

from homeassistant.core import CoreState, State, Context
from homeassistant.setup import async_setup_component
from homeassistant.components.input_boolean import (
    is_on, CONF_INITIAL, DOMAIN)
from homeassistant.const import (
    STATE_ON, STATE_OFF, ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, ATTR_ICON,
    SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON)

from tests.common import mock_component, mock_restore_cache

_LOGGER = logging.getLogger(__name__)

CONTROL_ENTITY = 'light.test_light';
SENSOR_ENTITY = 'binary_sensor.test_sensor';

async def test_config_options(hass):
    """Test configuration options."""

    hass.state = CoreState.starting
    _LOGGER.debug('ENTITIES @ start: %s', hass.states.async_entity_ids())

    assert await async_setup_component(hass, 'lightingsm', {'lightingsm': {
        'test': {'entity': CONTROL_ENTITY,
            'sensor': SENSOR_ENTITY
        },
        }})

    _LOGGER.debug('ENTITIES: %s', hass.states.async_entity_ids())
    hass.states.set(CONTROL_ENTITY, 'off')
  • Without the last line, test passes.
  • With the last line, test is stuck (some async problem)
  • Changed to await hass.states.set(CONTROL_ENTITY, 'off') does not make a difference test case is stuck.

Any advise?

Hi there,

Have you managed to find some help/tips/references ?

Em

no, i gave up it was too time consuming in the end.

Looks like deCONZ tests are pretty good…