diff --git a/tests/config.py b/tests/config.py deleted file mode 100644 index e6023a0..0000000 --- a/tests/config.py +++ /dev/null @@ -1 +0,0 @@ -MOCK_TEST_MODE = True diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..492bf15 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,29 @@ +# content of conftest.py +from typing import Any + +import pytest + + +def pytest_addoption(parser: Any) -> None: + parser.addoption( + "--hardware_mode", + action="store", + default="True", + help="Set to true to run tests on hardware (requires video signal)", + ) + + +@pytest.fixture +def hardware_mode(request: Any) -> None: + try: + if request.config.getoption("--hardware") == "True": + print("Hardware test mode enabled") + request.cls.hardware_mode_is_set = True + elif request.config.getoption("--hardware") == "False": + print("Hardware test mode disabled") + request.cls.hardware_mode_is_set = False + else: + raise ValueError("hardware_mode option must be set to either 'True' or 'False'") + except ValueError: + print("Hardware test mode disabled") + request.cls.hardware_mode_is_set = False diff --git a/tests/test_capture_controller.py b/tests/test_capture_controller.py index 5e96c7a..f38ad1c 100644 --- a/tests/test_capture_controller.py +++ b/tests/test_capture_controller.py @@ -1,6 +1,7 @@ from time import perf_counter from unittest import TestCase +import pytest from numpy import diff, array from pymagewell.pro_capture_controller import ProCaptureController @@ -8,17 +9,18 @@ from pymagewell.pro_capture_device.device_interface import ProCaptureDeviceInterface from pymagewell.pro_capture_device.device_settings import ProCaptureSettings, TransferMode from pymagewell.pro_capture_device.mock_pro_capture_device import MockProCaptureDevice -from tests.config import MOCK_TEST_MODE +@pytest.mark.usefixtures("hardware_mode") class TestCaptureController(TestCase): def setUp(self) -> None: device_settings = ProCaptureSettings() device_settings.transfer_mode = TransferMode.TIMER - if MOCK_TEST_MODE: - self._device: ProCaptureDeviceInterface = MockProCaptureDevice(device_settings) + assert hasattr(self, "hardware_mode_is_set") + if self.hardware_mode_is_set: # type: ignore + self._device: ProCaptureDeviceInterface = ProCaptureDevice(device_settings) else: - self._device = ProCaptureDevice(device_settings) + self._device = MockProCaptureDevice(device_settings) self._controller = ProCaptureController(device=self._device) diff --git a/tests/test_device.py b/tests/test_device.py index 5a79dc7..9f45e2d 100644 --- a/tests/test_device.py +++ b/tests/test_device.py @@ -3,22 +3,26 @@ from time import perf_counter from unittest import TestCase +import pytest + from pymagewell.events.event import wait_for_event from pymagewell.pro_capture_device import ProCaptureDevice from pymagewell.pro_capture_device.device_interface import ProCaptureDeviceInterface from pymagewell.pro_capture_device.device_settings import ProCaptureSettings, TransferMode from pymagewell.pro_capture_device.mock_pro_capture_device import MockProCaptureDevice -from tests.config import MOCK_TEST_MODE +@pytest.mark.usefixtures("hardware_mode") class TestEvents(TestCase): def setUp(self) -> None: device_settings = ProCaptureSettings() device_settings.transfer_mode = TransferMode.TIMER - if MOCK_TEST_MODE: - self._device: ProCaptureDeviceInterface = MockProCaptureDevice(device_settings) + + assert hasattr(self, "hardware_mode_is_set") + if self.hardware_mode_is_set: # type: ignore + self._device: ProCaptureDeviceInterface = ProCaptureDevice(device_settings) else: - self._device = ProCaptureDevice(device_settings) + self._device = MockProCaptureDevice(device_settings) def tearDown(self) -> None: self._device.shutdown() @@ -46,7 +50,11 @@ def test_schedule_timer_event(self) -> None: self._device.schedule_timer_event() wait_for_event(self._device.events.timer_event, timeout_ms=1000) time_waited = perf_counter() - second_frame_start_time - self.assertAlmostEqual(time_waited, expected_wait_time, 2) + if self.hardware_mode_is_set: # type: ignore + self.assertAlmostEqual(time_waited, expected_wait_time, 3) + else: + # Mock mode timing is less accurate + self.assertAlmostEqual(time_waited, expected_wait_time, 1) def test_frame_transfer(self) -> None: transfer_buffer = create_string_buffer(3840 * 2160 * 4)