Correcting examples
[EVA-2020-02-2.git] / library / tests / conftest.py
1 """Test configuration.
2 These allow the mocking of various Python modules
3 that might otherwise have runtime side-effects.
4 """
5 import sys
6 import mock
7 import pytest
8 from i2cdevice import MockSMBus
9
10
11 class SMBusFakeDevice(MockSMBus):
12 def __init__(self, i2c_bus):
13 MockSMBus.__init__(self, i2c_bus)
14 self.regs[0x00:0x01] = 0x0f, 0x00
15
16
17 @pytest.fixture(scope='function', autouse=True)
18 def cleanup():
19 yield None
20 try:
21 del sys.modules['enviroplus']
22 except KeyError:
23 pass
24 try:
25 del sys.modules['enviroplus.noise']
26 except KeyError:
27 pass
28 try:
29 del sys.modules['enviroplus.gas']
30 except KeyError:
31 pass
32
33
34 @pytest.fixture(scope='function', autouse=False)
35 def GPIO():
36 """Mock RPi.GPIO module."""
37 GPIO = mock.MagicMock()
38 # Fudge for Python < 37 (possibly earlier)
39 sys.modules['RPi'] = mock.Mock()
40 sys.modules['RPi'].GPIO = GPIO
41 sys.modules['RPi.GPIO'] = GPIO
42 yield GPIO
43 del sys.modules['RPi']
44 del sys.modules['RPi.GPIO']
45
46
47 @pytest.fixture(scope='function', autouse=False)
48 def spidev():
49 """Mock spidev module."""
50 spidev = mock.MagicMock()
51 sys.modules['spidev'] = spidev
52 yield spidev
53 del sys.modules['spidev']
54
55
56 @pytest.fixture(scope='function', autouse=False)
57 def smbus():
58 """Mock smbus module."""
59 smbus = mock.MagicMock()
60 smbus.SMBus = SMBusFakeDevice
61 sys.modules['smbus'] = smbus
62 yield smbus
63 del sys.modules['smbus']
64
65
66 @pytest.fixture(scope='function', autouse=False)
67 def atexit():
68 """Mock atexit module."""
69 atexit = mock.MagicMock()
70 sys.modules['atexit'] = atexit
71 yield atexit
72 del sys.modules['atexit']
73
74
75 @pytest.fixture(scope='function', autouse=False)
76 def sounddevice():
77 """Mock sounddevice module."""
78 sounddevice = mock.MagicMock()
79 sys.modules['sounddevice'] = sounddevice
80 yield sounddevice
81 del sys.modules['sounddevice']
82
83
84 @pytest.fixture(scope='function', autouse=False)
85 def numpy():
86 """Mock numpy module."""
87 numpy = mock.MagicMock()
88 sys.modules['numpy'] = numpy
89 yield numpy
90 del sys.modules['numpy']