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