|
| 1 | +import os |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +def _real_gui_wanted() -> bool: |
| 7 | + """True when the tests should use real windows, not stubs. |
| 8 | +
|
| 9 | + CI (GitHub Actions sets ``CI=true``) keeps real windows so that the OpenCV |
| 10 | + HighGUI calls are exercised on each OS and OpenCV version. Locally, set |
| 11 | + ``MVTB_TEST_REAL_GUI=1`` to do the same. |
| 12 | + """ |
| 13 | + ci = os.environ.get("CI", "").lower() not in ("", "0", "false") |
| 14 | + return ci or os.environ.get("MVTB_TEST_REAL_GUI") == "1" |
| 15 | + |
| 16 | + |
| 17 | +# Keep local test runs headless by default. |
| 18 | +if not _real_gui_wanted(): |
| 19 | + os.environ.setdefault("MPLBACKEND", "Agg") |
| 20 | + os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") |
| 21 | + os.environ.setdefault("MVTB_TEST_MODE", "True") |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(autouse=True) |
| 25 | +def _suppress_gui(monkeypatch): |
| 26 | + """Disable GUI popups during tests. |
| 27 | +
|
| 28 | + Many tests exercise display code paths; these patches keep behavior testable |
| 29 | + without opening OpenCV or Matplotlib windows. They are skipped when real |
| 30 | + windows are wanted, see :func:`_real_gui_wanted`. |
| 31 | + """ |
| 32 | + if _real_gui_wanted(): |
| 33 | + return |
| 34 | + |
| 35 | + try: |
| 36 | + import cv2 |
| 37 | + |
| 38 | + if hasattr(cv2, "namedWindow"): |
| 39 | + monkeypatch.setattr(cv2, "namedWindow", lambda *args, **kwargs: None) |
| 40 | + if hasattr(cv2, "imshow"): |
| 41 | + monkeypatch.setattr(cv2, "imshow", lambda *args, **kwargs: None) |
| 42 | + if hasattr(cv2, "waitKey"): |
| 43 | + monkeypatch.setattr(cv2, "waitKey", lambda *args, **kwargs: -1) |
| 44 | + if hasattr(cv2, "destroyWindow"): |
| 45 | + monkeypatch.setattr(cv2, "destroyWindow", lambda *args, **kwargs: None) |
| 46 | + if hasattr(cv2, "destroyAllWindows"): |
| 47 | + monkeypatch.setattr(cv2, "destroyAllWindows", lambda *args, **kwargs: None) |
| 48 | + except Exception: |
| 49 | + pass |
| 50 | + |
| 51 | + try: |
| 52 | + import matplotlib |
| 53 | + |
| 54 | + matplotlib.use("Agg", force=True) |
| 55 | + import matplotlib.pyplot as plt |
| 56 | + |
| 57 | + monkeypatch.setattr(plt, "show", lambda *args, **kwargs: None) |
| 58 | + except Exception: |
| 59 | + pass |
0 commit comments