Skip to content

Commit

Permalink
Add pytest v5 pytester compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ConorMacBride committed Oct 7, 2023
1 parent 14037b2 commit fd3ca4e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 23 deletions.
7 changes: 7 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pathlib import Path


def pytester_path(pytester):
if hasattr(pytester, "path"):
return pytester.path
return Path(pytester.tmpdir) # pytest v5
12 changes: 7 additions & 5 deletions tests/test_baseline_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path

import pytest
from helpers import pytester_path


@pytest.mark.parametrize(
Expand All @@ -15,20 +16,21 @@
],
)
def test_config(pytester, ini, cli, kwarg, expected_baseline_path, success_expected):
(pytester.path / expected_baseline_path).mkdir()
path = pytester_path(pytester)
(path / expected_baseline_path).mkdir()
shutil.copyfile( # Test will only pass if baseline is at expected path
Path(__file__).parent / "baseline" / "2.0.x" / "test_base_style.png",
pytester.path / expected_baseline_path / "test_mpl.png",
path / expected_baseline_path / "test_mpl.png",
)
ini = f"mpl-baseline-path = {pytester.path / ini}" if ini is not None else ""
ini = f"mpl-baseline-path = {path / ini}" if ini is not None else ""
pytester.makeini(
f"""
[pytest]
mpl-default-style = fivethirtyeight
{ini}
"""
)
kwarg = f"baseline_dir='{pytester.path / kwarg}'" if kwarg else ""
kwarg = f"baseline_dir='{path / kwarg}'" if kwarg else ""
pytester.makepyfile(
f"""
import matplotlib.pyplot as plt
Expand All @@ -40,7 +42,7 @@ def test_mpl():
return fig
"""
)
cli = f"--mpl-baseline-path={pytester.path / cli}" if cli else ""
cli = f"--mpl-baseline-path={path / cli}" if cli else ""
result = pytester.runpytest("--mpl", cli)
if success_expected:
result.assert_outcomes(passed=1)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_default_style.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from helpers import pytester_path


@pytest.mark.parametrize(
Expand All @@ -14,7 +15,7 @@ def test_config(pytester, ini, cli, kwarg, expected):
pytester.makeini(
f"""
[pytest]
mpl-baseline-path = {pytester.path}
mpl-baseline-path = {pytester_path(pytester)}
{ini}
"""
)
Expand Down
16 changes: 10 additions & 6 deletions tests/test_generate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from helpers import pytester_path

PYFILE = (
"""
import matplotlib.pyplot as plt
Expand All @@ -13,28 +15,30 @@ def test_mpl():

def test_generate_baseline_images(pytester):
pytester.makepyfile(PYFILE)
baseline_dir = pytester.path / "alternative_baseline"
baseline_dir = pytester_path(pytester) / "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"
path = pytester_path(pytester)
hash_library = path / "alternative_baseline" / "hash_library_1.json"
result = pytester.runpytest(
f"--mpl-generate-hash-library={hash_library}",
f"--mpl-results-path={pytester.path}",
f"--mpl-results-path={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()
assert (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"
path = pytester_path(pytester)
baseline_dir = path / "alternative_baseline"
hash_library = path / "alternative_baseline" / "hash_library_1.json"
result = pytester.runpytest(
f"--mpl-generate-path={baseline_dir}",
f"--mpl-generate-hash-library={hash_library}",
Expand Down
10 changes: 6 additions & 4 deletions tests/test_generate_summary.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

import pytest
from helpers import pytester_path


@pytest.mark.parametrize(
Expand All @@ -13,11 +14,12 @@
],
)
def test_config(pytester, ini, cli, expected):
path = pytester_path(pytester)
ini = f"mpl-generate-summary = {ini}" if ini else ""
pytester.makeini(
f"""
[pytest]
mpl-results-path = {pytester.path}
mpl-results-path = {path}
{ini}
"""
)
Expand All @@ -36,15 +38,15 @@ def test_mpl():
result = pytester.runpytest("--mpl", cli)
result.assert_outcomes(failed=1)

json_summary = pytester.path / "results.json"
json_summary = path / "results.json"
if "json" in expected:
with open(json_summary) as fp:
results = json.load(fp)
assert "test_config.test_mpl" in results
else:
assert not json_summary.exists()

html_summary = pytester.path / "fig_comparison.html"
html_summary = path / "fig_comparison.html"
if "html" in expected:
with open(html_summary) as fp:
raw = fp.read()
Expand All @@ -53,7 +55,7 @@ def test_mpl():
else:
assert not html_summary.exists()

basic_html_summary = pytester.path / "fig_comparison_basic.html"
basic_html_summary = path / "fig_comparison_basic.html"
if "basic-html" in expected:
with open(basic_html_summary) as fp:
raw = fp.read()
Expand Down
6 changes: 4 additions & 2 deletions tests/test_hash_library.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

import pytest
from helpers import pytester_path


@pytest.mark.parametrize(
Expand All @@ -14,9 +15,10 @@
],
)
def test_config(pytester, ini, cli, kwarg, success_expected):
path = pytester_path(pytester)
hash_libraries = {
"good": pytester.path / "good_hash_library.json",
"bad": pytester.path / "bad_hash_library.json",
"good": path / "good_hash_library.json",
"bad": path / "bad_hash_library.json",
}
ini = rf"mpl-hash-library = {hash_libraries[ini]}" if ini else ""
pytester.makeini(
Expand Down
6 changes: 4 additions & 2 deletions tests/test_results_always.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path

import pytest
from helpers import pytester_path


@pytest.mark.parametrize(
Expand All @@ -14,13 +15,14 @@
],
)
def test_config(pytester, ini, cli, enabled_expected):
path = pytester_path(pytester)
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}
mpl-results-path = {path}
{ini}
"""
)
Expand All @@ -38,4 +40,4 @@ def test_base_style():
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
assert (path / "test_config.test_base_style" / "result.png").exists() == enabled_expected
3 changes: 2 additions & 1 deletion tests/test_results_path.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from helpers import pytester_path


@pytest.mark.parametrize(
Expand Down Expand Up @@ -31,4 +32,4 @@ def test_mpl():
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()
assert (pytester_path(pytester) / expected / "test_config.test_mpl" / "result.png").exists()
6 changes: 4 additions & 2 deletions tests/test_use_full_test_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path

import pytest
from helpers import pytester_path

FULL_TEST_NAME = "test_config.TestClass.test_mpl"
SHORT_TEST_NAME = "test_mpl"
Expand All @@ -19,16 +20,17 @@
],
)
def test_config(pytester, ini, cli, expected_baseline_name, success_expected):
path = pytester_path(pytester)
shutil.copyfile( # Test will only pass if baseline is at expected path
Path(__file__).parent / "baseline" / "2.0.x" / "test_base_style.png",
pytester.path / f"{expected_baseline_name}.png",
path / f"{expected_baseline_name}.png",
)
ini = f"mpl-use-full-test-name = {ini}" if ini is not None else ""
pytester.makeini(
f"""
[pytest]
mpl-default-style = fivethirtyeight
mpl-baseline-path = {pytester.path}
mpl-baseline-path = {path}
{ini}
"""
)
Expand Down

0 comments on commit fd3ca4e

Please sign in to comment.