1"""
2Pytest test running.
3
4This module implements the ``test()`` function for NumPy modules. The usual
5boiler plate for doing that is to put the following in the module
6``__init__.py`` file::
7
8 from numpy._pytesttester import PytestTester
9 test = PytestTester(__name__)
10 del PytestTester
11
12
13Warnings filtering and other runtime settings should be dealt with in the
14``pytest.ini`` file in the numpy repo root. The behavior of the test depends on
15whether or not that file is found as follows:
16
17* ``pytest.ini`` is present (develop mode)
18 All warnings except those explicitly filtered out are raised as error.
19* ``pytest.ini`` is absent (release mode)
20 DeprecationWarnings and PendingDeprecationWarnings are ignored, other
21 warnings are passed through.
22
23In practice, tests run from the numpy repo are run in develop mode. That
24includes the standard ``python runtests.py`` invocation.
25
26This module is imported by every numpy subpackage, so lies at the top level to
27simplify circular import issues. For the same reason, it contains no numpy
28imports at module scope, instead importing numpy within function calls.
29"""
30import sys
31import os
32
33__all__ = ['PytestTester']
34
35
36def _show_numpy_info():
37 import numpy as np
38
39 print("NumPy version %s" % np.__version__)
40 relaxed_strides = np.ones((10, 1), order="C").flags.f_contiguous
41 print("NumPy relaxed strides checking option:", relaxed_strides)
42 info = np.lib.utils._opt_info()
43 print("NumPy CPU features: ", (info if info else 'nothing enabled'))
44
45
46class PytestTester:
47 """
48 Pytest test runner.
49
50 A test function is typically added to a package's __init__.py like so::
51
52 from numpy._pytesttester import PytestTester
53 test = PytestTester(__name__).test
54 del PytestTester
55
56 Calling this test function finds and runs all tests associated with the
57 module and all its sub-modules.
58
59 Attributes
60 ----------
61 module_name : str
62 Full path to the package to test.
63
64 Parameters
65 ----------
66 module_name : module name
67 The name of the module to test.
68
69 Notes
70 -----
71 Unlike the previous ``nose``-based implementation, this class is not
72 publicly exposed as it performs some ``numpy``-specific warning
73 suppression.
74
75 """
76 def __init__(self, module_name):
77 self.module_name = module_name
78
79 def __call__(self, label='fast', verbose=1, extra_argv=None,
80 doctests=False, coverage=False, durations=-1, tests=None):
81 """
82 Run tests for module using pytest.
83
84 Parameters
85 ----------
86 label : {'fast', 'full'}, optional
87 Identifies the tests to run. When set to 'fast', tests decorated
88 with `pytest.mark.slow` are skipped, when 'full', the slow marker
89 is ignored.
90 verbose : int, optional
91 Verbosity value for test outputs, in the range 1-3. Default is 1.
92 extra_argv : list, optional
93 List with any extra arguments to pass to pytests.
94 doctests : bool, optional
95 .. note:: Not supported
96 coverage : bool, optional
97 If True, report coverage of NumPy code. Default is False.
98 Requires installation of (pip) pytest-cov.
99 durations : int, optional
100 If < 0, do nothing, If 0, report time of all tests, if > 0,
101 report the time of the slowest `timer` tests. Default is -1.
102 tests : test or list of tests
103 Tests to be executed with pytest '--pyargs'
104
105 Returns
106 -------
107 result : bool
108 Return True on success, false otherwise.
109
110 Notes
111 -----
112 Each NumPy module exposes `test` in its namespace to run all tests for
113 it. For example, to run all tests for numpy.lib:
114
115 >>> np.lib.test() #doctest: +SKIP
116
117 Examples
118 --------
119 >>> result = np.lib.test() #doctest: +SKIP
120 ...
121 1023 passed, 2 skipped, 6 deselected, 1 xfailed in 10.39 seconds
122 >>> result
123 True
124
125 """
126 import pytest
127 import warnings
128
129 module = sys.modules[self.module_name]
130 module_path = os.path.abspath(module.__path__[0])
131
132 # setup the pytest arguments
133 pytest_args = ["-l"]
134
135 # offset verbosity. The "-q" cancels a "-v".
136 pytest_args += ["-q"]
137
138 if sys.version_info < (3, 12):
139 with warnings.catch_warnings():
140 warnings.simplefilter("always")
141 # Filter out distutils cpu warnings (could be localized to
142 # distutils tests). ASV has problems with top level import,
143 # so fetch module for suppression here.
144 from numpy.distutils import cpuinfo
145
146 with warnings.catch_warnings(record=True):
147 # Ignore the warning from importing the array_api submodule. This
148 # warning is done on import, so it would break pytest collection,
149 # but importing it early here prevents the warning from being
150 # issued when it imported again.
151 import numpy.array_api
152
153 # Filter out annoying import messages. Want these in both develop and
154 # release mode.
155 pytest_args += [
156 "-W ignore:Not importing directory",
157 "-W ignore:numpy.dtype size changed",
158 "-W ignore:numpy.ufunc size changed",
159 "-W ignore::UserWarning:cpuinfo",
160 ]
161
162 # When testing matrices, ignore their PendingDeprecationWarnings
163 pytest_args += [
164 "-W ignore:the matrix subclass is not",
165 "-W ignore:Importing from numpy.matlib is",
166 ]
167
168 if doctests:
169 pytest_args += ["--doctest-modules"]
170
171 if extra_argv:
172 pytest_args += list(extra_argv)
173
174 if verbose > 1:
175 pytest_args += ["-" + "v"*(verbose - 1)]
176
177 if coverage:
178 pytest_args += ["--cov=" + module_path]
179
180 if label == "fast":
181 # not importing at the top level to avoid circular import of module
182 from numpy.testing import IS_PYPY
183 if IS_PYPY:
184 pytest_args += ["-m", "not slow and not slow_pypy"]
185 else:
186 pytest_args += ["-m", "not slow"]
187
188 elif label != "full":
189 pytest_args += ["-m", label]
190
191 if durations >= 0:
192 pytest_args += ["--durations=%s" % durations]
193
194 if tests is None:
195 tests = [self.module_name]
196
197 pytest_args += ["--pyargs"] + list(tests)
198
199 # run tests.
200 _show_numpy_info()
201
202 try:
203 code = pytest.main(pytest_args)
204 except SystemExit as exc:
205 code = exc.code
206
207 return code == 0