From a3c1926da57b5c89bf77c15622d75d231653f5d8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 14 Jul 2022 14:40:17 +0200 Subject: [PATCH] Add mock_bluetooth fixture (#75075) --- tests/components/bluetooth/conftest.py | 24 ----------------- tests/components/bluetooth/test_init.py | 23 +--------------- tests/conftest.py | 36 ++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 47 deletions(-) diff --git a/tests/components/bluetooth/conftest.py b/tests/components/bluetooth/conftest.py index fc0bd85b795..760500fe7a1 100644 --- a/tests/components/bluetooth/conftest.py +++ b/tests/components/bluetooth/conftest.py @@ -1,25 +1 @@ """Tests for the bluetooth component.""" - -import threading - -import pytest - -from tests.common import INSTANCES - - -@pytest.fixture(autouse=True) -def verify_cleanup(): - """Verify that the test has cleaned up resources correctly.""" - threads_before = frozenset(threading.enumerate()) - - yield - - if len(INSTANCES) >= 2: - count = len(INSTANCES) - for inst in INSTANCES: - inst.stop() - pytest.exit(f"Detected non stopped instances ({count}), aborting test run") - - threads = frozenset(threading.enumerate()) - threads_before - for thread in threads: - assert isinstance(thread, threading._DummyThread) diff --git a/tests/components/bluetooth/test_init.py b/tests/components/bluetooth/test_init.py index f43ef4737f6..2bbe4ce7dcb 100644 --- a/tests/components/bluetooth/test_init.py +++ b/tests/components/bluetooth/test_init.py @@ -1,10 +1,8 @@ """Tests for the Bluetooth integration.""" -from unittest.mock import AsyncMock, MagicMock, patch +from unittest.mock import MagicMock, patch -import bleak from bleak import BleakError from bleak.backends.scanner import AdvertisementData, BLEDevice -import pytest from homeassistant.components import bluetooth from homeassistant.components.bluetooth import ( @@ -16,25 +14,6 @@ from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT from homeassistant.setup import async_setup_component -@pytest.fixture() -def mock_bleak_scanner_start(): - """Fixture to mock starting the bleak scanner.""" - scanner = bleak.BleakScanner - models.HA_BLEAK_SCANNER = None - - with patch("homeassistant.components.bluetooth.HaBleakScanner.stop"), patch( - "homeassistant.components.bluetooth.HaBleakScanner.start", - ) as mock_bleak_scanner_start: - yield mock_bleak_scanner_start - - # We need to drop the stop method from the object since we patched - # out start and this fixture will expire before the stop method is called - # when EVENT_HOMEASSISTANT_STOP is fired. - if models.HA_BLEAK_SCANNER: - models.HA_BLEAK_SCANNER.stop = AsyncMock() - bleak.BleakScanner = scanner - - async def test_setup_and_stop(hass, mock_bleak_scanner_start): """Test we and setup and stop the scanner.""" mock_bt = [ diff --git a/tests/conftest.py b/tests/conftest.py index 4b2852e94fc..ee2e903e88d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -167,7 +167,8 @@ def verify_cleanup(): pytest.exit(f"Detected non stopped instances ({count}), aborting test run") threads = frozenset(threading.enumerate()) - threads_before - assert not threads + for thread in threads: + assert isinstance(thread, threading._DummyThread) @pytest.fixture(autouse=True) @@ -855,3 +856,36 @@ def mock_integration_frame(): ], ): yield correct_frame + + +@pytest.fixture(name="mock_bleak_scanner_start") +def mock_bleak_scanner_start(): + """Fixture to mock starting the bleak scanner.""" + + # Late imports to avoid loading bleak unless we need it + + import bleak # pylint: disable=import-outside-toplevel + + from homeassistant.components.bluetooth import ( # pylint: disable=import-outside-toplevel + models as bluetooth_models, + ) + + scanner = bleak.BleakScanner + bluetooth_models.HA_BLEAK_SCANNER = None + + with patch("homeassistant.components.bluetooth.HaBleakScanner.stop"), patch( + "homeassistant.components.bluetooth.HaBleakScanner.start", + ) as mock_bleak_scanner_start: + yield mock_bleak_scanner_start + + # We need to drop the stop method from the object since we patched + # out start and this fixture will expire before the stop method is called + # when EVENT_HOMEASSISTANT_STOP is fired. + if bluetooth_models.HA_BLEAK_SCANNER: + bluetooth_models.HA_BLEAK_SCANNER.stop = AsyncMock() + bleak.BleakScanner = scanner + + +@pytest.fixture(name="mock_bluetooth") +def mock_bluetooth(mock_bleak_scanner_start): + """Mock out bluetooth from starting."""