|
| 1 | +""" |
| 2 | +Pytest test running. |
| 3 | +
|
| 4 | +This module implements the ``test()`` function for NumPy modules. The usual |
| 5 | +boiler 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__).test |
| 10 | + del PytestTester |
| 11 | +
|
| 12 | +
|
| 13 | +Warnings 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 |
| 15 | +whether or not that file is found as follows: |
| 16 | +
|
| 17 | +* ``pytest.ini`` is present (develop mode) |
| 18 | + All warnings except those explicily 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 | +
|
| 23 | +In practice, tests run from the numpy repo are run in develop mode. That |
| 24 | +includes the standard ``python runtests.py`` invocation. |
| 25 | +
|
| 26 | +This module is imported by every numpy subpackage, so lies at the top level to |
| 27 | +simplify circular import issues. For the same reason, it contains no numpy |
| 28 | +imports at module scope, instead importing numpy within function calls. |
| 29 | +""" |
| 30 | +from __future__ import division, absolute_import, print_function |
| 31 | + |
| 32 | +import sys |
| 33 | +import os |
| 34 | + |
| 35 | +__all__ = ['PytestTester'] |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +def _show_numpy_info(): |
| 40 | + import numpy as np |
| 41 | + |
| 42 | + print("NumPy version %s" % np.__version__) |
| 43 | + relaxed_strides = np.ones((10, 1), order="C").flags.f_contiguous |
| 44 | + print("NumPy relaxed strides checking option:", relaxed_strides) |
| 45 | + |
| 46 | + |
| 47 | +class PytestTester(object): |
| 48 | + """ |
| 49 | + Pytest test runner. |
| 50 | +
|
| 51 | + This class is made available in ``numpy.testing``, and a test function |
| 52 | + is typically added to a package's __init__.py like so:: |
| 53 | +
|
| 54 | + from numpy.testing import PytestTester |
| 55 | + test = PytestTester(__name__).test |
| 56 | + del PytestTester |
| 57 | +
|
| 58 | + Calling this test function finds and runs all tests associated with the |
| 59 | + module and all its sub-modules. |
| 60 | +
|
| 61 | + Attributes |
| 62 | + ---------- |
| 63 | + module_name : str |
| 64 | + Full path to the package to test. |
| 65 | +
|
| 66 | + Parameters |
| 67 | + ---------- |
| 68 | + module_name : module name |
| 69 | + The name of the module to test. |
| 70 | +
|
| 71 | + """ |
| 72 | + def __init__(self, module_name): |
| 73 | + self.module_name = module_name |
| 74 | + |
| 75 | + def __call__(self, label='fast', verbose=1, extra_argv=None, |
| 76 | + doctests=False, coverage=False, durations=-1, tests=None): |
| 77 | + """ |
| 78 | + Run tests for module using pytest. |
| 79 | +
|
| 80 | + Parameters |
| 81 | + ---------- |
| 82 | + label : {'fast', 'full'}, optional |
| 83 | + Identifies the tests to run. When set to 'fast', tests decorated |
| 84 | + with `pytest.mark.slow` are skipped, when 'full', the slow marker |
| 85 | + is ignored. |
| 86 | + verbose : int, optional |
| 87 | + Verbosity value for test outputs, in the range 1-3. Default is 1. |
| 88 | + extra_argv : list, optional |
| 89 | + List with any extra arguments to pass to pytests. |
| 90 | + doctests : bool, optional |
| 91 | + .. note:: Not supported |
| 92 | + coverage : bool, optional |
| 93 | + If True, report coverage of NumPy code. Default is False. |
| 94 | + Requires installation of (pip) pytest-cov. |
| 95 | + durations : int, optional |
| 96 | + If < 0, do nothing, If 0, report time of all tests, if > 0, |
| 97 | + report the time of the slowest `timer` tests. Default is -1. |
| 98 | + tests : test or list of tests |
| 99 | + Tests to be executed with pytest '--pyargs' |
| 100 | +
|
| 101 | + Returns |
| 102 | + ------- |
| 103 | + result : bool |
| 104 | + Return True on success, false otherwise. |
| 105 | +
|
| 106 | + Notes |
| 107 | + ----- |
| 108 | + Each NumPy module exposes `test` in its namespace to run all tests for |
| 109 | + it. For example, to run all tests for numpy.lib: |
| 110 | +
|
| 111 | + >>> np.lib.test() #doctest: +SKIP |
| 112 | +
|
| 113 | + Examples |
| 114 | + -------- |
| 115 | + >>> result = np.lib.test() #doctest: +SKIP |
| 116 | + ... |
| 117 | + 1023 passed, 2 skipped, 6 deselected, 1 xfailed in 10.39 seconds |
| 118 | + >>> result |
| 119 | + True |
| 120 | +
|
| 121 | + """ |
| 122 | + import pytest |
| 123 | + import warnings |
| 124 | + |
| 125 | + #FIXME This is no longer needed? Assume it was for use in tests. |
| 126 | + # cap verbosity at 3, which is equivalent to the pytest '-vv' option |
| 127 | + #from . import utils |
| 128 | + #verbose = min(int(verbose), 3) |
| 129 | + #utils.verbose = verbose |
| 130 | + # |
| 131 | + |
| 132 | + module = sys.modules[self.module_name] |
| 133 | + module_path = os.path.abspath(module.__path__[0]) |
| 134 | + |
| 135 | + # setup the pytest arguments |
| 136 | + pytest_args = ["-l"] |
| 137 | + |
| 138 | + # offset verbosity. The "-q" cancels a "-v". |
| 139 | + pytest_args += ["-q"] |
| 140 | + |
| 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 | + with warnings.catch_warnings(): |
| 145 | + warnings.simplefilter("always") |
| 146 | + from numpy.distutils import cpuinfo |
| 147 | + |
| 148 | + # Filter out annoying import messages. Want these in both develop and |
| 149 | + # release mode. |
| 150 | + pytest_args += [ |
| 151 | + "-W ignore:Not importing directory", |
| 152 | + "-W ignore:numpy.dtype size changed", |
| 153 | + "-W ignore:numpy.ufunc size changed", |
| 154 | + "-W ignore::UserWarning:cpuinfo", |
| 155 | + ] |
| 156 | + |
| 157 | + # When testing matrices, ignore their PendingDeprecationWarnings |
| 158 | + pytest_args += [ |
| 159 | + "-W ignore:the matrix subclass is not", |
| 160 | + ] |
| 161 | + |
| 162 | + # Ignore python2.7 -3 warnings |
| 163 | + pytest_args += [ |
| 164 | + r"-W ignore:sys\.exc_clear\(\) not supported in 3\.x:DeprecationWarning", |
| 165 | + r"-W ignore:in 3\.x, __setslice__:DeprecationWarning", |
| 166 | + r"-W ignore:in 3\.x, __getslice__:DeprecationWarning", |
| 167 | + r"-W ignore:buffer\(\) not supported in 3\.x:DeprecationWarning", |
| 168 | + r"-W ignore:CObject type is not supported in 3\.x:DeprecationWarning", |
| 169 | + r"-W ignore:comparing unequal types not supported in 3\.x:DeprecationWarning", |
| 170 | + r"-W ignore:the commands module has been removed in Python 3\.0:DeprecationWarning", |
| 171 | + r"-W ignore:The 'new' module has been removed in Python 3\.0:DeprecationWarning", |
| 172 | + ] |
| 173 | + |
| 174 | + |
| 175 | + if doctests: |
| 176 | + raise ValueError("Doctests not supported") |
| 177 | + |
| 178 | + if extra_argv: |
| 179 | + pytest_args += list(extra_argv) |
| 180 | + |
| 181 | + if verbose > 1: |
| 182 | + pytest_args += ["-" + "v"*(verbose - 1)] |
| 183 | + |
| 184 | + if coverage: |
| 185 | + pytest_args += ["--cov=" + module_path] |
| 186 | + |
| 187 | + if label == "fast": |
| 188 | + pytest_args += ["-m", "not slow"] |
| 189 | + elif label != "full": |
| 190 | + pytest_args += ["-m", label] |
| 191 | + |
| 192 | + if durations >= 0: |
| 193 | + pytest_args += ["--durations=%s" % durations] |
| 194 | + |
| 195 | + if tests is None: |
| 196 | + tests = [self.module_name] |
| 197 | + |
| 198 | + pytest_args += ["--pyargs"] + list(tests) |
| 199 | + |
| 200 | + |
| 201 | + # run tests. |
| 202 | + _show_numpy_info() |
| 203 | + |
| 204 | + try: |
| 205 | + code = pytest.main(pytest_args) |
| 206 | + except SystemExit as exc: |
| 207 | + code = exc.code |
| 208 | + |
| 209 | + return code == 0 |
0 commit comments