From b6433bfa8d10d0cea0ceb14b09c14ffb4c51ad63 Mon Sep 17 00:00:00 2001 From: Tim Nicholls Date: Thu, 3 Aug 2023 10:59:09 +0100 Subject: [PATCH] Update test cases to cope with mocking changed smbus imports in I2CDevice and fix module interdependencies --- tests/test_bme280.py | 13 +++++-------- tests/test_i2c_device.py | 33 ++++++++++++++++++--------------- tests/test_pac1921.py | 3 +-- tests/test_si570.py | 16 +++++++--------- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/tests/test_bme280.py b/tests/test_bme280.py index 3f807e5..8bceecf 100644 --- a/tests/test_bme280.py +++ b/tests/test_bme280.py @@ -11,15 +11,12 @@ import pytest if sys.version_info[0] == 3: - from unittest.mock import Mock, MagicMock, call, patch + from unittest.mock import Mock, patch else: - from mock import Mock, MagicMock, call, patch + from mock import Mock, patch -spidev_mock = MagicMock() -sys.modules['spidev'] = spidev_mock - -smbus_mock = MagicMock() -sys.modules['smbus'] = smbus_mock +sys.modules['spidev'] = Mock() +sys.modules['smbus'] = Mock() from odin_devices.bme280 import BME280 @@ -81,7 +78,7 @@ def assert_read_any_call(self, reg, length): @pytest.fixture(scope="class") def test_bme280_device_i2c(): """Fixture used in device test cases.""" - with patch('odin_devices.i2c_device.smbus.SMBus') as MockSMBus: + with patch('odin_devices.i2c_device.SMBus') as MockSMBus: mock_smbus = MockSMBus.return_value test_i2c_fixture = BME280TextFixtureI2C(mock_smbus) yield test_i2c_fixture diff --git a/tests/test_i2c_device.py b/tests/test_i2c_device.py index 684f6ca..2962f3b 100644 --- a/tests/test_i2c_device.py +++ b/tests/test_i2c_device.py @@ -1,21 +1,20 @@ """Test cases for the I2CTContainer class from odin_devices. Tim Nicholls, STFC Application Engineering Group """ - import sys import pytest if sys.version_info[0] == 3: # pragma: no cover - from unittest.mock import Mock, MagicMock, call + from unittest.mock import Mock, patch + from importlib import reload else: # pragma: no cover - from mock import Mock, MagicMock, call - -smbus_mock = MagicMock() -sys.modules['smbus'] = smbus_mock - -from odin_devices.i2c_device import I2CDevice, I2CException + from mock import Mock, patch + from imp import reload +# Initial mock of default smbus to allow i2c_device module to be imported +sys.modules['smbus'] = Mock() +import odin_devices.i2c_device class dummy_cm(): def __enter__(self): @@ -32,14 +31,18 @@ def __init__(self): self.device_busnum = 1 self.device_address = 0x70 self.device_debug = True - self.device = I2CDevice(self.device_address, self.device_busnum, self.device_debug) + self.device = odin_devices.i2c_device.I2CDevice(self.device_address, self.device_busnum, self.device_debug) self.device.pre_access = Mock() -@pytest.fixture(scope="class") -def test_i2c_device(): +@pytest.fixture(scope="class", params=["smbus", "smbus2"]) +def test_i2c_device(request): """Fixture used in driver test cases""" + # Mock out the parameterised smbus/smbus2 module and reload the i2c_device module + sys.modules[request.param] = Mock() + reload(odin_devices.i2c_device) + test_i2c_fixture = I2CDeviceTestFixture() yield test_i2c_fixture @@ -68,9 +71,9 @@ def test_device_init(self, test_i2c_device): def test_change_default_bus(self, test_i2c_device): default_i2c_bus = 0 - I2CDevice.set_default_i2c_bus(default_i2c_bus) + odin_devices.i2c_device.I2CDevice.set_default_i2c_bus(default_i2c_bus) - new_device = I2CDevice(test_i2c_device.device_address, debug=test_i2c_device.device_debug) + new_device = odin_devices.i2c_device.I2CDevice(test_i2c_device.device_address, debug=test_i2c_device.device_debug) assert default_i2c_bus == new_device.busnum def test_pre_access_called(self, test_i2c_device): @@ -113,7 +116,7 @@ def test_device_access(self, method, smbus_method, exc_mode, args, exp_rc, test_ elif exc_mode == self.EXC_MODE_TRAP: side_effect = IOError('mocked error') exc_enable = False - exp_rc = I2CDevice.ERROR + exp_rc = odin_devices.i2c_device.I2CDevice.ERROR elif exc_mode == self.EXC_MODE_RAISE: side_effect = IOError('mocked error') exc_enable = True @@ -127,7 +130,7 @@ def test_device_access(self, method, smbus_method, exc_mode, args, exp_rc, test_ exception_message = 'error from device' if exc_enable: - with pytest.raises(I2CException) as excinfo: + with pytest.raises(odin_devices.i2c_device.I2CException) as excinfo: rc = getattr(test_i2c_device.device, method)(*args) assert rc == exp_rc assert exception_message in excinfo.value diff --git a/tests/test_pac1921.py b/tests/test_pac1921.py index 69e41fe..f9cc665 100644 --- a/tests/test_pac1921.py +++ b/tests/test_pac1921.py @@ -74,10 +74,9 @@ from mock import Mock, MagicMock, call, patch sys.modules['smbus'] = MagicMock() -sys.modules['gpiod.Line'] = MagicMock() +sys.modules['gpiod'] = MagicMock() import odin_devices.pac1921 # Needed so that module can be reloaded from odin_devices.pac1921 import PAC1921, PAC1921_Synchronised_Array, Measurement_Type, OverflowException -import smbus from odin_devices.i2c_device import I2CDevice import gpiod diff --git a/tests/test_si570.py b/tests/test_si570.py index 3ca3f8f..5e2baba 100644 --- a/tests/test_si570.py +++ b/tests/test_si570.py @@ -11,13 +11,8 @@ else: # pragma: no cover from mock import Mock, call, patch -# sys.modules['smbus'] = Mock() +sys.modules['smbus'] = Mock() from odin_devices.si570 import SI570 -from odin_devices.i2c_device import I2CDevice, I2CException - - -def readList_side_effect(reg, length): - return [i for i in range(length)] class si570TestFixture(object): @@ -26,12 +21,15 @@ def __init__(self): self.address = 0x5d self.busnum = 1 self.model = SI570.SI570_C - with patch("odin_devices.i2c_device.I2CDevice.readU8") as mocked_readu8, \ - patch("odin_devices.i2c_device.I2CDevice.readList") as mocked_readlist: + with patch("odin_devices.si570.SI570.readU8") as mocked_readu8, \ + patch("odin_devices.si570.SI570.readList") as mocked_readlist: mocked_readu8.return_value = 0 - mocked_readlist.side_effect = readList_side_effect + mocked_readlist.side_effect = self.readList_side_effect self.driver = SI570(self.address, self.model, busnum=self.busnum) + def readList_side_effect(self, reg, length): + return [i for i in range(length)] + @pytest.fixture(scope="class") def test_si570_driver():