Skip to content

tests: turn widget leaks into warning #435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings
from collections.abc import Iterator
from pathlib import Path
from typing import TYPE_CHECKING
from unittest.mock import patch
Expand All @@ -15,15 +17,17 @@

# to create a new CMMCorePlus() for every test
@pytest.fixture(autouse=True)
def global_mmcore():
def global_mmcore() -> Iterator[CMMCorePlus]:
mmc = CMMCorePlus()
mmc.loadSystemConfiguration(TEST_CONFIG)
with patch.object(_mmcore_plus, "_instance", mmc):
yield mmc


@pytest.fixture(autouse=True)
def _run_after_each_test(request: "FixtureRequest", qapp: "QApplication"):
def _run_after_each_test(
request: "FixtureRequest", qapp: "QApplication"
) -> Iterator[None]:
"""Run after each test to ensure no widgets have been left around.

When this test fails, it means that a widget being tested has an issue closing
Expand Down Expand Up @@ -51,4 +55,8 @@ def _run_after_each_test(request: "FixtureRequest", qapp: "QApplication"):
return

test = f"{request.node.path.name}::{request.node.originalname}"
raise AssertionError(f"topLevelWidgets remaining after {test!r}: {remaining}")
warnings.warn(
f"topLevelWidgets remaining after {test!r}: {remaining}",
UserWarning,
stacklevel=2,
)