forked from pimoroni/enviroplus-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
117 lines (91 loc) · 2.85 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
117
"""Test configuration.
These allow the mocking of various Python modules
that might otherwise have runtime side-effects.
"""
import sys
import mock
import pytest
from i2cdevice import MockSMBus
class SMBusFakeDevice(MockSMBus):
def __init__(self, i2c_bus):
MockSMBus.__init__(self, i2c_bus)
self.regs[0x00:0x01] = 0x0f, 0x00
class SMBusFakeDeviceNoTimeout(MockSMBus):
def __init__(self, i2c_bus):
MockSMBus.__init__(self, i2c_bus)
self.regs[0x00:0x01] = 0x0f, 0x80
@pytest.fixture(scope="function", autouse=True)
def cleanup():
yield None
modules = "enviroplus", "enviroplus.noise", "enviroplus.gas", "ads1015", "i2cdevice"
for module in modules:
try:
del sys.modules[module]
except KeyError:
pass
@pytest.fixture(scope="function", autouse=False)
def gpiod():
sys.modules["gpiod"] = mock.Mock()
sys.modules["gpiod.line"] = mock.Mock()
yield sys.modules["gpiod"]
del sys.modules["gpiod.line"]
del sys.modules["gpiod"]
@pytest.fixture(scope="function", autouse=False)
def gpiodevice():
gpiodevice = mock.Mock()
gpiodevice.get_pins_for_platform.return_value = [(mock.Mock(), 0)]
gpiodevice.get_pin.return_value = (mock.Mock(), 0)
sys.modules["gpiodevice"] = gpiodevice
yield gpiodevice
del sys.modules["gpiodevice"]
@pytest.fixture(scope="function", autouse=False)
def spidev():
"""Mock spidev module."""
spidev = mock.MagicMock()
sys.modules["spidev"] = spidev
yield spidev
del sys.modules["spidev"]
@pytest.fixture(scope="function", autouse=False)
def smbus():
"""Mock smbus2 module."""
smbus = mock.MagicMock()
smbus.SMBus = SMBusFakeDevice
sys.modules["smbus2"] = smbus
yield smbus
del sys.modules["smbus2"]
@pytest.fixture(scope="function", autouse=False)
def smbus_notimeout():
"""Mock smbus2 module."""
smbus = mock.MagicMock()
smbus.SMBus = SMBusFakeDeviceNoTimeout
sys.modules["smbus2"] = smbus
yield smbus
del sys.modules["smbus2"]
@pytest.fixture(scope="function", autouse=False)
def mocksmbus():
"""Mock smbus2 module."""
smbus = mock.MagicMock()
sys.modules["smbus2"] = smbus
yield smbus
del sys.modules["smbus2"]
@pytest.fixture(scope="function", autouse=False)
def atexit():
"""Mock atexit module."""
atexit = mock.MagicMock()
sys.modules["atexit"] = atexit
yield atexit
del sys.modules["atexit"]
@pytest.fixture(scope="function", autouse=False)
def sounddevice():
"""Mock sounddevice module."""
sounddevice = mock.MagicMock()
sys.modules["sounddevice"] = sounddevice
yield sounddevice
del sys.modules["sounddevice"]
@pytest.fixture(scope="function", autouse=False)
def numpy():
"""Mock numpy module."""
numpy = mock.MagicMock()
sys.modules["numpy"] = numpy
yield numpy
del sys.modules["numpy"]