Skip to content

Commit 913c476

Browse files
committed
Test other config options
1 parent c4cb3de commit 913c476

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

Diff for: tests/test_generate.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
PYFILE = (
2+
"""
3+
import matplotlib.pyplot as plt
4+
import pytest
5+
@pytest.mark.mpl_image_compare
6+
def test_mpl():
7+
fig, ax = plt.subplots()
8+
ax.plot([1, 2, 3])
9+
return fig
10+
"""
11+
)
12+
13+
14+
def test_generate_baseline_images(pytester):
15+
pytester.makepyfile(PYFILE)
16+
baseline_dir = pytester.path / "alternative_baseline"
17+
result = pytester.runpytest(f"--mpl-generate-path={baseline_dir}")
18+
result.assert_outcomes(skipped=1)
19+
assert (baseline_dir / "test_mpl.png").exists()
20+
21+
22+
def test_generate_baseline_hashes(pytester):
23+
pytester.makepyfile(PYFILE)
24+
hash_library = pytester.path / "alternative_baseline" / "hash_library_1.json"
25+
result = pytester.runpytest(
26+
f"--mpl-generate-hash-library={hash_library}",
27+
f"--mpl-results-path={pytester.path}",
28+
)
29+
result.assert_outcomes(failed=1) # this option enables --mpl
30+
assert hash_library.exists()
31+
assert (pytester.path / "test_generate_baseline_hashes.test_mpl" / "result.png").exists()
32+
33+
34+
def test_generate_baseline_images_and_hashes(pytester):
35+
pytester.makepyfile(PYFILE)
36+
baseline_dir = pytester.path / "alternative_baseline"
37+
hash_library = pytester.path / "alternative_baseline" / "hash_library_1.json"
38+
result = pytester.runpytest(
39+
f"--mpl-generate-path={baseline_dir}",
40+
f"--mpl-generate-hash-library={hash_library}",
41+
)
42+
result.assert_outcomes(skipped=1)
43+
assert (baseline_dir / "test_mpl.png").exists()
44+
assert hash_library.exists()

Diff for: tests/test_results_always.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
6+
@pytest.mark.parametrize(
7+
"ini, cli, enabled_expected",
8+
[
9+
(None, None, False),
10+
(True, None, True),
11+
(False, None, False),
12+
(False, True, True),
13+
(True, True, True),
14+
],
15+
)
16+
def test_config(pytester, ini, cli, enabled_expected):
17+
ini = f"mpl-results-always = {ini}" if ini else ""
18+
pytester.makeini(
19+
f"""
20+
[pytest]
21+
mpl-default-style = fivethirtyeight
22+
mpl-baseline-path = {Path(__file__).parent / "baseline" / "2.0.x"}
23+
mpl-results-path = {pytester.path}
24+
{ini}
25+
"""
26+
)
27+
pytester.makepyfile(
28+
"""
29+
import matplotlib.pyplot as plt
30+
import pytest
31+
@pytest.mark.mpl_image_compare
32+
def test_base_style():
33+
fig, ax = plt.subplots()
34+
ax.plot([1, 2, 3])
35+
return fig
36+
"""
37+
)
38+
cli = "--mpl-results-always" if cli else ""
39+
result = pytester.runpytest("--mpl", cli)
40+
result.assert_outcomes(passed=1)
41+
assert (pytester.path / "test_config.test_base_style" / "result.png").exists() == enabled_expected

Diff for: tests/test_results_path.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
3+
4+
@pytest.mark.parametrize(
5+
"ini, cli, expected",
6+
[
7+
("dir1", None, "dir1"),
8+
("dir1", "dir2", "dir2"),
9+
(None, "dir2", "dir2"),
10+
],
11+
)
12+
def test_config(pytester, ini, cli, expected):
13+
ini = f"mpl-results-path = {ini}" if ini else ""
14+
pytester.makeini(
15+
f"""
16+
[pytest]
17+
{ini}
18+
"""
19+
)
20+
pytester.makepyfile(
21+
"""
22+
import matplotlib.pyplot as plt
23+
import pytest
24+
@pytest.mark.mpl_image_compare
25+
def test_mpl():
26+
fig, ax = plt.subplots()
27+
ax.plot([1, 2, 3])
28+
return fig
29+
"""
30+
)
31+
cli = f"--mpl-results-path={cli}" if cli else ""
32+
result = pytester.runpytest("--mpl", cli)
33+
result.assert_outcomes(failed=1)
34+
assert (pytester.path / expected / "test_config.test_mpl" / "result.png").exists()

0 commit comments

Comments
 (0)