diff --git a/tests/helpers.py b/tests/helpers.py new file mode 100644 index 0000000..037343d --- /dev/null +++ b/tests/helpers.py @@ -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 diff --git a/tests/test_baseline_path.py b/tests/test_baseline_path.py index 767baea..745006a 100644 --- a/tests/test_baseline_path.py +++ b/tests/test_baseline_path.py @@ -2,6 +2,7 @@ from pathlib import Path import pytest +from helpers import pytester_path @pytest.mark.parametrize( @@ -15,12 +16,13 @@ ], ) 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] @@ -28,7 +30,7 @@ def test_config(pytester, ini, cli, kwarg, expected_baseline_path, success_expec {ini} """ ) - kwarg = f"baseline_dir=r'{pytester.path / kwarg}'" if kwarg else "" + kwarg = f"baseline_dir=r'{path / kwarg}'" if kwarg else "" pytester.makepyfile( f""" import matplotlib.pyplot as plt @@ -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) diff --git a/tests/test_default_style.py b/tests/test_default_style.py index a193d54..9088008 100644 --- a/tests/test_default_style.py +++ b/tests/test_default_style.py @@ -1,4 +1,5 @@ import pytest +from helpers import pytester_path @pytest.mark.parametrize( @@ -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} """ ) diff --git a/tests/test_generate.py b/tests/test_generate.py index c87c8d6..61b1447 100644 --- a/tests/test_generate.py +++ b/tests/test_generate.py @@ -1,3 +1,5 @@ +from helpers import pytester_path + PYFILE = ( """ import matplotlib.pyplot as plt @@ -13,7 +15,7 @@ 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() @@ -21,20 +23,22 @@ def test_generate_baseline_images(pytester): 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}", diff --git a/tests/test_generate_summary.py b/tests/test_generate_summary.py index 3632207..6350975 100644 --- a/tests/test_generate_summary.py +++ b/tests/test_generate_summary.py @@ -1,6 +1,7 @@ import json import pytest +from helpers import pytester_path @pytest.mark.parametrize( @@ -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} """ ) @@ -36,7 +38,7 @@ 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) @@ -44,7 +46,7 @@ def test_mpl(): 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() @@ -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() diff --git a/tests/test_hash_library.py b/tests/test_hash_library.py index 893f270..58c457b 100644 --- a/tests/test_hash_library.py +++ b/tests/test_hash_library.py @@ -1,6 +1,7 @@ import json import pytest +from helpers import pytester_path @pytest.mark.parametrize( @@ -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 = f"mpl-hash-library = {hash_libraries[ini]}" if ini else "" pytester.makeini( diff --git a/tests/test_results_always.py b/tests/test_results_always.py index c39f5b3..ff2cc84 100644 --- a/tests/test_results_always.py +++ b/tests/test_results_always.py @@ -1,6 +1,7 @@ from pathlib import Path import pytest +from helpers import pytester_path @pytest.mark.parametrize( @@ -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} """ ) @@ -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 diff --git a/tests/test_results_path.py b/tests/test_results_path.py index 962a210..a727c2a 100644 --- a/tests/test_results_path.py +++ b/tests/test_results_path.py @@ -1,4 +1,5 @@ import pytest +from helpers import pytester_path @pytest.mark.parametrize( @@ -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() diff --git a/tests/test_use_full_test_name.py b/tests/test_use_full_test_name.py index f78bd48..56aaca5 100644 --- a/tests/test_use_full_test_name.py +++ b/tests/test_use_full_test_name.py @@ -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" @@ -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} """ )