diff --git a/tests/conftest.py b/tests/conftest.py index 952b4ee4..964421c5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,17 +12,33 @@ from tools import MockSMBus +@pytest.fixture(scope='function', autouse=True) +def cleanup(): + for module in list(sys.modules.keys()): + if module.startswith('inky'): + del sys.modules[module] + + +@pytest.fixture(scope='function', autouse=False) +def nopath(): + path = sys.path + sys.path = [path for path in sys.path if not path.startswith("/usr/lib") and not path.startswith("/opt/hostedtoolcache")] + yield + sys.path = path + + @pytest.fixture(scope='function', autouse=False) def GPIO(): - """Mock RPi.GPIO module.""" - GPIO = mock.MagicMock() - # Fudge for Python < 37 (possibly earlier) - sys.modules['RPi'] = mock.MagicMock() - sys.modules['RPi'].GPIO = GPIO - sys.modules['RPi.GPIO'] = GPIO - yield GPIO - del sys.modules['RPi'] - del sys.modules['RPi.GPIO'] + """Mock gpiod and gpiodevice modules.""" + gpiod = mock.MagicMock() + gpiodevice = mock.MagicMock() + sys.modules['gpiod'] = gpiod + sys.modules['gpiodevice'] = gpiodevice + sys.modules['gpiodevice.platform'] = mock.MagicMock() + yield gpiod, gpiodevice + del sys.modules['gpiod'] + del sys.modules['gpiodevice'] + del sys.modules['gpiodevice.platform'] @pytest.fixture(scope='function', autouse=False) diff --git a/tests/test_init.py b/tests/test_init.py index fe6e0866..f31ad10a 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -1,6 +1,6 @@ """Initialization tests for Inky.""" -from unittest import mock +import sys import pytest @@ -19,49 +19,49 @@ def test_init_mock_what_black(tkinter, PIL): InkyMockWHAT('black') -def test_init_phat_black(spidev, smbus2): +def test_init_phat_black(spidev, smbus2, PIL): """Test initialisation of InkyPHAT with 'black' colour choice.""" from inky import InkyPHAT InkyPHAT('black') -def test_init_phat_red(spidev, smbus2): +def test_init_phat_red(spidev, smbus2, PIL): """Test initialisation of InkyPHAT with 'red' colour choice.""" from inky import InkyPHAT InkyPHAT('red') -def test_init_phat_yellow(spidev, smbus2): +def test_init_phat_yellow(spidev, smbus2, PIL): """Test initialisation of InkyPHAT with 'yellow' colour choice.""" from inky import InkyPHAT InkyPHAT('red') -def test_init_what_black(spidev, smbus2): +def test_init_what_black(spidev, smbus2, PIL): """Test initialisation of InkyWHAT with 'black' colour choice.""" from inky import InkyWHAT InkyWHAT('black') -def test_init_what_red(spidev, smbus2): +def test_init_what_red(spidev, smbus2, PIL): """Test initialisation of InkyWHAT with 'red' colour choice.""" from inky import InkyWHAT InkyWHAT('red') -def test_init_what_yellow(spidev, smbus2): +def test_init_what_yellow(spidev, smbus2, PIL): """Test initialisation of InkyWHAT with 'yellow' colour choice.""" from inky import InkyWHAT InkyWHAT('yellow') -def test_init_invalid_colour(spidev, smbus2): +def test_init_invalid_colour(spidev, smbus2, PIL): """Test initialisation of InkyWHAT with an invalid colour choice.""" from inky import InkyWHAT @@ -69,17 +69,7 @@ def test_init_invalid_colour(spidev, smbus2): InkyWHAT('octarine') -def test_init_what_setup_no_gpio(spidev, smbus2): - """Test Inky init with a missing RPi.GPIO library.""" - from inky import InkyWHAT - - inky = InkyWHAT('red') - - with pytest.raises(ImportError): - inky.setup() - - -def test_init_what_setup(spidev, smbus2, GPIO): +def test_init_what_setup(spidev, smbus2, GPIO, PIL): """Test initialisation and setup of InkyWHAT. Verify our expectations for GPIO setup in order to catch regressions. @@ -87,46 +77,17 @@ def test_init_what_setup(spidev, smbus2, GPIO): """ from inky import InkyWHAT - # TODO: _busy_wait should timeout after N seconds - GPIO.input.return_value = GPIO.LOW + # _busy_wait will timeout after N seconds + # GPIO.input.return_value = GPIO.LOW inky = InkyWHAT('red') inky.setup() - # Check GPIO setup - GPIO.setwarnings.assert_called_with(False) - GPIO.setmode.assert_called_with(GPIO.BCM) - GPIO.setup.assert_has_calls([ - mock.call(inky.dc_pin, GPIO.OUT, initial=GPIO.LOW, pull_up_down=GPIO.PUD_OFF), - mock.call(inky.reset_pin, GPIO.OUT, initial=GPIO.HIGH, pull_up_down=GPIO.PUD_OFF), - mock.call(inky.busy_pin, GPIO.IN, pull_up_down=GPIO.PUD_OFF) - ]) - - # Check device will been reset - GPIO.output.assert_has_calls([ - mock.call(inky.reset_pin, GPIO.LOW), - mock.call(inky.reset_pin, GPIO.HIGH) - ]) - # Check API will been opened spidev.SpiDev().open.assert_called_with(0, inky.cs_channel) -def test_init_7colour_setup_no_gpio(spidev, smbus2): - """Test initialisation and setup of 7-colour Inky. - - Verify an error is raised when RPi.GPIO is not present. - - """ - from inky.inky_uc8159 import Inky - - inky = Inky() - - with pytest.raises(ImportError): - inky.setup() - - -def test_init_7colour_setup(spidev, smbus2, GPIO): +def test_init_7colour_setup(spidev, smbus2, GPIO, PIL): """Test initialisation and setup of 7-colour Inky. Verify our expectations for GPIO setup in order to catch regressions. @@ -134,26 +95,11 @@ def test_init_7colour_setup(spidev, smbus2, GPIO): """ from inky.inky_uc8159 import Inky - # TODO: _busy_wait should timeout after N seconds - GPIO.input.return_value = GPIO.LOW + # _busy_wait will timeout after N seconds + # GPIO.input.return_value = GPIO.LOW inky = Inky() inky.setup() - # Check GPIO setup - GPIO.setwarnings.assert_called_with(False) - GPIO.setmode.assert_called_with(GPIO.BCM) - GPIO.setup.assert_has_calls([ - mock.call(inky.dc_pin, GPIO.OUT, initial=GPIO.LOW, pull_up_down=GPIO.PUD_OFF), - mock.call(inky.reset_pin, GPIO.OUT, initial=GPIO.HIGH, pull_up_down=GPIO.PUD_OFF), - mock.call(inky.busy_pin, GPIO.IN, pull_up_down=GPIO.PUD_OFF) - ]) - - # Check device will been reset - GPIO.output.assert_has_calls([ - mock.call(inky.reset_pin, GPIO.LOW), - mock.call(inky.reset_pin, GPIO.HIGH) - ]) - # Check API will been opened spidev.SpiDev().open.assert_called_with(0, inky.cs_channel) diff --git a/tests/test_install_helpers.py b/tests/test_install_helpers.py index 55c20395..e90e0cbe 100644 --- a/tests/test_install_helpers.py +++ b/tests/test_install_helpers.py @@ -8,7 +8,7 @@ import pytest -def test_mock_phat_no_tkinter(): +def test_mock_phat_no_tkinter(PIL, nopath): """Test initialisation of InkyMockPHAT without tkinter.""" from inky import InkyMockPHAT @@ -16,25 +16,10 @@ def test_mock_phat_no_tkinter(): InkyMockPHAT('black') -def test_mock_what_no_tkinter(): +def test_mock_what_no_tkinter(PIL, nopath): """Test initialisation of InkyMockWHAT without tkinter.""" from inky import InkyMockWHAT with pytest.raises(ImportError): InkyMockWHAT('black') - -def test_mock_phat_no_pil(tkinter): - """Test initialisation of InkyMockPHAT without PIL.""" - from inky import InkyMockPHAT - - with pytest.raises(ImportError): - InkyMockPHAT('black') - - -def test_mock_what_no_pil(tkinter): - """Test initialisation of InkyMockWHAT without PIL.""" - from inky import InkyMockWHAT - - with pytest.raises(ImportError): - InkyMockWHAT('black')