Skip to content

TEST: Convert remainder of nibabel.tests to pytest #876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 79 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
5fdeed9
converting more tests from nibabel.tests
djarecka Feb 4, 2020
1a435fa
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
d85e79c
adding todo
djarecka Feb 4, 2020
9e6cac1
converting more tests nibabel.tests.test_s*
djarecka Feb 4, 2020
393f630
converting more tests nibabel.tests.test_t*
djarecka Feb 4, 2020
7aaa2d0
converting test_volumeutils
djarecka Feb 4, 2020
4ee8855
converting nibabel_data
djarecka Feb 4, 2020
a170497
removing todos
djarecka Feb 4, 2020
ff3cb80
Apply suggestions from code review
djarecka Feb 5, 2020
e87d3a5
Apply suggestions from code review
djarecka Feb 5, 2020
f2931ab
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
af8b386
adding todo
djarecka Feb 4, 2020
06210d5
removing todos
djarecka Feb 4, 2020
dd42dd8
small edits: suggestions from reviews
djarecka Feb 5, 2020
e55940d
ignoring tests with pytest.mark.parametrize
djarecka Feb 5, 2020
1e2caf4
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
b1fe921
adding todo
djarecka Feb 4, 2020
31c1698
converting more tests nibabel.tests.test_s*
djarecka Feb 4, 2020
e099c29
removing todos
djarecka Feb 4, 2020
264d1fa
Apply suggestions from code review
djarecka Feb 5, 2020
dd0c521
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
159956e
adding todo
djarecka Feb 4, 2020
e0da084
removing todos
djarecka Feb 4, 2020
0519f36
small edits: suggestions from reviews
djarecka Feb 5, 2020
f4f48ef
applying suggestions from 865
djarecka Feb 5, 2020
c5ccb2e
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
5122e2d
adding todo
djarecka Feb 4, 2020
a67a9c1
converting more tests nibabel.tests.test_s*
djarecka Feb 4, 2020
7013d72
removing todos
djarecka Feb 4, 2020
857bed6
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
1a24525
adding todo
djarecka Feb 4, 2020
fd2ddde
removing todos
djarecka Feb 4, 2020
920b30f
small edits: suggestions from reviews
djarecka Feb 5, 2020
f331d20
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
9751784
adding todo
djarecka Feb 4, 2020
29da7b0
converting more tests nibabel.tests.test_s*
djarecka Feb 4, 2020
f42da04
removing todos
djarecka Feb 4, 2020
dff77ba
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
61bc714
adding todo
djarecka Feb 4, 2020
338b4c3
removing todos
djarecka Feb 4, 2020
2d799c4
small edits: suggestions from reviews
djarecka Feb 5, 2020
436404f
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
831e553
adding todo
djarecka Feb 4, 2020
099e5cf
converting more tests nibabel.tests.test_s*
djarecka Feb 4, 2020
91be3bf
converting nibabel_data
djarecka Feb 4, 2020
4becc96
removing todos
djarecka Feb 4, 2020
ddb21c3
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
5ca8a74
adding todo
djarecka Feb 4, 2020
9373292
removing todos
djarecka Feb 4, 2020
20dcc69
small edits: suggestions from reviews
djarecka Feb 5, 2020
f6f5e04
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
13bacaf
adding todo
djarecka Feb 4, 2020
525b99d
converting more tests nibabel.tests.test_s*
djarecka Feb 4, 2020
82c39a1
removing todos
djarecka Feb 4, 2020
ff899a3
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
9daf10f
adding todo
djarecka Feb 4, 2020
b1d8cfb
removing todos
djarecka Feb 4, 2020
75f2ce6
small edits: suggestions from reviews
djarecka Feb 5, 2020
683a3ee
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
39c011e
adding todo
djarecka Feb 4, 2020
d5e5907
converting more tests nibabel.tests.test_s*
djarecka Feb 4, 2020
f7b49f3
removing todos
djarecka Feb 4, 2020
cdf6e18
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
8ba8134
adding todo
djarecka Feb 4, 2020
e860669
removing todos
djarecka Feb 4, 2020
32c1c17
small edits: suggestions from reviews
djarecka Feb 5, 2020
8a9f53d
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
50e7d98
adding todo
djarecka Feb 4, 2020
d4f2044
converting more tests nibabel.tests.test_s*
djarecka Feb 4, 2020
547ba92
removing todos
djarecka Feb 4, 2020
d8a15af
converting nibabel/tests/test_scripts.py
djarecka Feb 4, 2020
4e32787
adding todo
djarecka Feb 4, 2020
59af0aa
removing todos
djarecka Feb 4, 2020
a21c083
small edits: suggestions from reviews
djarecka Feb 5, 2020
f79de1d
ignoring tests that use needs_nibabel_data
djarecka Feb 6, 2020
2539bcd
some cleaning after using nose2pytest
djarecka Feb 6, 2020
a9bbe82
small cleaning after review
djarecka Feb 6, 2020
19b55b9
small edit
djarecka Feb 6, 2020
6838af6
Update nibabel/tests/test_image_api.py
djarecka Feb 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .azure-pipelines/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ jobs:
-I test_batteryrunners ^
-I test_brikhead ^
-I test_casting ^
-I test_cifti2io_axes ^
-I test_cifti2io_header ^
-I test_data ^
-I test_deprecated ^
-I test_deprecator ^
-I test_dicomwrappers ^
-I test_dft ^
-I test_ecat ^
-I test_ecat_data ^
Expand All @@ -75,6 +77,7 @@ jobs:
-I test_image_types ^
-I test_imageclasses ^
-I test_imageglobals ^
-I test_io ^
-I test_keywordonly ^
-I test_loadsave ^
-I test_minc1 ^
Expand All @@ -99,6 +102,9 @@ jobs:
-I test_round_trip ^
-I test_rstutils ^
-I test_scaling ^
-I test_scripts ^
-I test_spaces ^
-I test_testing ^
-I test_wrapstruct
displayName: 'Nose tests'
condition: and(succeeded(), eq(variables['CHECK_TYPE'], 'nosetests'))
Expand Down
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,12 @@ script:
-I test_batteryrunners \
-I test_brikhead \
-I test_casting \
-I test_cifti2io_axes \
-I test_cifti2io_header \
-I test_data \
-I test_deprecated \
-I test_deprecator \
-I test_dicomwrappers \
-I test_dft \
-I test_ecat \
-I test_ecat_data \
Expand All @@ -162,6 +164,7 @@ script:
-I test_image_types \
-I test_imageclasses \
-I test_imageglobals \
-I test_io \
-I test_keywordonly \
-I test_loadsave \
-I test_minc1 \
Expand All @@ -186,6 +189,9 @@ script:
-I test_round_trip \
-I test_rstutils \
-I test_scaling \
-I test_scripts \
-I test_spaces \
-I test_testing \
-I test_wrapstruct
elif [ "${CHECK_TYPE}" == "test" ]; then
# Change into an innocuous directory and find tests from installation
Expand Down
10 changes: 5 additions & 5 deletions nibabel/tests/nibabel_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from os import environ, listdir
from os.path import dirname, realpath, join as pjoin, isdir, exists

from ..testing import skipif
import pytest


def get_nibabel_data():
Expand Down Expand Up @@ -39,11 +39,11 @@ def needs_nibabel_data(subdir=None):
"""
nibabel_data = get_nibabel_data()
if nibabel_data == '':
return skipif(True, "Need nibabel-data directory for this test")
return pytest.mark.skipif(True, reason="Need nibabel-data directory for this test")
if subdir is None:
return skipif(False)
return pytest.mark.skipif(False, reason="Don't skip")
required_path = pjoin(nibabel_data, subdir)
# Path should not be empty (as is the case for not-updated submodules)
have_files = exists(required_path) and len(listdir(required_path)) > 0
return skipif(not have_files,
"Need files in {0} for these tests".format(required_path))
return pytest.mark.skipif(not have_files,
reason="Need files in {0} for these tests".format(required_path))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is now a pytest.mark.skipif, nose isn't skipping these. You should grep for @needs_nibabel_data, and any files that use it should be added to the ignore list.

2 changes: 1 addition & 1 deletion nibabel/tests/scriptrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def run_command(self, cmd, check_code=True):
env['PYTHONPATH'] = self.local_module_dir + pathsep + pypath
proc = Popen(cmd, stdout=PIPE, stderr=PIPE, env=env)
stdout, stderr = proc.communicate()
if proc.poll() == None:
if proc.poll() is None:
proc.terminate()
if check_code and proc.returncode != 0:
raise RuntimeError(
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def test_from_header(self):
for check in (True, False):
copy = klass.from_header(hdr, check=check)
assert hdr == copy
assert not hdr is copy
assert hdr is not copy

class C(object):

Expand Down
8 changes: 4 additions & 4 deletions nibabel/tests/test_arraywriters.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,9 @@ def test_nan2zero():
assert_array_equal(np.isnan(data_back), [True, False])
# Deprecation warning for nan2zero as argument to `to_fileobj`
with error_warnings():
with pytest.raises(DeprecationWarning):
with pytest.deprecated_call():
aw.to_fileobj(BytesIO(), 'F', True)
with pytest.raises(DeprecationWarning):
with pytest.deprecated_call():
aw.to_fileobj(BytesIO(), 'F', nan2zero=True)
# Error if nan2zero is not the value set at initialization
with pytest.raises(WriterError):
Expand All @@ -530,9 +530,9 @@ def test_nan2zero():
assert_array_equal(data_back, [astype_res, 99])
# Deprecation warning for nan2zero as argument to `to_fileobj`
with error_warnings():
with pytest.raises(DeprecationWarning):
with pytest.deprecated_call():
aw.to_fileobj(BytesIO(), 'F', False)
with pytest.raises(DeprecationWarning):
with pytest.deprecated_call():
aw.to_fileobj(BytesIO(), 'F', nan2zero=False)
# Error if nan2zero is not the value set at initialization
with pytest.raises(WriterError):
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_casting.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_floor_log2():
assert floor_log2(0.75) == -1
assert floor_log2(0.25) == -2
assert floor_log2(0.24) == -3
assert floor_log2(0) == None
assert floor_log2(0) is None


def test_able_int_type():
Expand Down
74 changes: 30 additions & 44 deletions nibabel/tests/test_deprecator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def test__add_dep_doc():
assert _add_dep_doc('bar\n\n', 'foo') == 'bar\n\nfoo\n'
assert _add_dep_doc('bar\n \n', 'foo') == 'bar\n\nfoo\n'
assert (_add_dep_doc(' bar\n\nSome explanation', 'foo\nbaz') ==
' bar\n\nfoo\nbaz\n\nSome explanation\n')
' bar\n\nfoo\nbaz\n\nSome explanation\n')
assert (_add_dep_doc(' bar\n\n Some explanation', 'foo\nbaz') ==
' bar\n \n foo\n baz\n \n Some explanation\n')
' bar\n \n foo\n baz\n \n Some explanation\n')


class CustomError(Exception):
Expand Down Expand Up @@ -69,36 +69,29 @@ def test_dep_func(self):
# Test function deprecation
dec = self.dep_func
func = dec('foo')(func_no_doc)
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
warnings.simplefilter('always')
assert func() == None
assert len(w) == 1
assert w[0].category is DeprecationWarning
with pytest.deprecated_call():
assert func() is None
assert func.__doc__ == 'foo\n'
func = dec('foo')(func_doc)
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
warnings.simplefilter('always')
assert func(1) == None
with pytest.deprecated_call() as w:
assert func(1) is None
assert len(w) == 1
assert func.__doc__ == 'A docstring\n\nfoo\n'
func = dec('foo')(func_doc_long)
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
warnings.simplefilter('always')
assert func(1, 2) == None
with pytest.deprecated_call() as w:
assert func(1, 2) is None
assert len(w) == 1
assert func.__doc__ == 'A docstring\n \n foo\n \n Some text\n'

# Try some since and until versions
func = dec('foo', '1.1')(func_no_doc)
assert func.__doc__ == 'foo\n\n* deprecated from version: 1.1\n'
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
warnings.simplefilter('always')
assert func() == None
with pytest.deprecated_call() as w:
assert func() is None
assert len(w) == 1
func = dec('foo', until='99.4')(func_no_doc)
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
warnings.simplefilter('always')
assert func() == None
with pytest.deprecated_call() as w:
assert func() is None
assert len(w) == 1
assert (func.__doc__ ==
'foo\n\n* Will raise {} as of version: 99.4\n'
Expand All @@ -107,39 +100,36 @@ def test_dep_func(self):
with pytest.raises(ExpiredDeprecationError):
func()
assert (func.__doc__ ==
'foo\n\n* Raises {} as of version: 1.8\n'
.format(ExpiredDeprecationError))
'foo\n\n* Raises {} as of version: 1.8\n'
.format(ExpiredDeprecationError))
func = dec('foo', '1.2', '1.8')(func_no_doc)
with pytest.raises(ExpiredDeprecationError):
func()
assert (func.__doc__ ==
'foo\n\n* deprecated from version: 1.2\n'
'* Raises {} as of version: 1.8\n'
.format(ExpiredDeprecationError))
'foo\n\n* deprecated from version: 1.2\n'
'* Raises {} as of version: 1.8\n'
.format(ExpiredDeprecationError))
func = dec('foo', '1.2', '1.8')(func_doc_long)
assert (func.__doc__ ==
'A docstring\n \n foo\n \n'
' * deprecated from version: 1.2\n'
' * Raises {} as of version: 1.8\n \n'
' Some text\n'
.format(ExpiredDeprecationError))
'A docstring\n \n foo\n \n'
' * deprecated from version: 1.2\n'
' * Raises {} as of version: 1.8\n \n'
' Some text\n'
.format(ExpiredDeprecationError))
with pytest.raises(ExpiredDeprecationError):
func()

# Check different warnings and errors
func = dec('foo', warn_class=UserWarning)(func_no_doc)
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
warnings.simplefilter('always')
assert func() == None
assert func() is None
assert len(w) == 1
assert w[0].category is UserWarning

func = dec('foo', error_class=CustomError)(func_no_doc)
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
warnings.simplefilter('always')
assert func() == None
assert len(w) == 1
assert w[0].category is DeprecationWarning
with pytest.deprecated_call():
assert func() is None

func = dec('foo', until='1.8', error_class=CustomError)(func_no_doc)
with pytest.raises(CustomError):
Expand All @@ -154,19 +144,15 @@ class TestDeprecatorMaker(object):
def test_deprecator_maker(self):
dec = self.dep_maker(warn_class=UserWarning)
func = dec('foo')(func_no_doc)
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
warnings.simplefilter('always')
assert func() == None
with pytest.warns(UserWarning) as w:
# warnings.simplefilter('always')
assert func() is None
assert len(w) == 1
assert w[0].category is UserWarning

dec = self.dep_maker(error_class=CustomError)
func = dec('foo')(func_no_doc)
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
warnings.simplefilter('always')
assert func() == None
assert len(w) == 1
assert w[0].category is DeprecationWarning
with pytest.deprecated_call():
assert func() is None

func = dec('foo', until='1.8')(func_no_doc)
with pytest.raises(CustomError):
Expand Down
7 changes: 3 additions & 4 deletions nibabel/tests/test_ecat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from numpy.testing import assert_array_equal, assert_array_almost_equal

from ..testing_pytest import data_path, suppress_warnings, clear_and_catch_warnings
from ..testing_pytest import data_path, suppress_warnings
from ..tmpdirs import InTemporaryDirectory

from .test_wrapstruct import _TestWrapStructBase
Expand Down Expand Up @@ -164,7 +164,7 @@ def test_subheader(self):
assert self.subhdr.get_nframes() == 1
assert (self.subhdr.get_nframes() ==
len(self.subhdr.subheaders))
assert self.subhdr._check_affines() == True
assert self.subhdr._check_affines() is True
assert_array_almost_equal(np.diag(self.subhdr.get_frame_affine()),
np.array([2.20241979, 2.20241979, 3.125, 1.]))
assert self.subhdr.get_zooms()[0] == 2.20241978764534
Expand Down Expand Up @@ -271,8 +271,7 @@ def test_mlist_regression(self):

def test_from_filespec_deprecation():
# Check from_filespec raises Deprecation
with clear_and_catch_warnings() as w:
warnings.simplefilter('always', DeprecationWarning)
with pytest.deprecated_call() as w:
# No warning for standard load
img_loaded = EcatImage.load(ecat_file)
assert len(w) == 0
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ def test_sys_dir():
elif os.name == 'posix':
assert sys_dir == r'/etc/nipy'
else:
assert sys_dir == None
assert sys_dir is None
8 changes: 4 additions & 4 deletions nibabel/tests/test_files_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def test_files_spatialimages():
for klass in klasses:
file_map = klass.make_file_map()
for key, value in file_map.items():
assert value.filename == None
assert value.fileobj == None
assert value.filename is None
assert value.fileobj is None
assert value.pos == 0
# If we can't create new images in memory without loading, bail here
if not klass.makeable:
Expand All @@ -42,8 +42,8 @@ def test_files_spatialimages():
else:
img = klass(arr, aff)
for key, value in img.file_map.items():
assert value.filename == None
assert value.fileobj == None
assert value.filename is None
assert value.fileobj is None
assert value.pos == 0


Expand Down
6 changes: 3 additions & 3 deletions nibabel/tests/test_fileslice.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_is_fancy():
assert not is_fancy((None, 1))
assert not is_fancy((1, None))
# Chack that actual False returned (rather than falsey)
assert is_fancy(1) == False
assert is_fancy(1) is False


def test_canonical_slicers():
Expand Down Expand Up @@ -243,9 +243,9 @@ def test_threshold_heuristic():
# Test for default skip / read heuristic
# int
assert threshold_heuristic(1, 9, 1, skip_thresh=8) == 'full'
assert threshold_heuristic(1, 9, 1, skip_thresh=7) == None
assert threshold_heuristic(1, 9, 1, skip_thresh=7) is None
assert threshold_heuristic(1, 9, 2, skip_thresh=16) == 'full'
assert threshold_heuristic(1, 9, 2, skip_thresh=15) == None
assert threshold_heuristic(1, 9, 2, skip_thresh=15) is None
# full slice, smallest step size
assert (threshold_heuristic(
slice(0, 9, 1), 9, 2, skip_thresh=2) ==
Expand Down
6 changes: 3 additions & 3 deletions nibabel/tests/test_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_closest_canonical():
# And a case where the Analyze image has to be flipped
img = AnalyzeImage(arr, np.diag([-1, 1, 1, 1]))
xyz_img = as_closest_canonical(img)
assert not img is xyz_img
assert img is not xyz_img
out_arr = xyz_img.get_fdata()
assert_array_equal(out_arr, np.flipud(arr))

Expand All @@ -156,7 +156,7 @@ def test_closest_canonical():
img = Nifti1Image(arr, np.diag([-1, 1, 1, 1]))
img.header.set_dim_info(0, 1, 2)
xyz_img = as_closest_canonical(img)
assert not img is xyz_img
assert img is not xyz_img
assert img.header.get_dim_info() == xyz_img.header.get_dim_info()
out_arr = xyz_img.get_fdata()
assert_array_equal(out_arr, np.flipud(arr))
Expand All @@ -181,7 +181,7 @@ def test_closest_canonical():
img.header.set_dim_info(0, 1, 2)

xyz_img = as_closest_canonical(img)
assert not img is xyz_img
assert img is not xyz_img
# Check both the original and new objects
assert img.header.get_dim_info() == (0, 1, 2)
assert xyz_img.header.get_dim_info() == (0, 2, 1)
Expand Down
Loading