diff --git a/tests/common.py b/tests/common.py index 5d9494eac81..30bd772a81f 100644 --- a/tests/common.py +++ b/tests/common.py @@ -33,7 +33,7 @@ from homeassistant.util.async import ( _TEST_INSTANCE_PORT = SERVER_PORT _LOGGER = logging.getLogger(__name__) -INST_COUNT = 0 +INSTANCES = [] def threadsafe_callback_factory(func): @@ -98,11 +98,10 @@ def get_test_home_assistant(): @asyncio.coroutine def async_test_home_assistant(loop): """Return a Home Assistant object pointing at test config dir.""" - global INST_COUNT - INST_COUNT += 1 loop._thread_ident = threading.get_ident() hass = ha.HomeAssistant(loop) + INSTANCES.append(hass) orig_async_add_job = hass.async_add_job @@ -134,8 +133,7 @@ def async_test_home_assistant(loop): @asyncio.coroutine def mock_async_start(): """Start the mocking.""" - # 1. We only mock time during tests - # 2. We want block_till_done that is called inside stop_track_tasks + # We only mock time during tests and we want to track tasks with patch('homeassistant.core._async_create_timer'), \ patch.object(hass, 'async_stop_track_tasks'): yield from orig_start() @@ -145,8 +143,7 @@ def async_test_home_assistant(loop): @ha.callback def clear_instance(event): """Clear global instance.""" - global INST_COUNT - INST_COUNT -= 1 + INSTANCES.remove(hass) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, clear_instance) diff --git a/tests/conftest.py b/tests/conftest.py index 07564e86c79..f1947a61ad0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,7 +12,7 @@ from homeassistant import util, setup from homeassistant.util import location from homeassistant.components import mqtt -from tests.common import async_test_home_assistant, mock_coro +from tests.common import async_test_home_assistant, mock_coro, INSTANCES from tests.test_util.aiohttp import mock_aiohttp_client from tests.mock.zwave import MockNetwork, MockOption @@ -50,8 +50,12 @@ def verify_cleanup(): """Verify that the test has cleaned up resources correctly.""" yield - from tests import common - assert common.INST_COUNT < 2 + if len(INSTANCES) >= 2: + count = len(INSTANCES) + for inst in INSTANCES: + inst.stop() + pytest.exit("Detected non stopped instances " + "({}), aborting test run".format(count)) @pytest.fixture