Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit45b9201

Browse files
authored
MNT/TST: generalize check_figures_equal to work with pytest.ma… (#15199)
MNT/TST: generalize check_figures_equal to work with pytest.marks
2 parentsd6e4051 +e10850e commit45b9201

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

‎lib/matplotlib/testing/decorators.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -381,41 +381,40 @@ def test_plot(fig_test, fig_ref):
381381
fig_test.subplots().plot([1, 3, 5])
382382
fig_ref.subplots().plot([0, 1, 2], [1, 3, 5])
383383
"""
384-
384+
POSITIONAL_OR_KEYWORD=inspect.Parameter.POSITIONAL_OR_KEYWORD
385385
defdecorator(func):
386386
importpytest
387387

388388
_,result_dir=_image_directories(func)
389389

390-
iflen(inspect.signature(func).parameters)==2:
391-
# Free-standing function.
392-
@pytest.mark.parametrize("ext",extensions)
393-
defwrapper(ext):
394-
fig_test=plt.figure("test")
395-
fig_ref=plt.figure("reference")
396-
func(fig_test,fig_ref)
397-
test_image_path=result_dir/ (func.__name__+"."+ext)
398-
ref_image_path= (
399-
result_dir/ (func.__name__+"-expected."+ext))
400-
fig_test.savefig(test_image_path)
401-
fig_ref.savefig(ref_image_path)
402-
_raise_on_image_difference(
403-
ref_image_path,test_image_path,tol=tol)
404-
405-
eliflen(inspect.signature(func).parameters)==3:
406-
# Method.
407-
@pytest.mark.parametrize("ext",extensions)
408-
defwrapper(self,ext):
409-
fig_test=plt.figure("test")
410-
fig_ref=plt.figure("reference")
411-
func(self,fig_test,fig_ref)
412-
test_image_path=result_dir/ (func.__name__+"."+ext)
413-
ref_image_path= (
414-
result_dir/ (func.__name__+"-expected."+ext))
415-
fig_test.savefig(test_image_path)
416-
fig_ref.savefig(ref_image_path)
417-
_raise_on_image_difference(
418-
ref_image_path,test_image_path,tol=tol)
390+
@pytest.mark.parametrize("ext",extensions)
391+
defwrapper(*args,ext,**kwargs):
392+
fig_test=plt.figure("test")
393+
fig_ref=plt.figure("reference")
394+
func(*args,fig_test=fig_test,fig_ref=fig_ref,**kwargs)
395+
test_image_path=result_dir/ (func.__name__+"."+ext)
396+
ref_image_path=result_dir/ (
397+
func.__name__+"-expected."+ext
398+
)
399+
fig_test.savefig(test_image_path)
400+
fig_ref.savefig(ref_image_path)
401+
_raise_on_image_difference(
402+
ref_image_path,test_image_path,tol=tol
403+
)
404+
405+
sig=inspect.signature(func)
406+
new_sig=sig.replace(
407+
parameters=([param
408+
forparaminsig.parameters.values()
409+
ifparam.namenotin {"fig_test","fig_ref"}]
410+
+ [inspect.Parameter("ext",POSITIONAL_OR_KEYWORD)])
411+
)
412+
wrapper.__signature__=new_sig
413+
414+
# reach a bit into pytest internals to hoist the marks from
415+
# our wrapped function
416+
new_marks=getattr(func,"pytestmark", [])+wrapper.pytestmark
417+
wrapper.pytestmark=new_marks
419418

420419
returnwrapper
421420

‎lib/matplotlib/tests/test_testing.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
importwarnings
22
importpytest
3+
frommatplotlib.testing.decoratorsimportcheck_figures_equal
34

4-
@pytest.mark.xfail(strict=True,
5-
reason="testing that warnings fail tests")
5+
6+
@pytest.mark.xfail(
7+
strict=True,reason="testing that warnings fail tests"
8+
)
69
deftest_warn_to_fail():
710
warnings.warn("This should fail the test")
11+
12+
13+
@pytest.mark.parametrize("a", [1])
14+
@check_figures_equal(extensions=["png"])
15+
@pytest.mark.parametrize("b", [1])
16+
deftest_parametrize_with_check_figure_equal(a,fig_ref,b,fig_test):
17+
asserta==b

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp