|
36 | 36 | For more detailed information see the :ref:`manual`.
|
37 | 37 | """
|
38 | 38 |
|
39 |
| -# Package-wide test setup and teardown |
40 |
| -_test_states = { |
41 |
| - # Numpy changed print options in 1.14; we can update docstrings and remove |
42 |
| - # these when our minimum for building docs exceeds that |
43 |
| - 'legacy_printopt': None, |
44 |
| - } |
45 |
| - |
46 |
| -def setup_package(): |
47 |
| - """ Set numpy print style to legacy="1.13" for newer versions of numpy """ |
48 |
| - import numpy as np |
49 |
| - from distutils.version import LooseVersion |
50 |
| - if LooseVersion(np.__version__) >= LooseVersion('1.14'): |
51 |
| - if _test_states.get('legacy_printopt') is None: |
52 |
| - _test_states['legacy_printopt'] = np.get_printoptions().get('legacy') |
53 |
| - np.set_printoptions(legacy="1.13") |
54 |
| - |
55 |
| -def teardown_package(): |
56 |
| - """ Reset print options when tests finish """ |
57 |
| - import numpy as np |
58 |
| - if _test_states.get('legacy_printopt') is not None: |
59 |
| - np.set_printoptions(legacy=_test_states.pop('legacy_printopt')) |
60 |
| - |
61 |
| - |
62 | 39 | # module imports
|
63 | 40 | from . import analyze as ana
|
64 | 41 | from . import spm99analyze as spm99
|
@@ -92,13 +69,101 @@ def teardown_package():
|
92 | 69 | from . import streamlines
|
93 | 70 | from . import viewers
|
94 | 71 |
|
95 |
| -from numpy.testing import Tester |
96 |
| -test = Tester().test |
97 |
| -bench = Tester().bench |
98 |
| -del Tester |
99 |
| - |
100 | 72 | from .pkg_info import get_pkg_info as _get_pkg_info
|
101 | 73 |
|
102 | 74 |
|
103 | 75 | def get_info():
|
104 | 76 | return _get_pkg_info(os.path.dirname(__file__))
|
| 77 | + |
| 78 | + |
| 79 | +def test(label=None, verbose=1, extra_argv=None, |
| 80 | + doctests=False, coverage=False, raise_warnings=None, |
| 81 | + timer=False): |
| 82 | + """ |
| 83 | + Run tests for nibabel using pytest |
| 84 | +
|
| 85 | + The protocol mimics the ``numpy.testing.NoseTester.test()``. |
| 86 | + Not all features are currently implemented. |
| 87 | +
|
| 88 | + Parameters |
| 89 | + ---------- |
| 90 | + label : None |
| 91 | + Unused. |
| 92 | + verbose: int, optional |
| 93 | + Verbosity value for test outputs. Positive values increase verbosity, and |
| 94 | + negative values decrease it. Default is 1. |
| 95 | + extra_argv : list, optional |
| 96 | + List with any extra arguments to pass to pytest. |
| 97 | + doctests: bool, optional |
| 98 | + If True, run doctests in module. Default is False. |
| 99 | + coverage: bool, optional |
| 100 | + If True, report coverage of NumPy code. Default is False. |
| 101 | + (This requires the |
| 102 | + `coverage module <https://nedbatchelder.com/code/modules/coveragehtml>`_). |
| 103 | + raise_warnings : None |
| 104 | + Unused. |
| 105 | + timer : False |
| 106 | + Unused. |
| 107 | +
|
| 108 | + Returns |
| 109 | + ------- |
| 110 | + code : ExitCode |
| 111 | + Returns the result of running the tests as a ``pytest.ExitCode`` enum |
| 112 | + """ |
| 113 | + import pytest |
| 114 | + args = [] |
| 115 | + |
| 116 | + if label is not None: |
| 117 | + raise NotImplementedError("Labels cannot be set at present") |
| 118 | + |
| 119 | + verbose = int(verbose) |
| 120 | + if verbose > 0: |
| 121 | + args.append("-" + "v" * verbose) |
| 122 | + elif verbose < 0: |
| 123 | + args.append("-" + "q" * -verbose) |
| 124 | + |
| 125 | + if extra_argv: |
| 126 | + args.extend(extra_argv) |
| 127 | + if doctests: |
| 128 | + args.append("--doctest-modules") |
| 129 | + if coverage: |
| 130 | + args.extend(["--cov", "nibabel"]) |
| 131 | + if raise_warnings is not None: |
| 132 | + raise NotImplementedError("Warning filters are not implemented") |
| 133 | + if timer: |
| 134 | + raise NotImplementedError("Timing is not implemented") |
| 135 | + |
| 136 | + args.extend(["--pyargs", "nibabel"]) |
| 137 | + |
| 138 | + return pytest.main(args=args) |
| 139 | + |
| 140 | + |
| 141 | +def bench(label=None, verbose=1, extra_argv=None): |
| 142 | + """ |
| 143 | + Run benchmarks for nibabel using pytest |
| 144 | +
|
| 145 | + The protocol mimics the ``numpy.testing.NoseTester.bench()``. |
| 146 | + Not all features are currently implemented. |
| 147 | +
|
| 148 | + Parameters |
| 149 | + ---------- |
| 150 | + label : None |
| 151 | + Unused. |
| 152 | + verbose: int, optional |
| 153 | + Verbosity value for test outputs. Positive values increase verbosity, and |
| 154 | + negative values decrease it. Default is 1. |
| 155 | + extra_argv : list, optional |
| 156 | + List with any extra arguments to pass to pytest. |
| 157 | +
|
| 158 | + Returns |
| 159 | + ------- |
| 160 | + code : ExitCode |
| 161 | + Returns the result of running the tests as a ``pytest.ExitCode`` enum |
| 162 | + """ |
| 163 | + from pkg_resources import resource_filename |
| 164 | + config = resource_filename("nibabel", "benchmarks/pytest.benchmark.ini") |
| 165 | + args = [] |
| 166 | + if extra_argv is not None: |
| 167 | + args.extend(extra_argv) |
| 168 | + args.extend(["-c", config]) |
| 169 | + return test(label, verbose, extra_argv=args) |
0 commit comments