From 84b0945cd9d89022354420bbf87ad0cdb67518e0 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Tue, 9 Apr 2024 10:58:57 -0400 Subject: [PATCH] ENH: Only check for closed when test passes --- tests/conftest.py | 25 +++++++++++++++++++++++++ tests/test_plotting.py | 4 +++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 3c3fdefe..e0bd48ee 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -120,3 +120,28 @@ def no_qt(monkeypatch): if need_reload: importlib.reload(pyvistaqt) assert 'qtpy' in sys.modules + + +# Adapted from MNE-Python and +# https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures # noqa: E501 + +_phase_report_key = pytest.StashKey() + + +@pytest.fixture +def check_test_passed(request): + """Return a function that checks if a test passed.""" + def _test_passed(): + if _phase_report_key not in request.node.stash: + return True + report = request.node.stash[_phase_report_key] + return "call" in report and report["call"].outcome == "passed" + return _test_passed + + +@pytest.hookimpl(tryfirst=True, hookwrapper=True) +def pytest_runtest_makereport(item, call): + """Stash the status of each item.""" + outcome = yield + rep = outcome.get_result() + item.stash.setdefault(_phase_report_key, {})[rep.when] = rep diff --git a/tests/test_plotting.py b/tests/test_plotting.py index 3cf517d9..c4920973 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -281,7 +281,7 @@ def test_editor(qtbot, plotting): @pytest.fixture() -def ensure_closed(): +def ensure_closed(check_test_passed): """Ensure all plotters are closed.""" try: from pyvista.plotting import close_all @@ -291,6 +291,8 @@ def ensure_closed(): close_all() # this is necessary to test _ALL_PLOTTERS assert len(_ALL_PLOTTERS) == 0 yield + if not check_test_passed(): + return WANT_AFTER = 0 if PV_VERSION >= Version('0.37') else 1 assert len(_ALL_PLOTTERS) == WANT_AFTER