From 6840f27bc6217ea57c1617261a320c624427acbf Mon Sep 17 00:00:00 2001
From: Erik Montnemery <erik@montnemery.com>
Date: Tue, 30 Jul 2024 17:12:58 +0200
Subject: [PATCH] Verify respx mock routes are cleaned up when tests finish
 (#122852)

---
 tests/conftest.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/tests/conftest.py b/tests/conftest.py
index 0d0fd826b44..0667edf4be2 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -34,6 +34,7 @@ import multidict
 import pytest
 import pytest_socket
 import requests_mock
+import respx
 from syrupy.assertion import SnapshotAssertion
 
 from homeassistant import block_async_io
@@ -398,6 +399,13 @@ def verify_cleanup(
         # Restore the default time zone to not break subsequent tests
         dt_util.DEFAULT_TIME_ZONE = datetime.UTC
 
+    try:
+        # Verify respx.mock has been cleaned up
+        assert not respx.mock.routes, "respx.mock routes not cleaned up, maybe the test needs to be decorated with @respx.mock"
+    finally:
+        # Clear mock routes not break subsequent tests
+        respx.mock.clear()
+
 
 @pytest.fixture(autouse=True)
 def reset_hass_threading_local_object() -> Generator[None]:
-- 
GitLab