Repository: mne-tools/mne-python

Issue:
Use brain_gc in remaining tests
The new `brain_gc` fixture should be added in `test_plot_source_estimates`, `test_plot_sparse_source_estimates` and `test_notebook`.

Related to https://github.com/mne-tools/mne-python/pull/8379#issuecomment-714429361


Discussion:

Resolving patch:
diff --git a/mne/conftest.py b/mne/conftest.py
--- a/mne/conftest.py
+++ b/mne/conftest.py
@@ -318,9 +318,9 @@ def _check_skip_backend(name):
 @pytest.fixture()
 def renderer_notebook():
     """Verify that pytest_notebook is installed."""
-    from mne.viz.backends.renderer import _use_test_3d_backend
-    with _use_test_3d_backend('notebook'):
-        yield
+    from mne.viz.backends import renderer
+    with renderer._use_test_3d_backend('notebook'):
+        yield renderer
 
 
 @pytest.fixture(scope='session')
@@ -474,19 +474,25 @@ def download_is_error(monkeypatch):
 @pytest.fixture()
 def brain_gc(request):
     """Ensure that brain can be properly garbage collected."""
-    keys = ('renderer_interactive', 'renderer')
+    keys = ('renderer_interactive', 'renderer', 'renderer_notebook')
     assert set(request.fixturenames) & set(keys) != set()
     for key in keys:
         if key in request.fixturenames:
             is_pv = request.getfixturevalue(key)._get_3d_backend() == 'pyvista'
+            close_func = request.getfixturevalue(key).backend._close_all
             break
     if not is_pv:
         yield
         return
+    import pyvista
+    if LooseVersion(pyvista.__version__) <= LooseVersion('0.26.1'):
+        yield
+        return
     from mne.viz import Brain
     _assert_no_instances(Brain, 'before')
     ignore = set(id(o) for o in gc.get_objects())
     yield
+    close_func()
     _assert_no_instances(Brain, 'after')
     # We only check VTK for PyVista -- Mayavi/PySurfer is not as strict
     objs = gc.get_objects()
diff --git a/mne/viz/backends/_notebook.py b/mne/viz/backends/_notebook.py
--- a/mne/viz/backends/_notebook.py
+++ b/mne/viz/backends/_notebook.py
@@ -6,7 +6,8 @@
 from contextlib import contextmanager
 from ...fixes import nullcontext
 from ._pyvista import _Renderer as _PyVistaRenderer
-from ._pyvista import _set_3d_view, _set_3d_title  # noqa: F401 analysis:ignore
+from ._pyvista import \
+    _close_all, _set_3d_view, _set_3d_title  # noqa: F401 analysis:ignore
 
 
 class _Renderer(_PyVistaRenderer):


Test patch:
diff --git a/mne/viz/_brain/tests/test_notebook.py b/mne/viz/_brain/tests/test_notebook.py
--- a/mne/viz/_brain/tests/test_notebook.py
+++ b/mne/viz/_brain/tests/test_notebook.py
@@ -12,7 +12,7 @@
 @requires_version('nbformat')
 @requires_version('nbclient')
 @requires_version('ipympl')
-def test_notebook_3d_backend(renderer_notebook):
+def test_notebook_3d_backend(renderer_notebook, brain_gc):
     """Test executing a notebook that should not fail."""
     import nbformat
     from nbclient import NotebookClient
diff --git a/mne/viz/tests/test_3d.py b/mne/viz/tests/test_3d.py
--- a/mne/viz/tests/test_3d.py
+++ b/mne/viz/tests/test_3d.py
@@ -105,7 +105,7 @@ def test_plot_head_positions():
 @requires_pysurfer
 @traits_test
 @pytest.mark.slowtest
-def test_plot_sparse_source_estimates(renderer_interactive):
+def test_plot_sparse_source_estimates(renderer_interactive, brain_gc):
     """Test plotting of (sparse) source estimates."""
     sample_src = read_source_spaces(src_fname)
 
@@ -574,7 +574,7 @@ def test_snapshot_brain_montage(renderer):
 @pytest.mark.parametrize('pick_ori', ('vector', None))
 @pytest.mark.parametrize('kind', ('surface', 'volume', 'mixed'))
 def test_plot_source_estimates(renderer_interactive, all_src_types_inv_evoked,
-                               pick_ori, kind):
+                               pick_ori, kind, brain_gc):
     """Test plotting of scalar and vector source estimates."""
     invs, evoked = all_src_types_inv_evoked
     inv = invs[kind]