1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Fail tests if wrapped callbacks or coroutines throw (#35010)

This commit is contained in:
Erik Montnemery
2020-05-06 23:14:57 +02:00
committed by GitHub
parent b35306052d
commit f1ecac92df
22 changed files with 156 additions and 38 deletions

View File

@@ -19,6 +19,7 @@ from homeassistant.setup import async_setup_component
from homeassistant.util import location
from tests.async_mock import patch
from tests.ignore_uncaught_exceptions import IGNORE_UNCAUGHT_EXCEPTIONS
pytest.register_assert_rewrite("tests.common")
@@ -36,6 +37,13 @@ logging.basicConfig(level=logging.DEBUG)
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
def pytest_configure(config):
"""Register marker for tests that log exceptions."""
config.addinivalue_line(
"markers", "no_fail_on_log_exception: mark test to not fail on logged exception"
)
def check_real(func):
"""Force a function to require a keyword _test_real to be passed in."""
@@ -95,6 +103,11 @@ def hass(loop, hass_storage, request):
loop.run_until_complete(hass.async_stop(force=True))
for ex in exceptions:
if (
request.module.__name__,
request.function.__name__,
) in IGNORE_UNCAUGHT_EXCEPTIONS:
continue
if isinstance(ex, ServiceNotFound):
continue
raise ex
@@ -242,3 +255,15 @@ def hass_ws_client(aiohttp_client, hass_access_token, hass):
return websocket
return create_client
@pytest.fixture(autouse=True)
def fail_on_log_exception(request, monkeypatch):
"""Fixture to fail if a callback wrapped by catch_log_exception or coroutine wrapped by async_create_catching_coro throws."""
if "no_fail_on_log_exception" in request.keywords:
return
def log_exception(format_err, *args):
raise
monkeypatch.setattr("homeassistant.util.logging.log_exception", log_exception)