Repository: nilearn/nilearn

Issue:
Homogenize location and naming of testing utilities
related to this: there are instances where utility functions used only for tests are in the folder `X` in a module called something like `X/testing.py` instead of in something like `X/tests/_utils.py`

Examples

- nilearn/datasets/_testing.py
- nilearn/surface/testing_utils.py 

Other cases where test utility functions are within the test folder:

- nilearn/mass_univariate/tests/utils.py
- nilearn/decoding/tests/_utils.py
- nilearn/interfaces/fmriprep/tests/utils.py

_Originally posted by @Remi-Gau in https://github.com/nilearn/nilearn/issues/3613#issuecomment-1511578081_
            


Discussion:

Resolving patch:
diff --git a/doc/conftest.py b/doc/conftest.py
--- a/doc/conftest.py
+++ b/doc/conftest.py
@@ -1 +1,4 @@
-from nilearn.datasets._testing import request_mocker, temp_nilearn_data_dir
+from nilearn.datasets.tests._testing import (
+    request_mocker,
+    temp_nilearn_data_dir,
+)
diff --git a/nilearn/conftest.py b/nilearn/conftest.py
--- a/nilearn/conftest.py
+++ b/nilearn/conftest.py
@@ -10,8 +10,8 @@
 from nilearn import image
 
 # we need to import these fixtures even if not used in this module
-from nilearn.datasets._testing import request_mocker  # noqa: F401
-from nilearn.datasets._testing import temp_nilearn_data_dir  # noqa: F401
+from nilearn.datasets.tests._testing import request_mocker  # noqa: F401
+from nilearn.datasets.tests._testing import temp_nilearn_data_dir  # noqa: F401
 
 collect_ignore = ["datasets/data/convert_templates.py"]
 collect_ignore_glob = ["reporting/_visual_testing/*"]


Test patch:
diff --git a/nilearn/datasets/_testing.py b/nilearn/datasets/tests/_testing.py
similarity index 99%
rename from nilearn/datasets/_testing.py
rename to nilearn/datasets/tests/_testing.py
--- a/nilearn/datasets/_testing.py
+++ b/nilearn/datasets/tests/_testing.py
@@ -330,7 +330,7 @@ def _get_format_and_pattern(file_path):
 
 def _index_archive_contents():
     archive_contents_dir = (
-        Path(__file__).parent / "tests" / "data" / "archive_contents"
+        Path(__file__).parent.parent / "tests" / "data" / "archive_contents"
     )
     index = {}
     for file_path in sorted(archive_contents_dir.glob("**/*")):
diff --git a/nilearn/datasets/tests/data/list_archive_contents.sh b/nilearn/datasets/tests/data/list_archive_contents.sh
--- a/nilearn/datasets/tests/data/list_archive_contents.sh
+++ b/nilearn/datasets/tests/data/list_archive_contents.sh
@@ -6,7 +6,7 @@
 # unix system. The same result can easily be obtained manually: download the
 # archive, list its contents (one item per line), using '/' as the path
 # separator, add the url on the first line. See the docstring for
-# nilearn.datasets._testing.Sender for details.
+# nilearn.datasets._testing.tests.Sender for details.
 
 url=$1
 if [[ -z "$url" ]]; then
diff --git a/nilearn/datasets/tests/test_atlas.py b/nilearn/datasets/tests/test_atlas.py
--- a/nilearn/datasets/tests/test_atlas.py
+++ b/nilearn/datasets/tests/test_atlas.py
@@ -16,7 +16,7 @@
 from nilearn._utils import data_gen
 from nilearn._utils.testing import serialize_niimg
 from nilearn.datasets import atlas
-from nilearn.datasets._testing import dict_to_archive
+from nilearn.datasets.tests._testing import dict_to_archive
 from nilearn.datasets.utils import _fetch_files
 from nilearn.image import get_data
 
diff --git a/nilearn/datasets/tests/test_func.py b/nilearn/datasets/tests/test_func.py
--- a/nilearn/datasets/tests/test_func.py
+++ b/nilearn/datasets/tests/test_func.py
@@ -18,7 +18,7 @@
 import pytest
 
 from nilearn.datasets import func
-from nilearn.datasets._testing import dict_to_archive, list_to_archive
+from nilearn.datasets.tests._testing import dict_to_archive, list_to_archive
 from nilearn.datasets.utils import _get_dataset_dir
 
 
diff --git a/nilearn/datasets/tests/test_struct.py b/nilearn/datasets/tests/test_struct.py
--- a/nilearn/datasets/tests/test_struct.py
+++ b/nilearn/datasets/tests/test_struct.py
@@ -10,7 +10,7 @@
 from nibabel import Nifti1Image
 
 from nilearn.datasets import struct
-from nilearn.datasets._testing import dict_to_archive, list_to_archive
+from nilearn.datasets.tests._testing import dict_to_archive, list_to_archive
 
 
 def test_fetch_icbm152_2009(tmp_path, request_mocker):
diff --git a/nilearn/datasets/tests/test_testing.py b/nilearn/datasets/tests/test_testing.py
--- a/nilearn/datasets/tests/test_testing.py
+++ b/nilearn/datasets/tests/test_testing.py
@@ -8,7 +8,7 @@
 
 from nilearn import image
 from nilearn._utils.data_gen import generate_fake_fmri
-from nilearn.datasets import _testing
+from nilearn.datasets.tests import _testing
 
 
 def test_sender_key_order(request_mocker):
diff --git a/nilearn/decoding/tests/_utils.py b/nilearn/decoding/tests/_testing.py
similarity index 100%
rename from nilearn/decoding/tests/_utils.py
rename to nilearn/decoding/tests/_testing.py
diff --git a/nilearn/decoding/tests/test_graph_net.py b/nilearn/decoding/tests/test_graph_net.py
--- a/nilearn/decoding/tests/test_graph_net.py
+++ b/nilearn/decoding/tests/test_graph_net.py
@@ -20,7 +20,7 @@
     _squared_loss_derivative_lipschitz_constant,
     mfista,
 )
-from nilearn.decoding.tests._utils import create_graph_net_simulation_data
+from nilearn.decoding.tests._testing import create_graph_net_simulation_data
 
 from .test_same_api import to_niimgs
 
diff --git a/nilearn/decoding/tests/test_space_net.py b/nilearn/decoding/tests/test_space_net.py
--- a/nilearn/decoding/tests/test_space_net.py
+++ b/nilearn/decoding/tests/test_space_net.py
@@ -25,7 +25,7 @@
     _graph_net_logistic,
     _graph_net_squared_loss,
 )
-from nilearn.decoding.tests._utils import create_graph_net_simulation_data
+from nilearn.decoding.tests._testing import create_graph_net_simulation_data
 from nilearn.image import get_data
 
 logistic_path_scores = partial(path_scores, is_classif=True)
diff --git a/nilearn/glm/tests/_utils.py b/nilearn/glm/tests/_testing.py
similarity index 98%
rename from nilearn/glm/tests/_utils.py
rename to nilearn/glm/tests/_testing.py
--- a/nilearn/glm/tests/_utils.py
+++ b/nilearn/glm/tests/_testing.py
@@ -1,3 +1,5 @@
+"""Test utilities for glm module."""
+
 import numpy as np
 import pandas as pd
 
diff --git a/nilearn/glm/tests/test_dmtx.py b/nilearn/glm/tests/test_dmtx.py
--- a/nilearn/glm/tests/test_dmtx.py
+++ b/nilearn/glm/tests/test_dmtx.py
@@ -24,7 +24,7 @@
     make_second_level_design_matrix,
 )
 
-from ._utils import (
+from ._testing import (
     block_paradigm,
     design_with_negative_onsets,
     modulated_block_paradigm,
diff --git a/nilearn/glm/tests/test_paradigm.py b/nilearn/glm/tests/test_paradigm.py
--- a/nilearn/glm/tests/test_paradigm.py
+++ b/nilearn/glm/tests/test_paradigm.py
@@ -18,7 +18,7 @@
     handle_modulation_of_duplicate_events,
 )
 
-from ._utils import (
+from ._testing import (
     block_paradigm,
     design_with_nan_durations,
     design_with_nan_onsets,
diff --git a/nilearn/interfaces/fmriprep/tests/utils.py b/nilearn/interfaces/fmriprep/tests/_testing.py
similarity index 100%
rename from nilearn/interfaces/fmriprep/tests/utils.py
rename to nilearn/interfaces/fmriprep/tests/_testing.py
diff --git a/nilearn/interfaces/fmriprep/tests/test_load_confounds.py b/nilearn/interfaces/fmriprep/tests/test_load_confounds.py
--- a/nilearn/interfaces/fmriprep/tests/test_load_confounds.py
+++ b/nilearn/interfaces/fmriprep/tests/test_load_confounds.py
@@ -16,7 +16,7 @@
     _check_strategy,
     _load_single_confounds_file,
 )
-from nilearn.interfaces.fmriprep.tests.utils import (
+from nilearn.interfaces.fmriprep.tests._testing import (
     create_tmp_filepath,
     get_legal_confound,
 )
diff --git a/nilearn/interfaces/fmriprep/tests/test_load_confounds_components.py b/nilearn/interfaces/fmriprep/tests/test_load_confounds_components.py
--- a/nilearn/interfaces/fmriprep/tests/test_load_confounds_components.py
+++ b/nilearn/interfaces/fmriprep/tests/test_load_confounds_components.py
@@ -6,7 +6,7 @@
 from nilearn.interfaces.fmriprep.load_confounds_utils import (
     _load_confounds_json,
 )
-from nilearn.interfaces.fmriprep.tests.utils import create_tmp_filepath
+from nilearn.interfaces.fmriprep.tests._testing import create_tmp_filepath
 
 
 @pytest.fixture
diff --git a/nilearn/interfaces/fmriprep/tests/test_load_confounds_strategy.py b/nilearn/interfaces/fmriprep/tests/test_load_confounds_strategy.py
--- a/nilearn/interfaces/fmriprep/tests/test_load_confounds_strategy.py
+++ b/nilearn/interfaces/fmriprep/tests/test_load_confounds_strategy.py
@@ -7,7 +7,7 @@
 from nilearn.interfaces.fmriprep.load_confounds_strategy import (
     preset_strategies,
 )
-from nilearn.interfaces.fmriprep.tests.utils import create_tmp_filepath
+from nilearn.interfaces.fmriprep.tests._testing import create_tmp_filepath
 
 
 @pytest.mark.parametrize("denoise_strategy,image_type",
diff --git a/nilearn/interfaces/fmriprep/tests/test_load_confounds_utils.py b/nilearn/interfaces/fmriprep/tests/test_load_confounds_utils.py
--- a/nilearn/interfaces/fmriprep/tests/test_load_confounds_utils.py
+++ b/nilearn/interfaces/fmriprep/tests/test_load_confounds_utils.py
@@ -4,7 +4,7 @@
     _get_file_name,
     _sanitize_confounds,
 )
-from nilearn.interfaces.fmriprep.tests.utils import create_tmp_filepath
+from nilearn.interfaces.fmriprep.tests._testing import create_tmp_filepath
 
 
 @pytest.mark.parametrize(
diff --git a/nilearn/mass_univariate/tests/utils.py b/nilearn/mass_univariate/tests/_testing.py
similarity index 100%
rename from nilearn/mass_univariate/tests/utils.py
rename to nilearn/mass_univariate/tests/_testing.py
diff --git a/nilearn/mass_univariate/tests/test_permuted_least_squares.py b/nilearn/mass_univariate/tests/test_permuted_least_squares.py
--- a/nilearn/mass_univariate/tests/test_permuted_least_squares.py
+++ b/nilearn/mass_univariate/tests/test_permuted_least_squares.py
@@ -55,7 +55,7 @@ def compare_to_ref_score(own_score, tested_var, target_var, covars=None):
 
 def ref_score(tested_var, target_var, covars=None):
     """Compute t-scores with linalg or statsmodels."""
-    from nilearn.mass_univariate.tests.utils import (
+    from nilearn.mass_univariate.tests._testing import (
         get_tvalue_with_alternative_library,
     )
 
diff --git a/nilearn/mass_univariate/tests/test__utils.py b/nilearn/mass_univariate/tests/test_utils.py
similarity index 99%
rename from nilearn/mass_univariate/tests/test__utils.py
rename to nilearn/mass_univariate/tests/test_utils.py
--- a/nilearn/mass_univariate/tests/test__utils.py
+++ b/nilearn/mass_univariate/tests/test_utils.py
@@ -8,7 +8,7 @@
 from sklearn.utils import check_random_state
 
 from nilearn.mass_univariate import _utils
-from nilearn.mass_univariate.tests.utils import (
+from nilearn.mass_univariate.tests._testing import (
     get_tvalue_with_alternative_library,
 )
 
diff --git a/nilearn/plotting/tests/test_matrix_plotting.py b/nilearn/plotting/tests/test_matrix_plotting.py
--- a/nilearn/plotting/tests/test_matrix_plotting.py
+++ b/nilearn/plotting/tests/test_matrix_plotting.py
@@ -7,7 +7,7 @@
 from nilearn.glm.first_level.design_matrix import (
     make_first_level_design_matrix,
 )
-from nilearn.glm.tests._utils import design_with_null_durations
+from nilearn.glm.tests._testing import design_with_null_durations
 from nilearn.plotting.matrix_plotting import (
     plot_contrast_matrix,
     plot_design_matrix,
diff --git a/nilearn/plotting/tests/test_surf_plotting.py b/nilearn/plotting/tests/test_surf_plotting.py
--- a/nilearn/plotting/tests/test_surf_plotting.py
+++ b/nilearn/plotting/tests/test_surf_plotting.py
@@ -25,7 +25,7 @@
     plot_surf_stat_map,
 )
 from nilearn.surface import load_surf_data, load_surf_mesh
-from nilearn.surface.testing_utils import generate_surf
+from nilearn.surface.tests._testing import generate_surf
 
 try:
     import plotly.graph_objects as go
diff --git a/nilearn/surface/testing_utils.py b/nilearn/surface/tests/_testing.py
similarity index 100%
rename from nilearn/surface/testing_utils.py
rename to nilearn/surface/tests/_testing.py
diff --git a/nilearn/surface/tests/test_surface.py b/nilearn/surface/tests/test_surface.py
--- a/nilearn/surface/tests/test_surface.py
+++ b/nilearn/surface/tests/test_surface.py
@@ -26,7 +26,11 @@
     _gifti_img_to_mesh,
     _load_surf_files_gifti_gzip,
 )
-from nilearn.surface.testing_utils import flat_mesh, generate_surf, z_const_img
+from nilearn.surface.tests._testing import (
+    flat_mesh,
+    generate_surf,
+    z_const_img,
+)
 
 currdir = os.path.dirname(os.path.abspath(__file__))
 datadir = os.path.join(currdir, 'data')