Skip to content

fix: mock remaining OpenCV GUI calls in tests to prevent guiReceiver error - #297

Open
Sthitadhi1 wants to merge 1 commit into
Udayraj123:masterfrom
Sthitadhi1:master
Open

fix: mock remaining OpenCV GUI calls in tests to prevent guiReceiver error#297
Sthitadhi1 wants to merge 1 commit into
Udayraj123:masterfrom
Sthitadhi1:master

Conversation

@Sthitadhi1

Copy link
Copy Markdown

Problem

When running pytest or pre-commit hooks in headless environments (e.g., CI runners, containers, or environments without an active display server), the test suite fails with:

Root Cause

InteractionUtils.show() and is_window_available() in src/utils/interaction.py invoke cv2.namedWindow(), cv2.moveWindow(), and cv2.getWindowProperty(). Previously, setup_mocker_patches() in src/tests/utils.py only mocked cv2.imshow, cv2.destroyAllWindows, and cv2.waitKey. The three unmocked OpenCV GUI functions attempted real GUI window operations during testing, which fail in headless environments without a display backend.

Fix

Updated setup_mocker_patches(mocker) in src/tests/utils.py to add mocks for the remaining OpenCV GUI calls:

  • cv2.namedWindow -> returns None
  • cv2.moveWindow -> returns None
  • cv2.getWindowProperty -> returns 1 (simulates window visibility)

No production code in src/utils/interaction.py was altered, preserving actual display runtime behavior for end users.

Testing Notes

  • Verified locally via pytest and pre-commit that cv2.error: NULL guiReceiver (please create a window) is completely eliminated.
  • Note: Snapshot test failures when running locally on Windows are due to pre-existing OS-specific path separator differences (\ vs /) and are unrelated to this OpenCV GUI mocking fix.

Fixes #230

…error

Adds mocks for cv2.namedWindow, cv2.moveWindow, and cv2.getWindowProperty in setup_mocker_patches(), which were previously unmocked and caused cv2.error: NULL guiReceiver (please create a window) when running pytest or pre-commit hooks in headless environments (no display server).

Fixes Udayraj123#230

Signed-off-by: Sthitadhi Maity <maitysthitadhi@gmail.com>
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Fix headless test failures by mocking remaining OpenCV GUI calls

🐞 Bug fix 🧪 Tests 🕐 Less than 10 minutes

Grey Divider

AI Description

• Mock remaining OpenCV window APIs to prevent GUI calls during tests
• Eliminate NULL guiReceiver errors in headless CI/container environments
• Preserve production GUI behavior by limiting changes to test utilities
Diagram

graph TD
  A["Pytest suite"] --> B["src/tests/utils.py"] --> C["setup_mocker_patches()"] --> D["cv2 GUI APIs"]
  A --> E["entry_point_for_args()"] --> F["InteractionUtils.show() / is_window_available()"] --> D
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Use `opencv-python-headless` for test environments
  • ➕ Avoids GUI backends entirely; fewer GUI-related runtime errors in CI
  • ➕ Reduces need to mock individual GUI functions
  • ➖ Can diverge from production dependency behavior
  • ➖ May break code paths that rely on non-headless OpenCV builds in some environments
2. Add headless-guard logic in production interaction utilities
  • ➕ Prevents GUI calls at runtime when no display is available
  • ➕ Reduces reliance on tests to mock GUI behavior
  • ➖ Changes production behavior (explicitly avoided in this PR)
  • ➖ Requires robust headless detection across OSes and CI providers

Recommendation: Keep the current approach: targeted mocking in setup_mocker_patches() is the lowest-risk fix that addresses CI/headless failures without changing end-user runtime GUI behavior. Consider opencv-python-headless only if the project wants to standardize on headless execution for CI across the repo.

Files changed (1) +9 / -0

Tests (1) +9 / -0
utils.pyMock OpenCV window management calls in test patch setup +9/-0

Mock OpenCV window management calls in test patch setup

• Extends 'setup_mocker_patches()' to mock 'cv2.namedWindow', 'cv2.moveWindow', and 'cv2.getWindowProperty'. This prevents real window operations when tests exercise 'InteractionUtils.show()'/'is_window_available()' in headless environments.

src/tests/utils.py

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

To customize comments, go to the Qodo configuration screen, or learn more in the docs.

Qodo Logo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Environment] OpenCV NULL guiReceiver error during pre-commit testing .

1 participant