FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Backport PR #15199 on branch v3.2.x (MNT/TST: generalize check_figures_equal to work with pytest.marks) by meeseeksmachine · Pull Request #16795 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
59 changes: 29 additions & 30 deletions lib/matplotlib/testing/decorators.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -381,41 +381,40 @@ def test_plot(fig_test, fig_ref):
fig_test.subplots().plot([1, 3, 5])
fig_ref.subplots().plot([0, 1, 2], [1, 3, 5])
"""

POSITIONAL_OR_KEYWORD = inspect.Parameter.POSITIONAL_OR_KEYWORD
def decorator(func):
import pytest

_, result_dir = _image_directories(func)

if len(inspect.signature(func).parameters) == 2:
# Free-standing function.
@pytest.mark.parametrize("ext", extensions)
def wrapper(ext):
fig_test = plt.figure("test")
fig_ref = plt.figure("reference")
func(fig_test, fig_ref)
test_image_path = result_dir / (func.__name__ + "." + ext)
ref_image_path = (
result_dir / (func.__name__ + "-expected." + ext))
fig_test.savefig(test_image_path)
fig_ref.savefig(ref_image_path)
_raise_on_image_difference(
ref_image_path, test_image_path, tol=tol)

elif len(inspect.signature(func).parameters) == 3:
# Method.
@pytest.mark.parametrize("ext", extensions)
def wrapper(self, ext):
fig_test = plt.figure("test")
fig_ref = plt.figure("reference")
func(self, fig_test, fig_ref)
test_image_path = result_dir / (func.__name__ + "." + ext)
ref_image_path = (
result_dir / (func.__name__ + "-expected." + ext))
fig_test.savefig(test_image_path)
fig_ref.savefig(ref_image_path)
_raise_on_image_difference(
ref_image_path, test_image_path, tol=tol)
@pytest.mark.parametrize("ext", extensions)
def wrapper(*args, ext, **kwargs):
fig_test = plt.figure("test")
fig_ref = plt.figure("reference")
func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs)
test_image_path = result_dir / (func.__name__ + "." + ext)
ref_image_path = result_dir / (
func.__name__ + "-expected." + ext
)
fig_test.savefig(test_image_path)
fig_ref.savefig(ref_image_path)
_raise_on_image_difference(
ref_image_path, test_image_path, tol=tol
)

sig = inspect.signature(func)
new_sig = sig.replace(
parameters=([param
for param in sig.parameters.values()
if param.name not in {"fig_test", "fig_ref"}]
+ [inspect.Parameter("ext", POSITIONAL_OR_KEYWORD)])
)
wrapper.__signature__ = new_sig

# reach a bit into pytest internals to hoist the marks from
# our wrapped function
new_marks = getattr(func, "pytestmark", []) + wrapper.pytestmark
wrapper.pytestmark = new_marks

return wrapper

Expand Down
14 changes: 12 additions & 2 deletions lib/matplotlib/tests/test_testing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import warnings
import pytest
from matplotlib.testing.decorators import check_figures_equal

@pytest.mark.xfail(strict=True,
reason="testing that warnings fail tests")

@pytest.mark.xfail(
strict=True, reason="testing that warnings fail tests"
)
def test_warn_to_fail():
warnings.warn("This should fail the test")


@pytest.mark.parametrize("a", [1])
@check_figures_equal(extensions=["png"])
@pytest.mark.parametrize("b", [1])
def test_parametrize_with_check_figure_equal(a, fig_ref, b, fig_test):
assert a == b

Back | FazBrowse Home | New Git URL