File: test_default_tolerance.py

package info (click to toggle)
pytest-mpl 0.17.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,132 kB
  • sloc: python: 2,514; javascript: 179; makefile: 16
file content (57 lines) | stat: -rw-r--r-- 1,601 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from pathlib import Path

import pytest
from PIL import Image, ImageDraw

TEST_NAME = "test_base_style"


@pytest.fixture(scope="module")
def baseline_image(tmp_path_factory):
    path = Path(__file__).parent / "baseline" / "2.0.x" / f"{TEST_NAME}.png"
    with Image.open(path) as image:
        draw = ImageDraw.Draw(image)
        draw.rectangle(((0, 0), (100, 100)), fill="red")
        output = tmp_path_factory.mktemp("data") / f"{TEST_NAME}.png"
        image.save(output)
    return output


@pytest.mark.parametrize(
    "ini, cli, kwarg, success_expected",
    [
        (40, None, None, True),
        (30, 40, None, True),
        (30, 30, 40, True),
        (30, 40, 30, False),
        (40, 30, 30, False),
    ],
)
def test_config(pytester, baseline_image, ini, cli, kwarg, success_expected):
    ini = f"mpl-default-tolerance = {ini}" if ini else ""
    pytester.makeini(
        f"""
        [pytest]
        mpl-default-style = fivethirtyeight
        mpl-baseline-path = {baseline_image.parent}
        {ini}
        """
    )
    kwarg = f"tolerance={kwarg}" if kwarg else ""
    pytester.makepyfile(
        f"""
        import matplotlib.pyplot as plt
        import pytest
        @pytest.mark.mpl_image_compare({kwarg})
        def {TEST_NAME}():
            fig, ax = plt.subplots()
            ax.plot([1, 2, 3])
            return fig
        """
    )
    cli = f"--mpl-default-tolerance={cli}" if cli else ""
    result = pytester.runpytest("--mpl", cli)
    if success_expected:
        result.assert_outcomes(passed=1)
    else:
        result.assert_outcomes(failed=1)