Coverage for nilearn/tests/test_package_import.py: 0%

20 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-16 12:32 +0200

1"""Test related to importing nilearn optional dependencies are missing.""" 

2 

3import sys 

4 

5import pytest 

6 

7from nilearn._utils.helpers import is_matplotlib_installed 

8from nilearn._utils.testing import on_windows_with_old_mpl_and_new_numpy 

9 

10 

11@pytest.mark.skipif( 

12 on_windows_with_old_mpl_and_new_numpy(), 

13 reason="Old matplotlib not compatible with numpy 2.0 on windows.", 

14) 

15@pytest.mark.skipif( 

16 is_matplotlib_installed(), 

17 reason="This test should run only if matplotlib is not installed.", 

18) 

19def test_import_plotting_should_raise_warning_if_matplotlib_not_installed(): 

20 """Tests if importing nilearn.plotting displays correct warning and raises 

21 error when matplotlib is not installed. 

22 """ 

23 with ( 

24 pytest.raises( 

25 ModuleNotFoundError, match="No module named 'matplotlib'" 

26 ), 

27 pytest.warns( 

28 UserWarning, match="Some dependencies of nilearn.plotting" 

29 ), 

30 ): 

31 from nilearn.plotting import cm # noqa: F401 

32 

33 

34@pytest.mark.skipif( 

35 on_windows_with_old_mpl_and_new_numpy(), 

36 reason="Old matplotlib not compatible with numpy 2.0 on windows.", 

37) 

38@pytest.mark.skipif( 

39 is_matplotlib_installed(), 

40 reason="This test should run only if matplotlib is not installed.", 

41) 

42def test_import_reporting_should_raise_warning_if_matplotlib_not_installed(): 

43 """Tests if importing nilearn.reporting.make_glm_report displays correct 

44 warning and raises error when matplotlib is not installed. 

45 """ 

46 del sys.modules["nilearn.reporting"] 

47 with ( 

48 pytest.warns(UserWarning, match="nilearn.reporting.glm_reporter and"), 

49 pytest.raises( 

50 ImportError, 

51 match="cannot import name 'make_glm_report' from*", 

52 ), 

53 ): 

54 from nilearn.reporting import make_glm_report # noqa: F401 

55 

56 

57@pytest.mark.skipif( 

58 is_matplotlib_installed(), 

59 reason="This test should run only if matplotlib is not installed.", 

60) 

61def test_import_get_clusters_table_when_matplotlib_not_installed(): 

62 """Tests if nilearn.reporting.get_clusters_table can be imported without 

63 problems when matplotlib is not installed. 

64 """ 

65 del sys.modules["nilearn.reporting"] 

66 with pytest.warns(UserWarning, match="nilearn.reporting.glm_reporter and"): 

67 from nilearn.reporting import get_clusters_table # noqa: F401