|
| 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 pywt._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 pywt 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 PyWavelets repo are run in develop mode. That |
| 24 | +includes the standard ``python runtests.py`` invocation. |
| 25 | +
|
| 26 | +""" |
| 27 | +from __future__ import division, absolute_import, print_function |
| 28 | + |
| 29 | +import sys |
| 30 | +import os |
| 31 | + |
| 32 | +__all__ = ['PytestTester'] |
| 33 | + |
| 34 | + |
| 35 | +def _show_pywt_info(): |
| 36 | + import pywt |
| 37 | + from pywt._c99_config import _have_c99_complex |
| 38 | + print("PyWavelets version %s" % pywt.__version__) |
| 39 | + if _have_c99_complex: |
| 40 | + print("Compiled with C99 complex support.") |
| 41 | + else: |
| 42 | + print("Compiled without C99 complex support.") |
| 43 | + |
| 44 | + |
| 45 | +class PytestTester(object): |
| 46 | + """ |
| 47 | + Pytest test runner. |
| 48 | +
|
| 49 | + This class is made available in ``pywt.testing``, and a test function |
| 50 | + is typically added to a package's __init__.py like so:: |
| 51 | +
|
| 52 | + from pywt.testing 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 | + """ |
| 70 | + def __init__(self, module_name): |
| 71 | + self.module_name = module_name |
| 72 | + |
| 73 | + def __call__(self, label='fast', verbose=1, extra_argv=None, |
| 74 | + doctests=False, coverage=False, durations=-1, tests=None): |
| 75 | + """ |
| 76 | + Run tests for module using pytest. |
| 77 | +
|
| 78 | + Parameters |
| 79 | + ---------- |
| 80 | + label : {'fast', 'full'}, optional |
| 81 | + Identifies the tests to run. When set to 'fast', tests decorated |
| 82 | + with `pytest.mark.slow` are skipped, when 'full', the slow marker |
| 83 | + is ignored. |
| 84 | + verbose : int, optional |
| 85 | + Verbosity value for test outputs, in the range 1-3. Default is 1. |
| 86 | + extra_argv : list, optional |
| 87 | + List with any extra arguments to pass to pytests. |
| 88 | + doctests : bool, optional |
| 89 | + .. note:: Not supported |
| 90 | + coverage : bool, optional |
| 91 | + If True, report coverage of NumPy code. Default is False. |
| 92 | + Requires installation of (pip) pytest-cov. |
| 93 | + durations : int, optional |
| 94 | + If < 0, do nothing, If 0, report time of all tests, if > 0, |
| 95 | + report the time of the slowest `timer` tests. Default is -1. |
| 96 | + tests : test or list of tests |
| 97 | + Tests to be executed with pytest '--pyargs' |
| 98 | +
|
| 99 | + Returns |
| 100 | + ------- |
| 101 | + result : bool |
| 102 | + Return True on success, false otherwise. |
| 103 | +
|
| 104 | + Examples |
| 105 | + -------- |
| 106 | + >>> result = np.lib.test() #doctest: +SKIP |
| 107 | + ... |
| 108 | + 1023 passed, 2 skipped, 6 deselected, 1 xfailed in 10.39 seconds |
| 109 | + >>> result |
| 110 | + True |
| 111 | +
|
| 112 | + """ |
| 113 | + import pytest |
| 114 | + |
| 115 | + module = sys.modules[self.module_name] |
| 116 | + module_path = os.path.abspath(module.__path__[0]) |
| 117 | + |
| 118 | + # setup the pytest arguments |
| 119 | + pytest_args = ["-l"] |
| 120 | + |
| 121 | + # offset verbosity. The "-q" cancels a "-v". |
| 122 | + pytest_args += ["-q"] |
| 123 | + |
| 124 | + # Filter out annoying import messages. Want these in both develop and |
| 125 | + # release mode. |
| 126 | + pytest_args += [ |
| 127 | + "-W ignore:Not importing directory", |
| 128 | + "-W ignore:numpy.dtype size changed", |
| 129 | + "-W ignore:numpy.ufunc size changed", ] |
| 130 | + |
| 131 | + if doctests: |
| 132 | + raise ValueError("Doctests not supported") |
| 133 | + |
| 134 | + if extra_argv: |
| 135 | + pytest_args += list(extra_argv) |
| 136 | + |
| 137 | + if verbose > 1: |
| 138 | + pytest_args += ["-" + "v"*(verbose - 1)] |
| 139 | + |
| 140 | + if coverage: |
| 141 | + pytest_args += ["--cov=" + module_path] |
| 142 | + |
| 143 | + if label == "fast": |
| 144 | + pytest_args += ["-m", "not slow"] |
| 145 | + elif label != "full": |
| 146 | + pytest_args += ["-m", label] |
| 147 | + |
| 148 | + if durations >= 0: |
| 149 | + pytest_args += ["--durations=%s" % durations] |
| 150 | + |
| 151 | + if tests is None: |
| 152 | + tests = [self.module_name] |
| 153 | + |
| 154 | + pytest_args += ["--pyargs"] + list(tests) |
| 155 | + |
| 156 | + # run tests. |
| 157 | + _show_pywt_info() |
| 158 | + |
| 159 | + try: |
| 160 | + code = pytest.main(pytest_args) |
| 161 | + except SystemExit as exc: |
| 162 | + code = exc.code |
| 163 | + |
| 164 | + return code == 0 |
0 commit comments