Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
84 lines (65 loc) · 2.67 KB
/
Copy pathconftest.py
File metadata and controls
84 lines (65 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Fixtures and the ``hardware`` switch.
Everything runs with no camera, against a ``ProtocolDebugPort`` built from
``protocol.json``. Tests marked ``hardware`` (and the ``hardware`` flavour of the
``camera`` fixture) need a real Kiralux on USB: they run when one is found and
are skipped otherwise.
pytest # auto: hardware tests run only if a camera is plugged in
pytest --hardware # fail loudly if no camera is found
pytest --no-hardware # never touch USB
"""
import pytest
from kiralux import KiraluxCameraDevice
from support import FakeCameraPort, frame_bytes
def pytest_addoption(parser):
group = parser.getgroup("kiralux")
group.addoption("--hardware", action="store_true", help="require a camera for the hardware tests")
group.addoption("--no-hardware", action="store_true", help="skip the hardware tests")
def _camera_present() -> bool:
try:
return len(KiraluxCameraDevice.available()) > 0
except Exception: # no libusb, no permission, ...
return False
def pytest_collection_modifyitems(config, items):
if config.getoption("--no-hardware"):
reason = "--no-hardware"
elif config.getoption("--hardware") or _camera_present():
return
else:
reason = "no Kiralux camera found on USB"
skip = pytest.mark.skip(reason=reason)
for item in items:
if "hardware" in item.keywords:
item.add_marker(skip)
@pytest.fixture(scope="session")
def frame():
"""A random full-size frame: (wire bytes, the mosaic they encode)."""
return frame_bytes()
@pytest.fixture
def port(frame):
"""A camera-less port: answers from the CommandDictionary and streams ``frame``."""
return FakeCameraPort(image=frame[0])
@pytest.fixture
def mock_camera(port):
"""An initialized ``KiraluxCameraDevice`` on the debug port."""
cam = KiraluxCameraDevice(port=port)
cam.initializeDevice()
yield cam
cam.shutdownDevice()
@pytest.fixture(params=["mock", pytest.param("hardware", marks=pytest.mark.hardware)])
def camera(request, mock_camera):
"""The same contract, with and without a camera.
``mock`` is the debug-port camera; ``hardware`` is a real one, whose settings
are put back as they were when the test is done.
"""
if request.param == "mock":
yield mock_camera
return
cam = KiraluxCameraDevice()
cam.initializeDevice()
saved = (cam.exposure_us, cam.gain_db, cam.black_level, cam.binning, cam.roi, cam.frame_count)
try:
yield cam
finally:
cam.exposure_us, cam.gain_db, cam.black_level = saved[:3]
cam.binning, cam.roi, cam.frame_count = saved[3:]
cam.shutdownDevice()