Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
_pytesttester.py202 linesDownload Raw Back to numpy
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 development mode with
24``spin``, through the standard ``spin test`` invocation or from an inplace
25build with ``pytest numpy``.
26
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 os
32import sys
33
34__all__ = ['PytestTester']
35
36
37def _show_numpy_info():
38    import numpy as np
39
40    print(f"NumPy version {np.__version__}")
41    info = np.lib._utils_impl._opt_info()
42    print("NumPy CPU features: ", (info or 'nothing enabled'))
43
44
45class PytestTester:
46    """
47    Pytest test runner.
48
49    A test function is typically added to a package's __init__.py like so::
50
51      from numpy._pytesttester import PytestTester
52      test = PytestTester(__name__).test
53      del PytestTester
54
55    Calling this test function finds and runs all tests associated with the
56    module and all its sub-modules.
57
58    Attributes
59    ----------
60    module_name : str
61        Full path to the package to test.
62
63    Parameters
64    ----------
65    module_name : module name
66        The name of the module to test.
67
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.
73
74    """
75    def __init__(self, module_name):
76        self.module_name = module_name
77        self.__module__ = 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 warnings
127
128        import pytest
129
130        module = sys.modules[self.module_name]
131        module_path = os.path.abspath(module.__path__[0])
132
133        # setup the pytest arguments
134        pytest_args = ["-l"]
135
136        # offset verbosity. The "-q" cancels a "-v".
137        pytest_args += ["-q"]
138
139        if sys.version_info < (3, 12):
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  # noqa: F401
146
147        # Filter out annoying import messages. Want these in both develop and
148        # release mode.
149        pytest_args += [
150            "-W ignore:Not importing directory",
151            "-W ignore:numpy.dtype size changed",
152            "-W ignore:numpy.ufunc size changed",
153            "-W ignore::UserWarning:cpuinfo",
154            ]
155
156        # When testing matrices, ignore their PendingDeprecationWarnings
157        pytest_args += [
158            "-W ignore:the matrix subclass is not",
159            "-W ignore:Importing from numpy.matlib is",
160            ]
161
162        if doctests:
163            pytest_args += ["--doctest-modules"]
164
165        if extra_argv:
166            pytest_args += list(extra_argv)
167
168        if verbose > 1:
169            pytest_args += ["-" + "v" * (verbose - 1)]
170
171        if coverage:
172            pytest_args += ["--cov=" + module_path]
173
174        if label == "fast":
175            # not importing at the top level to avoid circular import of module
176            from numpy.testing import IS_PYPY
177            if IS_PYPY:
178                pytest_args += ["-m", "not slow and not slow_pypy"]
179            else:
180                pytest_args += ["-m", "not slow"]
181
182        elif label != "full":
183            pytest_args += ["-m", label]
184
185        if durations >= 0:
186            pytest_args += [f"--durations={durations}"]
187
188        if tests is None:
189            tests = [self.module_name]
190
191        pytest_args += ["--pyargs"] + list(tests)
192
193        # run tests.
194        _show_numpy_info()
195
196        try:
197            code = pytest.main(pytest_args)
198        except SystemExit as exc:
199            code = exc.code
200
201        return code == 0
202 
codekingpro/portable-devtools · Team Ai