import pytest
import matplotlib.pyplot as plt


@pytest.mark.mpl_image_compare
def test_succeeds():
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.plot([1,2,3])
    return fig

@pytest.mark.xfail
@pytest.mark.mpl_image_compare
def test_fails():
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.plot([1,2,2])
    return fig
    
@pytest.mark.mpl_image_compare(tolerance=20)
def test_tolerance():
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.plot([1,2,2])
    return fig

def test_nofigure():
    pass

@@ -1,12 +1,41 @@
+import os%0Aimport subprocess%0A%0A
 import pytes
@@ -69,17 +69,16 @@
 as plt%0A%0A
-%0A
 @pytest.
@@ -217,26 +217,72 @@
 ig%0A%0A
-@pytest.mark.xfail
+TEST_FAILING = %22%22%22%0Aimport pytest%0Aimport matplotlib.pyplot as plt
 %0A@py
@@ -322,17 +322,16 @@
 est_fail
-s
 ():%0A    
@@ -417,20 +417,1383 @@
 urn fig%0A
-    
+%22%22%22%0A%0Adef test_fails(tmpdir):%0A%0A    test_file = tmpdir.join('test.py').strpath%0A    with open(test_file, 'w') as f:%0A        f.write(TEST_FAILING)%0A%0A    # If we use --mpl, it should detect that the figure is wrong%0A    code = subprocess.call('py.test --mpl %7B0%7D'.format(test_file), shell=True)%0A    assert code != 0%0A%0A    # If we don't use --mpl option, the test should succeed%0A    code = subprocess.call('py.test %7B0%7D'.format(test_file), shell=True)%0A    assert code == 0%0A%0A%0ATEST_GENERATE = %22%22%22%0Aimport pytest%0Aimport matplotlib.pyplot as plt%0A@pytest.mark.mpl_image_compare%0Adef test_gen():%0A    fig = plt.figure()%0A    ax = fig.add_subplot(1,1,1)%0A    ax.plot(%5B1,2,3%5D)%0A    return fig%0A%22%22%22%0A%0Adef test_generate(tmpdir):%0A%0A    test_file = tmpdir.join('test.py').strpath%0A    with open(test_file, 'w') as f:%0A        f.write(TEST_GENERATE)%0A%0A    gen_dir = tmpdir.join('spam').join('egg').strpath%0A    print(gen_dir)%0A%0A    # If we don't generate, the test will fail%0A    output = subprocess.check_output('py.test --mpl %7B0%7D; exit 0'.format(test_file), shell=True)%0A    assert b'Image file not found for comparison test' in output%0A%0A    # If we do generate, the test should succeed and a new file will appear%0A    code = subprocess.call('py.test --mpl-generate-path=%7B0%7D %7B1%7D'.format(gen_dir, test_file), shell=True)%0A    assert code == 0%0A    assert os.path.exists(os.path.join(gen_dir, 'test_gen.png'))%0A%0A
 %0A@pytest

Test that errors are raised