dreader-hal/tests/conftest.py
2025-11-10 18:06:11 +01:00

73 lines
2.0 KiB
Python

"""
Pytest configuration and fixtures for dreader-hal tests.
"""
import os
import sys
import pytest
from unittest.mock import MagicMock
def is_raspberry_pi():
"""
Check if running on Raspberry Pi hardware.
Returns:
bool: True if running on RPi, False otherwise
"""
try:
# Check for BCM2xxx processor (Raspberry Pi)
with open('/proc/cpuinfo', 'r') as f:
cpuinfo = f.read()
return 'BCM2' in cpuinfo or 'BCM3' in cpuinfo
except (FileNotFoundError, PermissionError):
return False
# Mock RPi.GPIO at module level if not on Raspberry Pi
if not is_raspberry_pi():
# Create a mock GPIO module
mock_gpio = MagicMock()
mock_gpio.BCM = 11
mock_gpio.OUT = 0
mock_gpio.IN = 1
mock_gpio.HIGH = 1
mock_gpio.LOW = 0
mock_gpio.PUD_UP = 22
mock_gpio.FALLING = 32
mock_gpio.setmode = MagicMock()
mock_gpio.setup = MagicMock()
mock_gpio.output = MagicMock()
mock_gpio.input = MagicMock(return_value=0)
mock_gpio.add_event_detect = MagicMock()
mock_gpio.remove_event_detect = MagicMock()
mock_gpio.cleanup = MagicMock()
# Inject mock into sys.modules before any tests import it
if 'RPi' not in sys.modules:
sys.modules['RPi'] = MagicMock()
sys.modules['RPi.GPIO'] = mock_gpio
sys.modules['RPi'].GPIO = mock_gpio
# Create pytest markers
def pytest_configure(config):
"""Register custom pytest markers."""
config.addinivalue_line(
"markers", "rpi_only: mark test as requiring Raspberry Pi hardware"
)
config.addinivalue_line(
"markers", "hardware: mark test as requiring physical hardware"
)
def pytest_collection_modifyitems(config, items):
"""
Automatically skip tests that require RPi hardware when not on RPi.
"""
if not is_raspberry_pi():
skip_rpi = pytest.mark.skip(reason="Test requires Raspberry Pi hardware")
for item in items:
if "rpi_only" in item.keywords:
item.add_marker(skip_rpi)