Skip to content

Commit 7a74da9

Browse files
authored
Merge pull request #906 from plugwise/bw_snap
Implement snapshot testing
2 parents 1769aed + 31872fa commit 7a74da9

17 files changed

+12900
-550
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Versions from 0.40 and up
44

55
## Ongoing
66

7-
- Fix mypy errors in Core
7+
- Fix mypy errors in Core (not accepted in HA Core)
8+
- Introduce Snapshot testing for all platforms (requirement from HA Core)
89

910
## v0.57.6
1011

scripts/core-testing.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,24 @@ if [ -z "${GITHUB_ACTIONS}" ] || [ "$1" == "testing" ] ; then
245245
if [ ! "${DEBUG}" == "" ] ; then
246246
debug_params="-rpP --log-cli-level=DEBUG"
247247
fi
248-
# shellcheck disable=SC2086
249-
pytest ${debug_params} ${subject} tests/components/${REPO_NAME}/${basedir} --snapshot-update --cov=homeassistant/components/${REPO_NAME}/ --cov-report term-missing || exit
248+
# First test if snapshots still valid (silent fail, otherwise will update snapshots)
249+
SNAPSHOT_UPDATED=0
250+
PYTEST_COMMAND="pytest ${debug_params} ${subject} tests/components/${REPO_NAME}/${basedir} --cov=homeassistant/components/${REPO_NAME}/ --cov-report term-missing"
251+
eval "${PYTEST_COMMAND}" || {
252+
echo ""
253+
echo -e "${CFAIL}Pytest / Snapshot validation failed, re-running to update snapshot ...${CNORM}"
254+
# Rerun with --snapshot-update; abort if this also fails
255+
eval "${PYTEST_COMMAND} --snapshot-update" || {
256+
echo ""
257+
echo -e "${CFAIL}Pytest failed, not a snapshot issue ...${CNORM}"
258+
exit 1
259+
}
260+
# Second run succeeded ⇒ mark snapshots updated
261+
SNAPSHOT_UPDATED=1
262+
} && {
263+
echo ""
264+
echo -e "${CINFO}Pytest / Snapshot validation passed"
265+
}
250266
fi # testing
251267

252268
if [ -z "${GITHUB_ACTIONS}" ] || [ "$1" == "quality" ] ; then
@@ -284,6 +300,9 @@ if [ -z "${GITHUB_ACTIONS}" ]; then
284300
echo ""
285301
cp -r ./homeassistant/components/${REPO_NAME} ../custom_components/
286302
cp -r ./tests/components/${REPO_NAME} ../tests/components/
303+
if [ "${SNAPSHOT_UPDATED}" == "1" ]; then
304+
echo -e "${CWARN}Note: updated snapshots ...${CNORM}"
305+
fi
287306
echo -e "${CINFO}Removing 'version' from manifest for hassfest-ing, version not allowed in core components${CNORM}"
288307
echo ""
289308
# shellcheck disable=SC2090

tests/components/plugwise/conftest.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Setup mocks for the Plugwise integration tests."""
22
from __future__ import annotations
33

4-
from collections.abc import Generator
4+
from collections.abc import AsyncGenerator, Generator
55
import json
66
from typing import Any
77
from unittest.mock import AsyncMock, MagicMock, patch
@@ -125,6 +125,28 @@ def mock_smile_config_flow() -> Generator[MagicMock]:
125125
yield api
126126

127127

128+
@pytest.fixture
129+
def platforms(request: pytest.FixtureRequest) -> list[str]:
130+
"""Fixture for platforms."""
131+
return list(request.param)
132+
133+
134+
@pytest.fixture
135+
async def setup_platform(
136+
hass: HomeAssistant,
137+
mock_config_entry: MockConfigEntry,
138+
platforms,
139+
) -> AsyncGenerator[None]:
140+
"""Set up one or all platforms."""
141+
142+
mock_config_entry.add_to_hass(hass)
143+
144+
with patch(f"homeassistant.components.{DOMAIN}.PLATFORMS", platforms):
145+
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
146+
await hass.async_block_till_done()
147+
yield mock_config_entry
148+
149+
128150
@pytest.fixture
129151
def mock_smile_adam() -> Generator[MagicMock]:
130152
"""Create a Mock Adam type for testing."""
@@ -187,8 +209,8 @@ def mock_smile_adam_heat_cool(chosen_env: str, cooling_present: bool) -> Generat
187209

188210

189211
@pytest.fixture
190-
def mock_smile_adam_4() -> Generator[MagicMock]:
191-
"""Create a 4th Mock Adam type for testing."""
212+
def mock_smile_adam_jip() -> Generator[MagicMock]:
213+
"""Create a Mock Adam-Jip type for testing."""
192214
chosen_env = "m_adam_jip"
193215
data = _read_json(chosen_env, "data")
194216
with patch(

0 commit comments

Comments
 (0)