Skip to content

Commit 5f4436f

Browse files
authored
Merge pull request #876 from djarecka/pytest_updates
updates for nibabel.tests
2 parents 51f2485 + 6838af6 commit 5f4436f

35 files changed

+689
-697
lines changed

.azure-pipelines/windows.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ jobs:
4949
-I test_batteryrunners ^
5050
-I test_brikhead ^
5151
-I test_casting ^
52+
-I test_cifti2io_axes ^
5253
-I test_cifti2io_header ^
5354
-I test_data ^
5455
-I test_deprecated ^
5556
-I test_deprecator ^
57+
-I test_dicomwrappers ^
5658
-I test_dft ^
5759
-I test_ecat ^
5860
-I test_ecat_data ^
@@ -75,6 +77,7 @@ jobs:
7577
-I test_image_types ^
7678
-I test_imageclasses ^
7779
-I test_imageglobals ^
80+
-I test_io ^
7881
-I test_keywordonly ^
7982
-I test_loadsave ^
8083
-I test_minc1 ^
@@ -99,6 +102,9 @@ jobs:
99102
-I test_round_trip ^
100103
-I test_rstutils ^
101104
-I test_scaling ^
105+
-I test_scripts ^
106+
-I test_spaces ^
107+
-I test_testing ^
102108
-I test_wrapstruct
103109
displayName: 'Nose tests'
104110
condition: and(succeeded(), eq(variables['CHECK_TYPE'], 'nosetests'))

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,12 @@ script:
142142
-I test_batteryrunners \
143143
-I test_brikhead \
144144
-I test_casting \
145+
-I test_cifti2io_axes \
145146
-I test_cifti2io_header \
146147
-I test_data \
147148
-I test_deprecated \
148149
-I test_deprecator \
150+
-I test_dicomwrappers \
149151
-I test_dft \
150152
-I test_ecat \
151153
-I test_ecat_data \
@@ -168,6 +170,7 @@ script:
168170
-I test_image_types \
169171
-I test_imageclasses \
170172
-I test_imageglobals \
173+
-I test_io \
171174
-I test_keywordonly \
172175
-I test_loadsave \
173176
-I test_minc1 \
@@ -192,6 +195,9 @@ script:
192195
-I test_round_trip \
193196
-I test_rstutils \
194197
-I test_scaling \
198+
-I test_scripts \
199+
-I test_spaces \
200+
-I test_testing \
195201
-I test_wrapstruct
196202
elif [ "${CHECK_TYPE}" == "test" ]; then
197203
# Change into an innocuous directory and find tests from installation

nibabel/tests/nibabel_data.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from os import environ, listdir
55
from os.path import dirname, realpath, join as pjoin, isdir, exists
66

7-
from ..testing import skipif
7+
import pytest
88

99

1010
def get_nibabel_data():
@@ -39,11 +39,11 @@ def needs_nibabel_data(subdir=None):
3939
"""
4040
nibabel_data = get_nibabel_data()
4141
if nibabel_data == '':
42-
return skipif(True, "Need nibabel-data directory for this test")
42+
return pytest.mark.skipif(True, reason="Need nibabel-data directory for this test")
4343
if subdir is None:
44-
return skipif(False)
44+
return pytest.mark.skipif(False, reason="Don't skip")
4545
required_path = pjoin(nibabel_data, subdir)
4646
# Path should not be empty (as is the case for not-updated submodules)
4747
have_files = exists(required_path) and len(listdir(required_path)) > 0
48-
return skipif(not have_files,
49-
"Need files in {0} for these tests".format(required_path))
48+
return pytest.mark.skipif(not have_files,
49+
reason="Need files in {0} for these tests".format(required_path))

nibabel/tests/scriptrunner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def run_command(self, cmd, check_code=True):
135135
env['PYTHONPATH'] = self.local_module_dir + pathsep + pypath
136136
proc = Popen(cmd, stdout=PIPE, stderr=PIPE, env=env)
137137
stdout, stderr = proc.communicate()
138-
if proc.poll() == None:
138+
if proc.poll() is None:
139139
proc.terminate()
140140
if check_code and proc.returncode != 0:
141141
raise RuntimeError(

nibabel/tests/test_analyze.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ def test_from_header(self):
512512
for check in (True, False):
513513
copy = klass.from_header(hdr, check=check)
514514
assert hdr == copy
515-
assert not hdr is copy
515+
assert hdr is not copy
516516

517517
class C(object):
518518

nibabel/tests/test_arraywriters.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,9 @@ def test_nan2zero():
508508
assert_array_equal(np.isnan(data_back), [True, False])
509509
# Deprecation warning for nan2zero as argument to `to_fileobj`
510510
with error_warnings():
511-
with pytest.raises(DeprecationWarning):
511+
with pytest.deprecated_call():
512512
aw.to_fileobj(BytesIO(), 'F', True)
513-
with pytest.raises(DeprecationWarning):
513+
with pytest.deprecated_call():
514514
aw.to_fileobj(BytesIO(), 'F', nan2zero=True)
515515
# Error if nan2zero is not the value set at initialization
516516
with pytest.raises(WriterError):
@@ -530,9 +530,9 @@ def test_nan2zero():
530530
assert_array_equal(data_back, [astype_res, 99])
531531
# Deprecation warning for nan2zero as argument to `to_fileobj`
532532
with error_warnings():
533-
with pytest.raises(DeprecationWarning):
533+
with pytest.deprecated_call():
534534
aw.to_fileobj(BytesIO(), 'F', False)
535-
with pytest.raises(DeprecationWarning):
535+
with pytest.deprecated_call():
536536
aw.to_fileobj(BytesIO(), 'F', nan2zero=False)
537537
# Error if nan2zero is not the value set at initialization
538538
with pytest.raises(WriterError):

nibabel/tests/test_casting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def test_floor_log2():
155155
assert floor_log2(0.75) == -1
156156
assert floor_log2(0.25) == -2
157157
assert floor_log2(0.24) == -3
158-
assert floor_log2(0) == None
158+
assert floor_log2(0) is None
159159

160160

161161
def test_able_int_type():

nibabel/tests/test_deprecator.py

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def test__add_dep_doc():
3434
assert _add_dep_doc('bar\n\n', 'foo') == 'bar\n\nfoo\n'
3535
assert _add_dep_doc('bar\n \n', 'foo') == 'bar\n\nfoo\n'
3636
assert (_add_dep_doc(' bar\n\nSome explanation', 'foo\nbaz') ==
37-
' bar\n\nfoo\nbaz\n\nSome explanation\n')
37+
' bar\n\nfoo\nbaz\n\nSome explanation\n')
3838
assert (_add_dep_doc(' bar\n\n Some explanation', 'foo\nbaz') ==
39-
' bar\n \n foo\n baz\n \n Some explanation\n')
39+
' bar\n \n foo\n baz\n \n Some explanation\n')
4040

4141

4242
class CustomError(Exception):
@@ -69,36 +69,29 @@ def test_dep_func(self):
6969
# Test function deprecation
7070
dec = self.dep_func
7171
func = dec('foo')(func_no_doc)
72-
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
73-
warnings.simplefilter('always')
74-
assert func() == None
75-
assert len(w) == 1
76-
assert w[0].category is DeprecationWarning
72+
with pytest.deprecated_call():
73+
assert func() is None
7774
assert func.__doc__ == 'foo\n'
7875
func = dec('foo')(func_doc)
79-
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
80-
warnings.simplefilter('always')
81-
assert func(1) == None
76+
with pytest.deprecated_call() as w:
77+
assert func(1) is None
8278
assert len(w) == 1
8379
assert func.__doc__ == 'A docstring\n\nfoo\n'
8480
func = dec('foo')(func_doc_long)
85-
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
86-
warnings.simplefilter('always')
87-
assert func(1, 2) == None
81+
with pytest.deprecated_call() as w:
82+
assert func(1, 2) is None
8883
assert len(w) == 1
8984
assert func.__doc__ == 'A docstring\n \n foo\n \n Some text\n'
9085

9186
# Try some since and until versions
9287
func = dec('foo', '1.1')(func_no_doc)
9388
assert func.__doc__ == 'foo\n\n* deprecated from version: 1.1\n'
94-
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
95-
warnings.simplefilter('always')
96-
assert func() == None
89+
with pytest.deprecated_call() as w:
90+
assert func() is None
9791
assert len(w) == 1
9892
func = dec('foo', until='99.4')(func_no_doc)
99-
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
100-
warnings.simplefilter('always')
101-
assert func() == None
93+
with pytest.deprecated_call() as w:
94+
assert func() is None
10295
assert len(w) == 1
10396
assert (func.__doc__ ==
10497
'foo\n\n* Will raise {} as of version: 99.4\n'
@@ -107,39 +100,36 @@ def test_dep_func(self):
107100
with pytest.raises(ExpiredDeprecationError):
108101
func()
109102
assert (func.__doc__ ==
110-
'foo\n\n* Raises {} as of version: 1.8\n'
111-
.format(ExpiredDeprecationError))
103+
'foo\n\n* Raises {} as of version: 1.8\n'
104+
.format(ExpiredDeprecationError))
112105
func = dec('foo', '1.2', '1.8')(func_no_doc)
113106
with pytest.raises(ExpiredDeprecationError):
114107
func()
115108
assert (func.__doc__ ==
116-
'foo\n\n* deprecated from version: 1.2\n'
117-
'* Raises {} as of version: 1.8\n'
118-
.format(ExpiredDeprecationError))
109+
'foo\n\n* deprecated from version: 1.2\n'
110+
'* Raises {} as of version: 1.8\n'
111+
.format(ExpiredDeprecationError))
119112
func = dec('foo', '1.2', '1.8')(func_doc_long)
120113
assert (func.__doc__ ==
121-
'A docstring\n \n foo\n \n'
122-
' * deprecated from version: 1.2\n'
123-
' * Raises {} as of version: 1.8\n \n'
124-
' Some text\n'
125-
.format(ExpiredDeprecationError))
114+
'A docstring\n \n foo\n \n'
115+
' * deprecated from version: 1.2\n'
116+
' * Raises {} as of version: 1.8\n \n'
117+
' Some text\n'
118+
.format(ExpiredDeprecationError))
126119
with pytest.raises(ExpiredDeprecationError):
127120
func()
128121

129122
# Check different warnings and errors
130123
func = dec('foo', warn_class=UserWarning)(func_no_doc)
131124
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
132125
warnings.simplefilter('always')
133-
assert func() == None
126+
assert func() is None
134127
assert len(w) == 1
135128
assert w[0].category is UserWarning
136129

137130
func = dec('foo', error_class=CustomError)(func_no_doc)
138-
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
139-
warnings.simplefilter('always')
140-
assert func() == None
141-
assert len(w) == 1
142-
assert w[0].category is DeprecationWarning
131+
with pytest.deprecated_call():
132+
assert func() is None
143133

144134
func = dec('foo', until='1.8', error_class=CustomError)(func_no_doc)
145135
with pytest.raises(CustomError):
@@ -154,19 +144,15 @@ class TestDeprecatorMaker(object):
154144
def test_deprecator_maker(self):
155145
dec = self.dep_maker(warn_class=UserWarning)
156146
func = dec('foo')(func_no_doc)
157-
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
158-
warnings.simplefilter('always')
159-
assert func() == None
147+
with pytest.warns(UserWarning) as w:
148+
# warnings.simplefilter('always')
149+
assert func() is None
160150
assert len(w) == 1
161-
assert w[0].category is UserWarning
162151

163152
dec = self.dep_maker(error_class=CustomError)
164153
func = dec('foo')(func_no_doc)
165-
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
166-
warnings.simplefilter('always')
167-
assert func() == None
168-
assert len(w) == 1
169-
assert w[0].category is DeprecationWarning
154+
with pytest.deprecated_call():
155+
assert func() is None
170156

171157
func = dec('foo', until='1.8')(func_no_doc)
172158
with pytest.raises(CustomError):

nibabel/tests/test_ecat.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from numpy.testing import assert_array_equal, assert_array_almost_equal
2323

24-
from ..testing_pytest import data_path, suppress_warnings, clear_and_catch_warnings
24+
from ..testing_pytest import data_path, suppress_warnings
2525
from ..tmpdirs import InTemporaryDirectory
2626

2727
from .test_wrapstruct import _TestWrapStructBase
@@ -164,7 +164,7 @@ def test_subheader(self):
164164
assert self.subhdr.get_nframes() == 1
165165
assert (self.subhdr.get_nframes() ==
166166
len(self.subhdr.subheaders))
167-
assert self.subhdr._check_affines() == True
167+
assert self.subhdr._check_affines() is True
168168
assert_array_almost_equal(np.diag(self.subhdr.get_frame_affine()),
169169
np.array([2.20241979, 2.20241979, 3.125, 1.]))
170170
assert self.subhdr.get_zooms()[0] == 2.20241978764534
@@ -271,8 +271,7 @@ def test_mlist_regression(self):
271271

272272
def test_from_filespec_deprecation():
273273
# Check from_filespec raises Deprecation
274-
with clear_and_catch_warnings() as w:
275-
warnings.simplefilter('always', DeprecationWarning)
274+
with pytest.deprecated_call() as w:
276275
# No warning for standard load
277276
img_loaded = EcatImage.load(ecat_file)
278277
assert len(w) == 0

nibabel/tests/test_environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ def test_sys_dir():
6060
elif os.name == 'posix':
6161
assert sys_dir == r'/etc/nipy'
6262
else:
63-
assert sys_dir == None
63+
assert sys_dir is None

nibabel/tests/test_files_interface.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def test_files_spatialimages():
2929
for klass in klasses:
3030
file_map = klass.make_file_map()
3131
for key, value in file_map.items():
32-
assert value.filename == None
33-
assert value.fileobj == None
32+
assert value.filename is None
33+
assert value.fileobj is None
3434
assert value.pos == 0
3535
# If we can't create new images in memory without loading, bail here
3636
if not klass.makeable:
@@ -42,8 +42,8 @@ def test_files_spatialimages():
4242
else:
4343
img = klass(arr, aff)
4444
for key, value in img.file_map.items():
45-
assert value.filename == None
46-
assert value.fileobj == None
45+
assert value.filename is None
46+
assert value.fileobj is None
4747
assert value.pos == 0
4848

4949

nibabel/tests/test_fileslice.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_is_fancy():
4747
assert not is_fancy((None, 1))
4848
assert not is_fancy((1, None))
4949
# Chack that actual False returned (rather than falsey)
50-
assert is_fancy(1) == False
50+
assert is_fancy(1) is False
5151

5252

5353
def test_canonical_slicers():
@@ -243,9 +243,9 @@ def test_threshold_heuristic():
243243
# Test for default skip / read heuristic
244244
# int
245245
assert threshold_heuristic(1, 9, 1, skip_thresh=8) == 'full'
246-
assert threshold_heuristic(1, 9, 1, skip_thresh=7) == None
246+
assert threshold_heuristic(1, 9, 1, skip_thresh=7) is None
247247
assert threshold_heuristic(1, 9, 2, skip_thresh=16) == 'full'
248-
assert threshold_heuristic(1, 9, 2, skip_thresh=15) == None
248+
assert threshold_heuristic(1, 9, 2, skip_thresh=15) is None
249249
# full slice, smallest step size
250250
assert (threshold_heuristic(
251251
slice(0, 9, 1), 9, 2, skip_thresh=2) ==

nibabel/tests/test_funcs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def test_closest_canonical():
138138
# And a case where the Analyze image has to be flipped
139139
img = AnalyzeImage(arr, np.diag([-1, 1, 1, 1]))
140140
xyz_img = as_closest_canonical(img)
141-
assert not img is xyz_img
141+
assert img is not xyz_img
142142
out_arr = xyz_img.get_fdata()
143143
assert_array_equal(out_arr, np.flipud(arr))
144144

@@ -156,7 +156,7 @@ def test_closest_canonical():
156156
img = Nifti1Image(arr, np.diag([-1, 1, 1, 1]))
157157
img.header.set_dim_info(0, 1, 2)
158158
xyz_img = as_closest_canonical(img)
159-
assert not img is xyz_img
159+
assert img is not xyz_img
160160
assert img.header.get_dim_info() == xyz_img.header.get_dim_info()
161161
out_arr = xyz_img.get_fdata()
162162
assert_array_equal(out_arr, np.flipud(arr))
@@ -181,7 +181,7 @@ def test_closest_canonical():
181181
img.header.set_dim_info(0, 1, 2)
182182

183183
xyz_img = as_closest_canonical(img)
184-
assert not img is xyz_img
184+
assert img is not xyz_img
185185
# Check both the original and new objects
186186
assert img.header.get_dim_info() == (0, 1, 2)
187187
assert xyz_img.header.get_dim_info() == (0, 2, 1)

0 commit comments

Comments
 (0)