Skip to content

Commit 3eed2f2

Browse files
committed
Add skip condition for tests if OpenCV is missing
1 parent 94e4962 commit 3eed2f2

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

cdl/tests/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
from __future__ import annotations
2222

2323
import os
24-
from collections.abc import Generator
2524
from contextlib import contextmanager
25+
from typing import Generator
2626

27+
import pytest
2728
from guidata.guitest import run_testlauncher
2829

2930
import cdl.config # Loading icons
@@ -81,6 +82,17 @@ def cdltest_app_context(
8182
win.close()
8283

8384

85+
@contextmanager
86+
def skip_if_opencv_missing() -> Generator[None, None, None]:
87+
"""Skip test if OpenCV is not available"""
88+
try:
89+
yield
90+
except ImportError as exc:
91+
if "cv2" in str(exc).lower():
92+
pytest.skip("OpenCV not available, skipping test")
93+
raise exc
94+
95+
8496
def take_plotwidget_screenshot(panel: SignalPanel | ImagePanel, name: str) -> None:
8597
"""Eventually takes plotwidget screenshot (only in screenshot mode)"""
8698
if execenv.screenshot:

cdl/tests/features/images/blobs_app_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import cdl.param
1111
from cdl.obj import create_image
12-
from cdl.tests import cdltest_app_context
12+
from cdl.tests import cdltest_app_context, skip_if_opencv_missing
1313
from cdl.tests.data import get_test_image
1414

1515

@@ -32,7 +32,8 @@ def test_blobs():
3232
image = create_image(name, data)
3333
image.add_label_with_title()
3434
panel.add_object(image)
35-
compute_method(param)
35+
with skip_if_opencv_missing():
36+
compute_method(param)
3637

3738
# Testing distribute_on_grid and reset_positions
3839
# ==============================================

cdl/tests/scenarios/example_app_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import cdl.obj as dlo
1414
import cdl.param as dlp
1515
from cdl.proxy import proxy_context
16+
from cdl.tests import skip_if_opencv_missing
1617
from cdl.tests.data import get_test_image
1718

1819

@@ -39,7 +40,8 @@ def test_example_app():
3940
filter_by_circularity=True,
4041
min_circularity=0.2,
4142
)
42-
proxy.compute_blob_opencv(param)
43+
with skip_if_opencv_missing():
44+
proxy.compute_blob_opencv(param)
4345

4446

4547
if __name__ == "__main__":

0 commit comments

Comments
 (0)