Skip to content

Commit d9298ed

Browse files
authored
Merge pull request #865 from nipy/pytest
TEST: Drop nose for pytest
2 parents 23f3875 + 5657d01 commit d9298ed

File tree

120 files changed

+4963
-5203
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+4963
-5203
lines changed

.azure-pipelines/windows.yml

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
variables:
1212
EXTRA_WHEELS: "https://5cf40426d9f06eb7461d-6fe47d9331aba7cd62fc36c7196769e4.ssl.cf2.rackcdn.com"
1313
DEPENDS: numpy scipy matplotlib h5py pydicom
14+
CHECK_TYPE: test
1415
strategy:
1516
matrix:
1617
${{ insert }}: ${{ parameters.matrix }}
@@ -29,19 +30,20 @@ jobs:
2930
displayName: 'Update build tools'
3031
- script: |
3132
python -m pip install --find-links %EXTRA_WHEELS% %DEPENDS%
32-
python -m pip install nose mock coverage codecov pytest
3333
displayName: 'Install dependencies'
3434
- script: |
35-
python -m pip install .
35+
python -m pip install .[$(CHECK_TYPE)]
3636
SET NIBABEL_DATA_DIR=%CD%\\nibabel-data
3737
displayName: 'Install nibabel'
3838
- script: |
3939
mkdir for_testing
4040
cd for_testing
4141
cp ../.coveragerc .
42-
nosetests --with-doctest --with-coverage --cover-package nibabel nibabel
43-
displayName: 'Nose tests'
42+
pytest --doctest-modules --cov nibabel -v --pyargs nibabel
43+
displayName: 'Pytest tests'
44+
condition: and(succeeded(), eq(variables['CHECK_TYPE'], 'test'))
4445
- script: |
46+
python -m pip install codecov
4547
cd for_testing
4648
codecov
4749
displayName: 'Upload To Codecov'

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@ doc/source/reference
8585
venv/
8686
.buildbot.patch
8787
.vscode
88+
for_testing/

.travis.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
# for it to be on multiple physical lines, so long as you remember: - There
55
# can't be any leading "-"s - All newlines will be removed, so use ";"s
66

7+
os: linux
78
dist: xenial
8-
sudo: true
99
language: python
10-
1110
cache: pip
11+
1212
env:
1313
global:
1414
- SETUP_REQUIRES="pip setuptools>=30.3.0 wheel"
@@ -25,7 +25,7 @@ python:
2525
- 3.7
2626
- 3.8
2727

28-
matrix:
28+
jobs:
2929
include:
3030
# Basic dependencies only
3131
- python: 3.5
@@ -143,7 +143,7 @@ script:
143143
mkdir for_testing
144144
cd for_testing
145145
cp ../.coveragerc .
146-
nosetests --with-doctest --with-coverage --cover-package nibabel nibabel
146+
pytest --doctest-modules --cov nibabel -v --pyargs nibabel
147147
else
148148
false
149149
fi

dev-requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Requirements for running tests
22
-r requirements.txt
3-
nose
3+
pytest

doc/source/devel/advanced_testing.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Long-running tests
2525
Long-running tests are not enabled by default, and can be resource-intensive. To run these tests:
2626

2727
* Set environment variable ``NIPY_EXTRA_TESTS=slow``;
28-
* Run ``nosetests``.
28+
* Run ``pytest nibabel``.
2929

3030
Note that some tests may require a machine with >4GB of RAM.
3131

doc/source/devel/make_release.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Release checklist
7979

8080
* Make sure all tests pass (from the nibabel root directory)::
8181

82-
nosetests --with-doctest nibabel
82+
pytest --doctest-modules nibabel
8383

8484
* Make sure you are set up to use the ``try_branch.py`` - see
8585
https://github.com/nipy/nibotmi/blob/master/install.rst#trying-a-set-of-changes-on-the-buildbots

doc/source/installation.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Requirements
9090
* h5py_ (optional, for MINC2 support)
9191
* PyDICOM_ 0.9.9 or greater (optional, for DICOM support)
9292
* `Python Imaging Library`_ (optional, for PNG conversion in DICOMFS)
93-
* nose_ 0.11 or greater and pytest_ (optional, to run the tests)
93+
* pytest_ (optional, to run the tests)
9494
* sphinx_ (optional, to build the documentation)
9595

9696
Get the development sources
@@ -128,7 +128,7 @@ module to see if everything is fine. It should look something like this::
128128
>>>
129129

130130

131-
To run the nibabel test suite, from the terminal run ``nosetests nibabel`` or
131+
To run the nibabel test suite, from the terminal run ``pytest nibabel`` or
132132
``python -c "import nibabel; nibabel.test()``.
133133

134134
To run an extended test suite that validates ``nibabel`` for long-running and

nibabel/__init__.py

+93-28
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,6 @@
3636
For more detailed information see the :ref:`manual`.
3737
"""
3838

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-
6239
# module imports
6340
from . import analyze as ana
6441
from . import spm99analyze as spm99
@@ -92,13 +69,101 @@ def teardown_package():
9269
from . import streamlines
9370
from . import viewers
9471

95-
from numpy.testing import Tester
96-
test = Tester().test
97-
bench = Tester().bench
98-
del Tester
99-
10072
from .pkg_info import get_pkg_info as _get_pkg_info
10173

10274

10375
def get_info():
10476
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)

nibabel/benchmarks/bench_array_to_file.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
import nibabel as nib
66
nib.bench()
77
8-
If you have doctests enabled by default in nose (with a noserc file or
9-
environment variable), and you have a numpy version <= 1.6.1, this will also
10-
run the doctests, let's hope they pass.
8+
Run this benchmark with::
119
12-
Run this benchmark with:
13-
14-
nosetests -s --match '(?:^|[\\b_\\.//-])[Bb]ench' /path/to/bench_load_save.py
10+
pytest -c <path>/benchmarks/pytest.benchmark.ini <path>/benchmarks/bench_array_to_file.py
1511
"""
1612

1713
import sys

nibabel/benchmarks/bench_arrayproxy_slicing.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
import nibabel as nib
66
nib.bench()
77
8-
If you have doctests enabled by default in nose (with a noserc file or
9-
environment variable), and you have a numpy version <= 1.6.1, this will also
10-
run the doctests, let's hope they pass.
8+
Run this benchmark with::
119
12-
Run this benchmark with:
13-
14-
nosetests -s --match '(?:^|[\\b_\\.//-])[Bb]ench' /path/to/bench_arrayproxy_slicing.py
10+
pytest -c <path>/benchmarks/pytest.benchmark.ini <path>/benchmarks/bench_arrayproxy_slicing.py
1511
"""
1612

1713
from timeit import timeit

nibabel/benchmarks/bench_fileslice.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
import nibabel as nib
44
nib.bench()
55
6-
If you have doctests enabled by default in nose (with a noserc file or
7-
environment variable), and you have a numpy version <= 1.6.1, this will also
8-
run the doctests, let's hope they pass.
6+
Run this benchmark with::
97
10-
Run this benchmark with:
11-
12-
nosetests -s --match '(?:^|[\\b_\\.//-])[Bb]ench' /path/to/bench_fileslice.py
8+
pytest -c <path>/benchmarks/pytest.benchmark.ini <path>/benchmarks/bench_fileslice.py
139
"""
1410

1511
import sys

nibabel/benchmarks/bench_finite_range.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
import nibabel as nib
66
nib.bench()
77
8-
If you have doctests enabled by default in nose (with a noserc file or
9-
environment variable), and you have a numpy version <= 1.6.1, this will also
10-
run the doctests, let's hope they pass.
8+
Run this benchmark with::
119
12-
Run this benchmark with:
13-
14-
nosetests -s --match '(?:^|[\\b_\\.//-])[Bb]ench' /path/to/bench_finite_range
10+
pytest -c <path>/benchmarks/pytest.benchmark.ini <path>/benchmarks/bench_finite_range.py
1511
"""
1612

1713
import sys

nibabel/benchmarks/bench_load_save.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
import nibabel as nib
66
nib.bench()
77
8-
If you have doctests enabled by default in nose (with a noserc file or
9-
environment variable), and you have a numpy version <= 1.6.1, this will also
10-
run the doctests, let's hope they pass.
8+
Run this benchmark with::
119
12-
Run this benchmark with:
13-
14-
nosetests -s --match '(?:^|[\\b_\\.//-])[Bb]ench' /path/to/bench_load_save.py
10+
pytest -c <path>/benchmarks/pytest.benchmark.ini <path>/benchmarks/bench_load_save.py
1511
"""
1612

1713
import sys

nibabel/benchmarks/bench_streamlines.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
import nibabel as nib
66
nib.bench()
77
8-
If you have doctests enabled by default in nose (with a noserc file or
9-
environment variable), and you have a numpy version <= 1.6.1, this will also run
10-
the doctests, let's hope they pass.
8+
Run this benchmark with::
119
12-
Run this benchmark with:
13-
14-
nosetests -s --match '(?:^|[\\b_\\.//-])[Bb]ench' /path/to/bench_streamlines.py
10+
pytest -c <path>/benchmarks/pytest.benchmark.ini <path>/benchmarks/bench_streamlines.py
1511
"""
1612

1713
import numpy as np
+4
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

0 commit comments

Comments
 (0)