From e0dafcf004f07f7aa333d16252f86b2fc5f70d0b Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Tue, 14 Jan 2014 23:27:23 -0600 Subject: [PATCH] Clean up test --- skimage/io/tests/test_null.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/skimage/io/tests/test_null.py b/skimage/io/tests/test_null.py index 8895f77c..56f5df89 100644 --- a/skimage/io/tests/test_null.py +++ b/skimage/io/tests/test_null.py @@ -1,5 +1,6 @@ import os import warnings +from contextlib import contextmanager import numpy as np from numpy.testing import raises @@ -8,35 +9,38 @@ from skimage import io from skimage import data_dir +@contextmanager +def warnings_as_errors(): + # Temporarily set warnings as errors so we can test the warning is raised. + with warnings.catch_warnings(): + warnings.filterwarnings('error') + yield + @raises(Warning) def test_null_imread(): path = os.path.join(data_dir, 'color.png') - with warnings.catch_warnings(): # Temporarily set warnings as errors. - warnings.filterwarnings('error') + with warnings_as_errors(): io.imread(path, plugin='null') @raises(Warning) def test_null_imsave(): - with warnings.catch_warnings(): # Temporarily set warnings as errors. - warnings.filterwarnings('error') + with warnings_as_errors(): io.imsave('dummy.png', np.zeros((3, 3)), plugin='null') @raises(Warning) def test_null_imshow(): - with warnings.catch_warnings(): # Temporarily set warnings as errors. - warnings.filterwarnings('error') + with warnings_as_errors(): io.imshow(np.zeros((3, 3)), plugin='null') @raises(Warning) def test_null_imread_collection(): # Note that the null plugin doesn't define an `imread_collection` plugin - # but this function dynamically added by the plugin manager. + # but this function is dynamically added by the plugin manager. path = os.path.join(data_dir, '*.png') - with warnings.catch_warnings(): # Temporarily set warnings as errors. - warnings.filterwarnings('error') + with warnings_as_errors(): collection = io.imread_collection(path, plugin='null') collection[0]