Skip to content

Commit

Permalink
Update cleanup fixture & more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pnbruckner committed Mar 12, 2024
1 parent d22bc24 commit 0fb8caa
Show file tree
Hide file tree
Showing 3 changed files with 420 additions and 336 deletions.
43 changes: 39 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
from __future__ import annotations

from collections.abc import AsyncGenerator
import logging

from custom_components.sun2.const import DOMAIN
import pytest
from pytest import LogCaptureFixture

from homeassistant.const import MAJOR_VERSION, MINOR_VERSION
from homeassistant.core import HomeAssistant
from pytest import FixtureRequest

pytest_plugins = ["pytest_homeassistant_custom_component"]

Expand All @@ -19,14 +21,47 @@ def auto_enable_custom_integrations(enable_custom_integrations):
yield


def pytest_configure(config):
config.addinivalue_line(
"markers", "cleanup_params(*, check_log_errors: bool = True)"
)


@pytest.fixture(autouse=True)
async def cleanup(
hass: HomeAssistant, caplog: LogCaptureFixture, check_errors: bool = True
hass: HomeAssistant, caplog: LogCaptureFixture, request: FixtureRequest
) -> AsyncGenerator[None, None]:
"""Cleanup after test & optionally check log for any errors."""
"""Cleanup after test & optionally check log for any errors.
Pass check_errors:
@pytest.mark.cleanup_params(
*, check_log_errors: bool = True, ignore_phrases: list[str] | None = None
)
async def test_abc() -> None:
...
"""
check_log_errors = True
ignore_phrases: list[str] = []
if marker := request.node.get_closest_marker("cleanup_params"):
if "check_log_errors" in marker.kwargs:
check_log_errors = marker.kwargs["check_log_errors"]
if "ignore_phrases" in marker.kwargs:
ignore_phrases = marker.kwargs["ignore_phrases"] or []
yield
if check_errors:
assert "ERROR" not in caplog.text
if check_log_errors:
for when in ("setup", "call"):
messages = [
x.message
for x in caplog.get_records(when)
if x.levelno == logging.ERROR
and not any(phrase in x.message for phrase in ignore_phrases)
]
if messages:
pytest.fail(
f"ERROR messages encountered during {when} phase: {messages}"
)
if (MAJOR_VERSION, MINOR_VERSION) > (2023, 5):
return
# Before 2023.5 configs were not unloaded at end of testing, since they are not
Expand Down
12 changes: 5 additions & 7 deletions tests/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ async def test_yaml_binary_sensor(
assert state.state == STATE_ON


_SUN_NEVER_REACHES = "Sun elevation never reaches"


@pytest.mark.cleanup_params(ignore_phrases=[_SUN_NEVER_REACHES])
@pytest.mark.parametrize(
"elevation,expected_state",
(
Expand Down Expand Up @@ -140,7 +144,7 @@ async def test_always_on_or_off(

# Check that there is an appropraite ERROR message.
assert any(
rec.levelname == "ERROR" and "Sun elevation never reaches" in rec.message
rec.levelname == "ERROR" and _SUN_NEVER_REACHES in rec.message
for rec in caplog.get_records("call")
)

Expand All @@ -156,12 +160,6 @@ async def test_always_on_or_off(
assert state.state == expected_state
assert state.attributes["next_change"] is None

# Check that there is an appropraite ERROR message.
assert any(
rec.levelname == "ERROR" and "Sun elevation never reaches" in rec.message
for rec in caplog.get_records("call")
)


@pytest.mark.parametrize(
"func,offset,expected_state", (("midnight", -1, STATE_ON), ("noon", 1, STATE_OFF))
Expand Down
Loading

0 comments on commit 0fb8caa

Please sign in to comment.