Skip to content

Commit 91d23c4

Browse files
committed
Add failure test for running inside class
1 parent b8bcfe1 commit 91d23c4

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/test_pytest_mpl.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,46 @@ def test_results_always(tmpdir):
486486
assert image and not image_exists
487487
assert image not in html
488488
assert json_res[json_image_key] is None
489+
490+
491+
TEST_FAILING_CLASS = """
492+
import pytest
493+
import matplotlib.pyplot as plt
494+
class TestClass(object):
495+
@pytest.mark.mpl_image_compare
496+
def test_fails(self):
497+
fig = plt.figure()
498+
ax = fig.add_subplot(1, 1, 1)
499+
ax.plot([1, 2, 3])
500+
return fig
501+
"""
502+
503+
TEST_FAILING_CLASS_SETUP_METHOD = """
504+
import pytest
505+
import matplotlib.pyplot as plt
506+
class TestClassWithSetup:
507+
def setup_method(self, method):
508+
self.x = [1, 2, 3]
509+
@pytest.mark.mpl_image_compare
510+
def test_fails(self):
511+
fig = plt.figure()
512+
ax = fig.add_subplot(1, 1, 1)
513+
ax.plot(self.x)
514+
return fig
515+
"""
516+
517+
518+
@pytest.mark.parametrize("code", [TEST_FAILING_CLASS, TEST_FAILING_CLASS_SETUP_METHOD])
519+
def test_class_fail(code, tmpdir):
520+
521+
test_file = tmpdir.join('test.py').strpath
522+
with open(test_file, 'w') as f:
523+
f.write(code)
524+
525+
# Assert fails if hash library missing
526+
assert_pytest_fails_with(['--mpl', test_file, '--mpl-hash-library=/not/a/path'],
527+
"Can't find hash library at path")
528+
529+
# If we don't use --mpl option, the test should succeed
530+
code = call_pytest([test_file])
531+
assert code == 0

0 commit comments

Comments
 (0)