Skip to content

Commit 6fa164c

Browse files
committed
Test new config options
Adds tests for the new config options across ini, CLI and kwarg where relevant. Asserts that they work and have the expected order of precedence. Still need to add dedicated tests for all the other options.
1 parent 8c12b1d commit 6fa164c

8 files changed

+346
-0
lines changed

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ filterwarnings =
5252
error
5353
ignore:distutils Version classes are deprecated
5454
ignore:the imp module is deprecated in favour of importlib
55+
ignore:The NumPy module was reloaded
5556

5657
[flake8]
5758
max-line-length = 100

tests/test_baseline_path.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import shutil
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
7+
@pytest.mark.parametrize(
8+
"ini, cli, kwarg, expected_baseline_path, success_expected",
9+
[
10+
("dir1", None, None, "dir1", True),
11+
("dir1", "dir2", None, "dir2", True),
12+
("dir1", "dir2", "dir3", "dir3", True),
13+
("dir1", "dir2", "dir3", "dir2", False),
14+
(None, None, "dir3", "dir3", True),
15+
],
16+
)
17+
def test_config(pytester, ini, cli, kwarg, expected_baseline_path, success_expected):
18+
(pytester.path / expected_baseline_path).mkdir()
19+
shutil.copyfile( # Test will only pass if baseline is at expected path
20+
Path(__file__).parent / "baseline" / "2.0.x" / "test_base_style.png",
21+
pytester.path / expected_baseline_path / "test_mpl.png",
22+
)
23+
ini = f"mpl-baseline-path = {pytester.path / ini}" if ini is not None else ""
24+
pytester.makeini(
25+
f"""
26+
[pytest]
27+
mpl-default-style = fivethirtyeight
28+
{ini}
29+
"""
30+
)
31+
kwarg = f"baseline_dir='{pytester.path / kwarg}'" if kwarg else ""
32+
pytester.makepyfile(
33+
f"""
34+
import matplotlib.pyplot as plt
35+
import pytest
36+
@pytest.mark.mpl_image_compare({kwarg})
37+
def test_mpl():
38+
fig, ax = plt.subplots()
39+
ax.plot([1, 2, 3])
40+
return fig
41+
"""
42+
)
43+
cli = f"--mpl-baseline-path={pytester.path / cli}" if cli else ""
44+
result = pytester.runpytest("--mpl", cli)
45+
if success_expected:
46+
result.assert_outcomes(passed=1)
47+
else:
48+
result.assert_outcomes(failed=1)

tests/test_default_backend.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
3+
4+
@pytest.mark.parametrize(
5+
"ini, cli, kwarg, expected",
6+
[
7+
("backend1", None, None, "backend1"),
8+
("backend1", "backend2", None, "backend2"),
9+
("backend1", "backend2", "backend3", "backend3"),
10+
],
11+
)
12+
def test_config(pytester, ini, cli, kwarg, expected):
13+
ini = f"mpl-default-backend = {ini}" if ini else ""
14+
pytester.makeini(
15+
f"""
16+
[pytest]
17+
{ini}
18+
"""
19+
)
20+
kwarg = f"backend='{kwarg}'" if kwarg else ""
21+
pytester.makepyfile(
22+
f"""
23+
import matplotlib.pyplot as plt
24+
import pytest
25+
@pytest.mark.mpl_image_compare({kwarg})
26+
def test_mpl():
27+
fig, ax = plt.subplots()
28+
ax.plot([1, 2, 3])
29+
return fig
30+
"""
31+
)
32+
cli = f"--mpl-default-backend={cli}" if cli else ""
33+
result = pytester.runpytest("--mpl", cli)
34+
result.assert_outcomes(failed=1)
35+
result.stdout.fnmatch_lines([f"*ModuleNotFoundError: No module named 'matplotlib.backends.backend_{expected}'*"])

tests/test_default_style.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pytest
2+
3+
4+
@pytest.mark.parametrize(
5+
"ini, cli, kwarg, expected",
6+
[
7+
("sty1", None, None, "sty1"),
8+
("sty1", "sty2", None, "sty2"),
9+
("sty1", "sty2", "sty3", "sty3"),
10+
],
11+
)
12+
def test_config(pytester, ini, cli, kwarg, expected):
13+
ini = "mpl-default-style = " + ini if ini else ""
14+
pytester.makeini(
15+
f"""
16+
[pytest]
17+
mpl-baseline-path = {pytester.path}
18+
{ini}
19+
"""
20+
)
21+
kwarg = f"style='{kwarg}'" if kwarg else ""
22+
pytester.makepyfile(
23+
f"""
24+
import matplotlib.pyplot as plt
25+
import pytest
26+
@pytest.mark.mpl_image_compare({kwarg})
27+
def test_mpl():
28+
fig, ax = plt.subplots()
29+
ax.plot([1, 2, 3])
30+
return fig
31+
"""
32+
)
33+
34+
cli = "--mpl-default-style=" + cli if cli else ""
35+
result = pytester.runpytest("--mpl", cli)
36+
result.assert_outcomes(failed=1)
37+
result.stdout.fnmatch_lines([f"*OSError: '{expected}' is not a valid package style*"])

tests/test_default_tolerance.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
from PIL import Image, ImageDraw
5+
6+
TEST_NAME = "test_base_style"
7+
8+
9+
@pytest.fixture(scope="module")
10+
def baseline_image(tmpdir_factory):
11+
path = Path(__file__).parent / "baseline" / "2.0.x" / f"{TEST_NAME}.png"
12+
image = Image.open(path)
13+
draw = ImageDraw.Draw(image)
14+
draw.rectangle(((0, 0), (100, 100)), fill="red")
15+
output = Path(tmpdir_factory.mktemp("data").join(f"{TEST_NAME}.png"))
16+
print(output)
17+
image.save(output)
18+
return output
19+
20+
21+
@pytest.mark.parametrize(
22+
"ini, cli, kwarg, success_expected",
23+
[
24+
(40, None, None, True),
25+
(30, 40, None, True),
26+
(30, 30, 40, True),
27+
(30, 40, 30, False),
28+
(40, 30, 30, False),
29+
],
30+
)
31+
def test_config(pytester, baseline_image, ini, cli, kwarg, success_expected):
32+
ini = f"mpl-default-tolerance = {ini}" if ini else ""
33+
pytester.makeini(
34+
f"""
35+
[pytest]
36+
mpl-default-style = fivethirtyeight
37+
mpl-baseline-path = {baseline_image.parent}
38+
{ini}
39+
"""
40+
)
41+
kwarg = f"tolerance={kwarg}" if kwarg else ""
42+
pytester.makepyfile(
43+
f"""
44+
import matplotlib.pyplot as plt
45+
import pytest
46+
@pytest.mark.mpl_image_compare({kwarg})
47+
def {TEST_NAME}():
48+
fig, ax = plt.subplots()
49+
ax.plot([1, 2, 3])
50+
return fig
51+
"""
52+
)
53+
cli = f"--mpl-default-tolerance={cli}" if cli else ""
54+
result = pytester.runpytest("--mpl", cli)
55+
if success_expected:
56+
result.assert_outcomes(passed=1)
57+
else:
58+
result.assert_outcomes(failed=1)

tests/test_generate_summary.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import json
2+
3+
import pytest
4+
5+
6+
@pytest.mark.parametrize(
7+
"ini, cli, expected",
8+
[
9+
("json", None, {"json"}),
10+
("json", "html", {"html"}),
11+
("basic-html", "json", {"json"}),
12+
(None, "json,basic-html,html", {"json", "basic-html", "html"}),
13+
],
14+
)
15+
def test_config(pytester, ini, cli, expected):
16+
ini = f"mpl-generate-summary = {ini}" if ini else ""
17+
pytester.makeini(
18+
f"""
19+
[pytest]
20+
mpl-results-path = {pytester.path}
21+
{ini}
22+
"""
23+
)
24+
pytester.makepyfile(
25+
"""
26+
import matplotlib.pyplot as plt
27+
import pytest
28+
@pytest.mark.mpl_image_compare
29+
def test_mpl():
30+
fig, ax = plt.subplots()
31+
ax.plot([1, 2, 3])
32+
return fig
33+
"""
34+
)
35+
cli = f"--mpl-generate-summary={cli}" if cli else ""
36+
result = pytester.runpytest("--mpl", cli)
37+
result.assert_outcomes(failed=1)
38+
39+
json_summary = pytester.path / "results.json"
40+
if "json" in expected:
41+
with open(json_summary) as fp:
42+
results = json.load(fp)
43+
assert "test_config.test_mpl" in results
44+
else:
45+
assert not json_summary.exists()
46+
47+
html_summary = pytester.path / "fig_comparison.html"
48+
if "html" in expected:
49+
with open(html_summary) as fp:
50+
raw = fp.read()
51+
assert "bootstrap" in raw
52+
assert "test_config.test_mpl" in raw
53+
else:
54+
assert not html_summary.exists()
55+
56+
basic_html_summary = pytester.path / "fig_comparison_basic.html"
57+
if "basic-html" in expected:
58+
with open(basic_html_summary) as fp:
59+
raw = fp.read()
60+
assert "bootstrap" not in raw
61+
assert "test_config.test_mpl" in raw
62+
else:
63+
assert not basic_html_summary.exists()

tests/test_hash_library.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import json
2+
3+
import pytest
4+
5+
6+
@pytest.mark.parametrize(
7+
"ini, cli, kwarg, success_expected",
8+
[
9+
("bad", None, None, False),
10+
("good", None, None, True),
11+
("bad", "good", None, True),
12+
("bad", "bad", "good", False), # Note: CLI overrides kwarg
13+
("bad", "good", "bad", True),
14+
],
15+
)
16+
def test_config(pytester, ini, cli, kwarg, success_expected):
17+
hash_libraries = {
18+
"good": (pytester.path / "good_hash_library.json",
19+
"b1e03274b2df4130e0894afd6c0faa76805e1851eec32f38e86d2423de0d9186"),
20+
"bad": (pytester.path / "bad_hash_library.json", "bad-value"),
21+
}
22+
for library_path, hash_val in hash_libraries.values():
23+
with open(library_path, "w") as fp:
24+
json.dump({f"test_config.test_mpl": hash_val}, fp)
25+
26+
ini = f"mpl-hash-library = {hash_libraries[ini][0]}" if ini else ""
27+
pytester.makeini(
28+
f"""
29+
[pytest]
30+
{ini}
31+
"""
32+
)
33+
34+
kwarg = f"hash_library='{hash_libraries[kwarg][0]}'" if kwarg else ""
35+
pytester.makepyfile(
36+
f"""
37+
import matplotlib.pyplot as plt
38+
import pytest
39+
@pytest.mark.mpl_image_compare({kwarg})
40+
def test_mpl():
41+
fig, ax = plt.subplots()
42+
ax.plot([1, 3, 2])
43+
return fig
44+
"""
45+
)
46+
47+
cli = f"--mpl-hash-library={hash_libraries[cli][0]}" if cli else ""
48+
result = pytester.runpytest("--mpl", cli)
49+
if success_expected:
50+
result.assert_outcomes(passed=1)
51+
else:
52+
result.assert_outcomes(failed=1)

tests/test_use_full_test_name.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import shutil
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
FULL_TEST_NAME = "test_config.TestClass.test_mpl"
7+
SHORT_TEST_NAME = "test_mpl"
8+
9+
10+
@pytest.mark.parametrize(
11+
"ini, cli, expected_baseline_name, success_expected",
12+
[
13+
(None, None, SHORT_TEST_NAME, True),
14+
(False, None, SHORT_TEST_NAME, True),
15+
(True, None, FULL_TEST_NAME, True),
16+
(False, True, FULL_TEST_NAME, True),
17+
(None, True, FULL_TEST_NAME, True),
18+
(True, True, "bad_name", False),
19+
],
20+
)
21+
def test_config(pytester, ini, cli, expected_baseline_name, success_expected):
22+
shutil.copyfile( # Test will only pass if baseline is at expected path
23+
Path(__file__).parent / "baseline" / "2.0.x" / "test_base_style.png",
24+
pytester.path / f"{expected_baseline_name}.png",
25+
)
26+
ini = f"mpl-use-full-test-name = {ini}" if ini is not None else ""
27+
pytester.makeini(
28+
f"""
29+
[pytest]
30+
mpl-default-style = fivethirtyeight
31+
mpl-baseline-path = {pytester.path}
32+
{ini}
33+
"""
34+
)
35+
pytester.makepyfile(
36+
"""
37+
import matplotlib.pyplot as plt
38+
import pytest
39+
class TestClass:
40+
@pytest.mark.mpl_image_compare
41+
def test_mpl(self):
42+
fig, ax = plt.subplots()
43+
ax.plot([1, 2, 3])
44+
return fig
45+
"""
46+
)
47+
cli = "--mpl-use-full-test-name" if cli else ""
48+
result = pytester.runpytest("--mpl", cli)
49+
if success_expected:
50+
result.assert_outcomes(passed=1)
51+
else:
52+
result.assert_outcomes(failed=1)

0 commit comments

Comments
 (0)