8a3ffb44b4f670bab3d5e2fb072f59eef4784b73
[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=False)
18 def GPIO():
19 """Mock RPi.GPIO module."""
20 GPIO = mock.MagicMock()
21 # Fudge for Python < 37 (possibly earlier)
22 sys.modules['RPi'] = mock.Mock()
23 sys.modules['RPi'].GPIO = GPIO
24 sys.modules['RPi.GPIO'] = GPIO
25 yield GPIO
26 del sys.modules['RPi']
27 del sys.modules['RPi.GPIO']
28
29
30 @pytest.fixture(scope='function', autouse=False)
31 def spidev():
32 """Mock spidev module."""
33 spidev = mock.MagicMock()
34 sys.modules['spidev'] = spidev
35 yield spidev
36 del sys.modules['spidev']
37
38
39 @pytest.fixture(scope='function', autouse=False)
40 def smbus():
41 """Mock smbus module."""
42 smbus = mock.MagicMock()
43 smbus.SMBus = SMBusFakeDevice
44 sys.modules['smbus'] = smbus
45 yield smbus
46 del sys.modules['smbus']
47
48
49 @pytest.fixture(scope='function', autouse=False)
50 def atexit():
51 """Mock atexit module."""
52 atexit = mock.MagicMock()
53 sys.modules['atexit'] = atexit
54 yield atexit
55 del sys.modules['atexit']
56