Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/numpy/_pytesttester.py: 14%
53 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1"""
2Pytest test running.
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::
8 from numpy._pytesttester import PytestTester
9 test = PytestTester(__name__)
10 del PytestTester
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:
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.
23In practice, tests run from the numpy repo are run in develop mode. That
24includes the standard ``python runtests.py`` invocation.
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
33__all__ = ['PytestTester']
37def _show_numpy_info():
38 import numpy as np
40 print("NumPy version %s" % np.__version__)
41 relaxed_strides = np.ones((10, 1), order="C").flags.f_contiguous
42 print("NumPy relaxed strides checking option:", relaxed_strides)
43 info = np.lib.utils._opt_info()
44 print("NumPy CPU features: ", (info if info else 'nothing enabled'))
48class PytestTester:
49 """
50 Pytest test runner.
52 A test function is typically added to a package's __init__.py like so::
54 from numpy._pytesttester import PytestTester
55 test = PytestTester(__name__).test
56 del PytestTester
58 Calling this test function finds and runs all tests associated with the
59 module and all its sub-modules.
61 Attributes
62 ----------
63 module_name : str
64 Full path to the package to test.
66 Parameters
67 ----------
68 module_name : module name
69 The name of the module to test.
71 Notes
72 -----
73 Unlike the previous ``nose``-based implementation, this class is not
74 publicly exposed as it performs some ``numpy``-specific warning
75 suppression.
77 """
78 def __init__(self, module_name):
79 self.module_name = module_name
81 def __call__(self, label='fast', verbose=1, extra_argv=None,
82 doctests=False, coverage=False, durations=-1, tests=None):
83 """
84 Run tests for module using pytest.
86 Parameters
87 ----------
88 label : {'fast', 'full'}, optional
89 Identifies the tests to run. When set to 'fast', tests decorated
90 with `pytest.mark.slow` are skipped, when 'full', the slow marker
91 is ignored.
92 verbose : int, optional
93 Verbosity value for test outputs, in the range 1-3. Default is 1.
94 extra_argv : list, optional
95 List with any extra arguments to pass to pytests.
96 doctests : bool, optional
97 .. note:: Not supported
98 coverage : bool, optional
99 If True, report coverage of NumPy code. Default is False.
100 Requires installation of (pip) pytest-cov.
101 durations : int, optional
102 If < 0, do nothing, If 0, report time of all tests, if > 0,
103 report the time of the slowest `timer` tests. Default is -1.
104 tests : test or list of tests
105 Tests to be executed with pytest '--pyargs'
107 Returns
108 -------
109 result : bool
110 Return True on success, false otherwise.
112 Notes
113 -----
114 Each NumPy module exposes `test` in its namespace to run all tests for
115 it. For example, to run all tests for numpy.lib:
117 >>> np.lib.test() #doctest: +SKIP
119 Examples
120 --------
121 >>> result = np.lib.test() #doctest: +SKIP
122 ...
123 1023 passed, 2 skipped, 6 deselected, 1 xfailed in 10.39 seconds
124 >>> result
125 True
127 """
128 import pytest
129 import warnings
131 module = sys.modules[self.module_name]
132 module_path = os.path.abspath(module.__path__[0])
134 # setup the pytest arguments
135 pytest_args = ["-l"]
137 # offset verbosity. The "-q" cancels a "-v".
138 pytest_args += ["-q"]
140 with warnings.catch_warnings():
141 warnings.simplefilter("always")
142 # Filter out distutils cpu warnings (could be localized to
143 # distutils tests). ASV has problems with top level import,
144 # so fetch module for suppression here.
145 from numpy.distutils import cpuinfo
147 with warnings.catch_warnings(record=True):
148 # Ignore the warning from importing the array_api submodule. This
149 # warning is done on import, so it would break pytest collection,
150 # but importing it early here prevents the warning from being
151 # issued when it imported again.
152 import numpy.array_api
154 # Filter out annoying import messages. Want these in both develop and
155 # release mode.
156 pytest_args += [
157 "-W ignore:Not importing directory",
158 "-W ignore:numpy.dtype size changed",
159 "-W ignore:numpy.ufunc size changed",
160 "-W ignore::UserWarning:cpuinfo",
161 ]
163 # When testing matrices, ignore their PendingDeprecationWarnings
164 pytest_args += [
165 "-W ignore:the matrix subclass is not",
166 "-W ignore:Importing from numpy.matlib is",
167 ]
169 if doctests:
170 raise ValueError("Doctests not supported")
172 if extra_argv:
173 pytest_args += list(extra_argv)
175 if verbose > 1:
176 pytest_args += ["-" + "v"*(verbose - 1)]
178 if coverage:
179 pytest_args += ["--cov=" + module_path]
181 if label == "fast":
182 # not importing at the top level to avoid circular import of module
183 from numpy.testing import IS_PYPY
184 if IS_PYPY:
185 pytest_args += ["-m", "not slow and not slow_pypy"]
186 else:
187 pytest_args += ["-m", "not slow"]
189 elif label != "full":
190 pytest_args += ["-m", label]
192 if durations >= 0:
193 pytest_args += ["--durations=%s" % durations]
195 if tests is None:
196 tests = [self.module_name]
198 pytest_args += ["--pyargs"] + list(tests)
200 # run tests.
201 _show_numpy_info()
203 try:
204 code = pytest.main(pytest_args)
205 except SystemExit as exc:
206 code = exc.code
208 return code == 0