-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
/
Copy pathconftest.py
116 lines (88 loc) · 3.17 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""Setup some common test helper things."""
import asyncio
import functools
import logging
import os
from unittest.mock import patch, MagicMock
import pytest
import requests_mock as _requests_mock
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, INSTANCES
from tests.test_util.aiohttp import mock_aiohttp_client
from tests.mock.zwave import MockNetwork, MockOption
if os.environ.get('UVLOOP') == '1':
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
def test_real(func):
"""Force a function to require a keyword _test_real to be passed in."""
@functools.wraps(func)
def guard_func(*args, **kwargs):
real = kwargs.pop('_test_real', None)
if not real:
raise Exception('Forgot to mock or pass "_test_real=True" to %s',
func.__name__)
return func(*args, **kwargs)
return guard_func
# Guard a few functions that would make network connections
location.detect_location_info = test_real(location.detect_location_info)
location.elevation = test_real(location.elevation)
util.get_local_ip = lambda: '127.0.0.1'
@pytest.fixture(autouse=True)
def verify_cleanup():
"""Verify that the test has cleaned up resources correctly."""
yield
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
def hass(loop):
"""Fixture to provide a test instance of HASS."""
hass = loop.run_until_complete(async_test_home_assistant(loop))
yield hass
loop.run_until_complete(hass.async_stop())
@pytest.fixture
def requests_mock():
"""Fixture to provide a requests mocker."""
with _requests_mock.mock() as m:
yield m
@pytest.fixture
def aioclient_mock():
"""Fixture to mock aioclient calls."""
with mock_aiohttp_client() as mock_session:
yield mock_session
@pytest.fixture
def mqtt_mock(loop, hass):
"""Fixture to mock MQTT."""
with patch('homeassistant.components.mqtt.MQTT') as mock_mqtt:
mock_mqtt().async_connect.return_value = mock_coro(True)
assert loop.run_until_complete(setup.async_setup_component(
hass, mqtt.DOMAIN, {
mqtt.DOMAIN: {
mqtt.CONF_BROKER: 'mock-broker',
}
}))
client = mock_mqtt()
client.reset_mock()
return client
@pytest.fixture
def mock_openzwave():
"""Mock out Open Z-Wave."""
base_mock = MagicMock()
libopenzwave = base_mock.libopenzwave
libopenzwave.__file__ = 'test'
base_mock.network.ZWaveNetwork = MockNetwork
base_mock.option.ZWaveOption = MockOption
with patch.dict('sys.modules', {
'libopenzwave': libopenzwave,
'openzwave.option': base_mock.option,
'openzwave.network': base_mock.network,
'openzwave.group': base_mock.group,
}):
yield base_mock