Skip to content

Commit ff922aa

Browse files
committed
RF: Reimplement nibabel.test() and nibabel.bench()
1 parent 1c6ea64 commit ff922aa

File tree

3 files changed

+102
-5
lines changed

3 files changed

+102
-5
lines changed

nibabel/__init__.py

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,105 @@
6969
from . import streamlines
7070
from . import viewers
7171

72-
from numpy.testing import Tester
73-
test = Tester().test
74-
bench = Tester().bench
75-
del Tester
76-
7772
from .pkg_info import get_pkg_info as _get_pkg_info
7873

7974

8075
def get_info():
8176
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+
try:
120+
verbose = int(verbose)
121+
except ValueError:
122+
pass
123+
else:
124+
if verbose > 0:
125+
args.append("-" + "v" * verbose)
126+
elif verbose < 0:
127+
args.append("-" + "q" * -verbose)
128+
129+
if extra_argv:
130+
args.extend(extra_argv)
131+
if doctests:
132+
args.append("--doctest-modules")
133+
if coverage:
134+
args.extend(["--cov", "nibabel"])
135+
if raise_warnings:
136+
raise NotImplementedError("Warning filters are not implemented")
137+
if timer:
138+
raise NotImplementedError("Timing is not implemented")
139+
140+
args.extend(["--pyargs", "nibabel"])
141+
142+
pytest.main(args=args)
143+
144+
145+
def bench(label=None, verbose=1, extra_argv=None):
146+
"""
147+
Run benchmarks for nibabel using pytest
148+
149+
The protocol mimics the ``numpy.testing.NoseTester.bench()``.
150+
Not all features are currently implemented.
151+
152+
Parameters
153+
----------
154+
label : None
155+
Unused.
156+
verbose: int, optional
157+
Verbosity value for test outputs. Positive values increase verbosity, and
158+
negative values decrease it. Default is 1.
159+
extra_argv : list, optional
160+
List with any extra arguments to pass to pytest.
161+
162+
Returns
163+
-------
164+
code : ExitCode
165+
Returns the result of running the tests as a ``pytest.ExitCode`` enum
166+
"""
167+
from pkg_resources import resource_filename
168+
config = resource_filename("nibabel", "benchmarks/pytest.benchmark.ini")
169+
args = []
170+
if extra_argv is not None:
171+
args.extend(extra_argv)
172+
args.extend(["-c", config])
173+
test(label, verbose, extra_argv=args)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pytest]
2+
python_files = bench_*.py
3+
python_functions = bench_*
4+
addopts = --capture=no

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ console_scripts =
8282
nibabel =
8383
tests/data/*
8484
*/tests/data/*
85+
benchmarks/pytest.benchmark.ini
8586

8687
[flake8]
8788
max-line-length = 100

0 commit comments

Comments
 (0)