Skip to content

Commit fd3ca4e

Browse files
committed
Add pytest v5 pytester compatibility
1 parent 14037b2 commit fd3ca4e

9 files changed

+46
-23
lines changed

tests/helpers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from pathlib import Path
2+
3+
4+
def pytester_path(pytester):
5+
if hasattr(pytester, "path"):
6+
return pytester.path
7+
return Path(pytester.tmpdir) # pytest v5

tests/test_baseline_path.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from pathlib import Path
33

44
import pytest
5+
from helpers import pytester_path
56

67

78
@pytest.mark.parametrize(
@@ -15,20 +16,21 @@
1516
],
1617
)
1718
def test_config(pytester, ini, cli, kwarg, expected_baseline_path, success_expected):
18-
(pytester.path / expected_baseline_path).mkdir()
19+
path = pytester_path(pytester)
20+
(path / expected_baseline_path).mkdir()
1921
shutil.copyfile( # Test will only pass if baseline is at expected path
2022
Path(__file__).parent / "baseline" / "2.0.x" / "test_base_style.png",
21-
pytester.path / expected_baseline_path / "test_mpl.png",
23+
path / expected_baseline_path / "test_mpl.png",
2224
)
23-
ini = f"mpl-baseline-path = {pytester.path / ini}" if ini is not None else ""
25+
ini = f"mpl-baseline-path = {path / ini}" if ini is not None else ""
2426
pytester.makeini(
2527
f"""
2628
[pytest]
2729
mpl-default-style = fivethirtyeight
2830
{ini}
2931
"""
3032
)
31-
kwarg = f"baseline_dir='{pytester.path / kwarg}'" if kwarg else ""
33+
kwarg = f"baseline_dir='{path / kwarg}'" if kwarg else ""
3234
pytester.makepyfile(
3335
f"""
3436
import matplotlib.pyplot as plt
@@ -40,7 +42,7 @@ def test_mpl():
4042
return fig
4143
"""
4244
)
43-
cli = f"--mpl-baseline-path={pytester.path / cli}" if cli else ""
45+
cli = f"--mpl-baseline-path={path / cli}" if cli else ""
4446
result = pytester.runpytest("--mpl", cli)
4547
if success_expected:
4648
result.assert_outcomes(passed=1)

tests/test_default_style.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from helpers import pytester_path
23

34

45
@pytest.mark.parametrize(
@@ -14,7 +15,7 @@ def test_config(pytester, ini, cli, kwarg, expected):
1415
pytester.makeini(
1516
f"""
1617
[pytest]
17-
mpl-baseline-path = {pytester.path}
18+
mpl-baseline-path = {pytester_path(pytester)}
1819
{ini}
1920
"""
2021
)

tests/test_generate.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from helpers import pytester_path
2+
13
PYFILE = (
24
"""
35
import matplotlib.pyplot as plt
@@ -13,28 +15,30 @@ def test_mpl():
1315

1416
def test_generate_baseline_images(pytester):
1517
pytester.makepyfile(PYFILE)
16-
baseline_dir = pytester.path / "alternative_baseline"
18+
baseline_dir = pytester_path(pytester) / "alternative_baseline"
1719
result = pytester.runpytest(f"--mpl-generate-path={baseline_dir}")
1820
result.assert_outcomes(skipped=1)
1921
assert (baseline_dir / "test_mpl.png").exists()
2022

2123

2224
def test_generate_baseline_hashes(pytester):
2325
pytester.makepyfile(PYFILE)
24-
hash_library = pytester.path / "alternative_baseline" / "hash_library_1.json"
26+
path = pytester_path(pytester)
27+
hash_library = path / "alternative_baseline" / "hash_library_1.json"
2528
result = pytester.runpytest(
2629
f"--mpl-generate-hash-library={hash_library}",
27-
f"--mpl-results-path={pytester.path}",
30+
f"--mpl-results-path={path}",
2831
)
2932
result.assert_outcomes(failed=1) # this option enables --mpl
3033
assert hash_library.exists()
31-
assert (pytester.path / "test_generate_baseline_hashes.test_mpl" / "result.png").exists()
34+
assert (path / "test_generate_baseline_hashes.test_mpl" / "result.png").exists()
3235

3336

3437
def test_generate_baseline_images_and_hashes(pytester):
3538
pytester.makepyfile(PYFILE)
36-
baseline_dir = pytester.path / "alternative_baseline"
37-
hash_library = pytester.path / "alternative_baseline" / "hash_library_1.json"
39+
path = pytester_path(pytester)
40+
baseline_dir = path / "alternative_baseline"
41+
hash_library = path / "alternative_baseline" / "hash_library_1.json"
3842
result = pytester.runpytest(
3943
f"--mpl-generate-path={baseline_dir}",
4044
f"--mpl-generate-hash-library={hash_library}",

tests/test_generate_summary.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22

33
import pytest
4+
from helpers import pytester_path
45

56

67
@pytest.mark.parametrize(
@@ -13,11 +14,12 @@
1314
],
1415
)
1516
def test_config(pytester, ini, cli, expected):
17+
path = pytester_path(pytester)
1618
ini = f"mpl-generate-summary = {ini}" if ini else ""
1719
pytester.makeini(
1820
f"""
1921
[pytest]
20-
mpl-results-path = {pytester.path}
22+
mpl-results-path = {path}
2123
{ini}
2224
"""
2325
)
@@ -36,15 +38,15 @@ def test_mpl():
3638
result = pytester.runpytest("--mpl", cli)
3739
result.assert_outcomes(failed=1)
3840

39-
json_summary = pytester.path / "results.json"
41+
json_summary = path / "results.json"
4042
if "json" in expected:
4143
with open(json_summary) as fp:
4244
results = json.load(fp)
4345
assert "test_config.test_mpl" in results
4446
else:
4547
assert not json_summary.exists()
4648

47-
html_summary = pytester.path / "fig_comparison.html"
49+
html_summary = path / "fig_comparison.html"
4850
if "html" in expected:
4951
with open(html_summary) as fp:
5052
raw = fp.read()
@@ -53,7 +55,7 @@ def test_mpl():
5355
else:
5456
assert not html_summary.exists()
5557

56-
basic_html_summary = pytester.path / "fig_comparison_basic.html"
58+
basic_html_summary = path / "fig_comparison_basic.html"
5759
if "basic-html" in expected:
5860
with open(basic_html_summary) as fp:
5961
raw = fp.read()

tests/test_hash_library.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22

33
import pytest
4+
from helpers import pytester_path
45

56

67
@pytest.mark.parametrize(
@@ -14,9 +15,10 @@
1415
],
1516
)
1617
def test_config(pytester, ini, cli, kwarg, success_expected):
18+
path = pytester_path(pytester)
1719
hash_libraries = {
18-
"good": pytester.path / "good_hash_library.json",
19-
"bad": pytester.path / "bad_hash_library.json",
20+
"good": path / "good_hash_library.json",
21+
"bad": path / "bad_hash_library.json",
2022
}
2123
ini = rf"mpl-hash-library = {hash_libraries[ini]}" if ini else ""
2224
pytester.makeini(

tests/test_results_always.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pathlib import Path
22

33
import pytest
4+
from helpers import pytester_path
45

56

67
@pytest.mark.parametrize(
@@ -14,13 +15,14 @@
1415
],
1516
)
1617
def test_config(pytester, ini, cli, enabled_expected):
18+
path = pytester_path(pytester)
1719
ini = f"mpl-results-always = {ini}" if ini else ""
1820
pytester.makeini(
1921
f"""
2022
[pytest]
2123
mpl-default-style = fivethirtyeight
2224
mpl-baseline-path = {Path(__file__).parent / "baseline" / "2.0.x"}
23-
mpl-results-path = {pytester.path}
25+
mpl-results-path = {path}
2426
{ini}
2527
"""
2628
)
@@ -38,4 +40,4 @@ def test_base_style():
3840
cli = "--mpl-results-always" if cli else ""
3941
result = pytester.runpytest("--mpl", cli)
4042
result.assert_outcomes(passed=1)
41-
assert (pytester.path / "test_config.test_base_style" / "result.png").exists() == enabled_expected
43+
assert (path / "test_config.test_base_style" / "result.png").exists() == enabled_expected

tests/test_results_path.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from helpers import pytester_path
23

34

45
@pytest.mark.parametrize(
@@ -31,4 +32,4 @@ def test_mpl():
3132
cli = f"--mpl-results-path={cli}" if cli else ""
3233
result = pytester.runpytest("--mpl", cli)
3334
result.assert_outcomes(failed=1)
34-
assert (pytester.path / expected / "test_config.test_mpl" / "result.png").exists()
35+
assert (pytester_path(pytester) / expected / "test_config.test_mpl" / "result.png").exists()

tests/test_use_full_test_name.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from pathlib import Path
33

44
import pytest
5+
from helpers import pytester_path
56

67
FULL_TEST_NAME = "test_config.TestClass.test_mpl"
78
SHORT_TEST_NAME = "test_mpl"
@@ -19,16 +20,17 @@
1920
],
2021
)
2122
def test_config(pytester, ini, cli, expected_baseline_name, success_expected):
23+
path = pytester_path(pytester)
2224
shutil.copyfile( # Test will only pass if baseline is at expected path
2325
Path(__file__).parent / "baseline" / "2.0.x" / "test_base_style.png",
24-
pytester.path / f"{expected_baseline_name}.png",
26+
path / f"{expected_baseline_name}.png",
2527
)
2628
ini = f"mpl-use-full-test-name = {ini}" if ini is not None else ""
2729
pytester.makeini(
2830
f"""
2931
[pytest]
3032
mpl-default-style = fivethirtyeight
31-
mpl-baseline-path = {pytester.path}
33+
mpl-baseline-path = {path}
3234
{ini}
3335
"""
3436
)

0 commit comments

Comments
 (0)