Skip to content

Commit

Permalink
Test other config options
Browse files Browse the repository at this point in the history
  • Loading branch information
ConorMacBride committed Sep 29, 2023
1 parent c4cb3de commit 913c476
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/test_generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
PYFILE = (
"""
import matplotlib.pyplot as plt
import pytest
@pytest.mark.mpl_image_compare
def test_mpl():
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
return fig
"""
)


def test_generate_baseline_images(pytester):
pytester.makepyfile(PYFILE)
baseline_dir = pytester.path / "alternative_baseline"
result = pytester.runpytest(f"--mpl-generate-path={baseline_dir}")
result.assert_outcomes(skipped=1)
assert (baseline_dir / "test_mpl.png").exists()


def test_generate_baseline_hashes(pytester):
pytester.makepyfile(PYFILE)
hash_library = pytester.path / "alternative_baseline" / "hash_library_1.json"
result = pytester.runpytest(
f"--mpl-generate-hash-library={hash_library}",
f"--mpl-results-path={pytester.path}",
)
result.assert_outcomes(failed=1) # this option enables --mpl
assert hash_library.exists()
assert (pytester.path / "test_generate_baseline_hashes.test_mpl" / "result.png").exists()


def test_generate_baseline_images_and_hashes(pytester):
pytester.makepyfile(PYFILE)
baseline_dir = pytester.path / "alternative_baseline"
hash_library = pytester.path / "alternative_baseline" / "hash_library_1.json"
result = pytester.runpytest(
f"--mpl-generate-path={baseline_dir}",
f"--mpl-generate-hash-library={hash_library}",
)
result.assert_outcomes(skipped=1)
assert (baseline_dir / "test_mpl.png").exists()
assert hash_library.exists()
41 changes: 41 additions & 0 deletions tests/test_results_always.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from pathlib import Path

import pytest


@pytest.mark.parametrize(
"ini, cli, enabled_expected",
[
(None, None, False),
(True, None, True),
(False, None, False),
(False, True, True),
(True, True, True),
],
)
def test_config(pytester, ini, cli, enabled_expected):
ini = f"mpl-results-always = {ini}" if ini else ""
pytester.makeini(
f"""
[pytest]
mpl-default-style = fivethirtyeight
mpl-baseline-path = {Path(__file__).parent / "baseline" / "2.0.x"}
mpl-results-path = {pytester.path}
{ini}
"""
)
pytester.makepyfile(
"""
import matplotlib.pyplot as plt
import pytest
@pytest.mark.mpl_image_compare
def test_base_style():
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
return fig
"""
)
cli = "--mpl-results-always" if cli else ""
result = pytester.runpytest("--mpl", cli)
result.assert_outcomes(passed=1)
assert (pytester.path / "test_config.test_base_style" / "result.png").exists() == enabled_expected
34 changes: 34 additions & 0 deletions tests/test_results_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest


@pytest.mark.parametrize(
"ini, cli, expected",
[
("dir1", None, "dir1"),
("dir1", "dir2", "dir2"),
(None, "dir2", "dir2"),
],
)
def test_config(pytester, ini, cli, expected):
ini = f"mpl-results-path = {ini}" if ini else ""
pytester.makeini(
f"""
[pytest]
{ini}
"""
)
pytester.makepyfile(
"""
import matplotlib.pyplot as plt
import pytest
@pytest.mark.mpl_image_compare
def test_mpl():
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
return fig
"""
)
cli = f"--mpl-results-path={cli}" if cli else ""
result = pytester.runpytest("--mpl", cli)
result.assert_outcomes(failed=1)
assert (pytester.path / expected / "test_config.test_mpl" / "result.png").exists()

0 comments on commit 913c476

Please sign in to comment.