Skip to content

Commit 5e48091

Browse files
committed
Fix a bug with test classes that use setup_method
1 parent f39478d commit 5e48091

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
0.8 (unreleased)
22
----------------
33

4-
- No changes yet.
4+
- Fixed use of mpl_image_compare on methods of test classes that also
5+
use setup_method. [#50]
56

67
0.7 (2016-11-26)
78
----------------

pytest_mpl/plugin.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,12 @@ def item_function_wrapper(*args, **kwargs):
164164
# Run test and get figure object
165165
import inspect
166166
if inspect.ismethod(original): # method
167-
fig = original(*args[1:], **kwargs)
167+
# In some cases, for example if setup_method is used,
168+
# original appears to belong to an instance of the test
169+
# class that is not the same as args[0], and args[0] is the
170+
# one that has the correct attributes set up from setup_method
171+
# so we ignore original.__self__ and use args[0] instead.
172+
fig = original.__func__(*args, **kwargs)
168173
else: # function
169174
fig = original(*args, **kwargs)
170175

tests/test_pytest_mpl.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,19 @@ def test_parametrized(s):
189189
ax = fig.add_subplot(1, 1, 1)
190190
ax.scatter([1,3,4,3,2],[1,4,3,3,1], s=s)
191191
return fig
192+
193+
194+
class TestClassWithSetup(object):
195+
196+
# Regression test for a bug that occurred when using setup_method
197+
198+
def setup_method(self, method):
199+
self.x = [1, 2, 3]
200+
201+
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
202+
filename='test_succeeds.png')
203+
def test_succeeds(self):
204+
fig = plt.figure()
205+
ax = fig.add_subplot(1, 1, 1)
206+
ax.plot(self.x)
207+
return fig

0 commit comments

Comments
 (0)