Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/numpy/_pytesttester.py: 16%
50 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-09 06:12 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-09 06:12 +0000
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 development mode with
24``spin``, through the standard ``spin test`` invocation or from an inplace
25build with ``pytest numpy``.
27This module is imported by every numpy subpackage, so lies at the top level to
28simplify circular import issues. For the same reason, it contains no numpy
29imports at module scope, instead importing numpy within function calls.
30"""
31import sys
32import os
34__all__ = ['PytestTester']
37def _show_numpy_info():
38 import numpy as np
40 print("NumPy version %s" % np.__version__)
41 info = np.lib._utils_impl._opt_info()
42 print("NumPy CPU features: ", (info if info else 'nothing enabled'))
45class PytestTester:
46 """
47 Pytest test runner.
49 A test function is typically added to a package's __init__.py like so::
51 from numpy._pytesttester import PytestTester
52 test = PytestTester(__name__).test
53 del PytestTester
55 Calling this test function finds and runs all tests associated with the
56 module and all its sub-modules.
58 Attributes
59 ----------
60 module_name : str
61 Full path to the package to test.
63 Parameters
64 ----------
65 module_name : module name
66 The name of the module to test.
68 Notes
69 -----
70 Unlike the previous ``nose``-based implementation, this class is not
71 publicly exposed as it performs some ``numpy``-specific warning
72 suppression.
74 """
75 def __init__(self, module_name):
76 self.module_name = module_name
78 def __call__(self, label='fast', verbose=1, extra_argv=None,
79 doctests=False, coverage=False, durations=-1, tests=None):
80 """
81 Run tests for module using pytest.
83 Parameters
84 ----------
85 label : {'fast', 'full'}, optional
86 Identifies the tests to run. When set to 'fast', tests decorated
87 with `pytest.mark.slow` are skipped, when 'full', the slow marker
88 is ignored.
89 verbose : int, optional
90 Verbosity value for test outputs, in the range 1-3. Default is 1.
91 extra_argv : list, optional
92 List with any extra arguments to pass to pytests.
93 doctests : bool, optional
94 .. note:: Not supported
95 coverage : bool, optional
96 If True, report coverage of NumPy code. Default is False.
97 Requires installation of (pip) pytest-cov.
98 durations : int, optional
99 If < 0, do nothing, If 0, report time of all tests, if > 0,
100 report the time of the slowest `timer` tests. Default is -1.
101 tests : test or list of tests
102 Tests to be executed with pytest '--pyargs'
104 Returns
105 -------
106 result : bool
107 Return True on success, false otherwise.
109 Notes
110 -----
111 Each NumPy module exposes `test` in its namespace to run all tests for
112 it. For example, to run all tests for numpy.lib:
114 >>> np.lib.test() #doctest: +SKIP
116 Examples
117 --------
118 >>> result = np.lib.test() #doctest: +SKIP
119 ...
120 1023 passed, 2 skipped, 6 deselected, 1 xfailed in 10.39 seconds
121 >>> result
122 True
124 """
125 import pytest
126 import warnings
128 module = sys.modules[self.module_name]
129 module_path = os.path.abspath(module.__path__[0])
131 # setup the pytest arguments
132 pytest_args = ["-l"]
134 # offset verbosity. The "-q" cancels a "-v".
135 pytest_args += ["-q"]
137 if sys.version_info < (3, 12):
138 with warnings.catch_warnings():
139 warnings.simplefilter("always")
140 # Filter out distutils cpu warnings (could be localized to
141 # distutils tests). ASV has problems with top level import,
142 # so fetch module for suppression here.
143 from numpy.distutils import cpuinfo
145 # Filter out annoying import messages. Want these in both develop and
146 # release mode.
147 pytest_args += [
148 "-W ignore:Not importing directory",
149 "-W ignore:numpy.dtype size changed",
150 "-W ignore:numpy.ufunc size changed",
151 "-W ignore::UserWarning:cpuinfo",
152 ]
154 # When testing matrices, ignore their PendingDeprecationWarnings
155 pytest_args += [
156 "-W ignore:the matrix subclass is not",
157 "-W ignore:Importing from numpy.matlib is",
158 ]
160 if doctests:
161 pytest_args += ["--doctest-modules"]
163 if extra_argv:
164 pytest_args += list(extra_argv)
166 if verbose > 1:
167 pytest_args += ["-" + "v"*(verbose - 1)]
169 if coverage:
170 pytest_args += ["--cov=" + module_path]
172 if label == "fast":
173 # not importing at the top level to avoid circular import of module
174 from numpy.testing import IS_PYPY
175 if IS_PYPY:
176 pytest_args += ["-m", "not slow and not slow_pypy"]
177 else:
178 pytest_args += ["-m", "not slow"]
180 elif label != "full":
181 pytest_args += ["-m", label]
183 if durations >= 0:
184 pytest_args += ["--durations=%s" % durations]
186 if tests is None:
187 tests = [self.module_name]
189 pytest_args += ["--pyargs"] + list(tests)
191 # run tests.
192 _show_numpy_info()
194 try:
195 code = pytest.main(pytest_args)
196 except SystemExit as exc:
197 code = exc.code
199 return code == 0