import os
import warnings

import numpy as np
from numpy.testing import raises

from skimage import io
from skimage import data_dir


@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')
        io.imread(path, plugin='null')


@raises(Warning)
def test_null_imsave():
    with warnings.catch_warnings():  # Temporarily set warnings as errors.
        warnings.filterwarnings('error')
        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')
        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.
    path = os.path.join(data_dir, '*.png')
    with warnings.catch_warnings():  # Temporarily set warnings as errors.
        warnings.filterwarnings('error')
        collection = io.imread_collection(path, plugin='null')
        collection[0]


if __name__ == '__main__':
    from numpy.testing import run_module_suite
    run_module_suite()

@@ -18,16 +18,54 @@
 warnings
+%0Afrom contextlib import contextmanager
 %0A%0Aimport
@@ -170,94 +170,127 @@
 %0A%0A%0A@
-raises(Warning)%0Adef test_null_imread():%0A    path = os.path.join(data_dir, 'color.png')
+contextmanager%0Adef warnings_as_errors():%0A    # Temporarily set warnings as errors so we can test the warning is raised.
 %0A   
@@ -325,36 +325,40 @@
 s():
-  # Temporarily set 
+%0A        warnings.filter
 warnings
  as 
@@ -349,35 +349,33 @@
 lterwarnings
- as 
+('
 error
-s.
+')
 %0A        war
@@ -371,39 +371,128 @@
         
-warnings.filter
+yield%0A%0A@raises(Warning)%0Adef test_null_imread():%0A    path = os.path.join(data_dir, 'color.png')%0A    with 
 warnings
 ('error'
@@ -479,33 +479,37 @@
 ith warnings
-('
+_as_
 error
-')
+s():
 %0A        io.
@@ -600,106 +600,21 @@
 ings
-.catch_warnings():  # Temporarily set warnings as errors.%0A        warnings.filterwarnings('error')
+_as_errors():
 %0A   
@@ -738,106 +738,21 @@
 ings
-.catch_warnings():  # Temporarily set warnings as errors.%0A        warnings.filterwarnings('error')
+_as_errors():
 %0A   
@@ -954,16 +954,19 @@
 unction 
+is 
 dynamica
@@ -1062,106 +1062,21 @@
 ings
-.catch_warnings():  # Temporarily set warnings as errors.%0A        warnings.filterwarnings('error')
+_as_errors():
 %0A   

Clean up test