From 92bae3f3bec67904dcc95a7a9de927a9deec01c1 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:03:18 +0200 Subject: [PATCH] Add tests highlighting xdist incompatibilities --- tests/conftest.py | 20 +++++ .../test_snapshot_option_update.py | 73 ++++++++++++------- .../test_snapshot_option_warn_unused.py | 12 +-- 3 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..4b9c2766 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,20 @@ +import pytest + +# Constants for testing with extra plugin arguments +_NO_ARGS = [] +_XDIST_ARGS = ["--numprocesses", "auto"] + + +@pytest.fixture( + params=[ + _NO_ARGS, + pytest.param( + _XDIST_ARGS, + marks=pytest.mark.xfail(reason="Not currently compatible with xdist"), + ), + ], + ids=["no_plugin", "with_xdist"], +) +def plugin_args(request: pytest.FixtureRequest) -> list[str]: + """Fixture to test with various plugins""" + return request.param diff --git a/tests/integration/test_snapshot_option_update.py b/tests/integration/test_snapshot_option_update.py index b898c9a8..8e8b82ab 100644 --- a/tests/integration/test_snapshot_option_update.py +++ b/tests/integration/test_snapshot_option_update.py @@ -120,10 +120,12 @@ def run_testcases(testdir, testcases_initial): return result, testdir, testcases_initial -def test_update_failure_shows_snapshot_diff(run_testcases, testcases_updated): +def test_update_failure_shows_snapshot_diff( + run_testcases, testcases_updated, plugin_args +): testdir = run_testcases[1] testdir.makepyfile(**testcases_updated) - result = testdir.runpytest("-vv") + result = testdir.runpytest("-vv", *plugin_args) result.stdout.re_match_lines( ( r".*assert snapshot == \['this', 'will', 'not', 'match'\]", @@ -181,16 +183,18 @@ def test_update_failure_shows_snapshot_diff(run_testcases, testcases_updated): assert result.ret == 1 -def test_update_success_shows_snapshot_report(run_testcases, testcases_updated): +def test_update_success_shows_snapshot_report( + run_testcases, testcases_updated, plugin_args +): testdir = run_testcases[1] testdir.makepyfile(**testcases_updated) - result = testdir.runpytest("-v", "--snapshot-update") + result = testdir.runpytest("-v", "--snapshot-update", *plugin_args) result.stdout.re_match_lines((r"5 snapshots passed\. 5 snapshots updated\.")) assert result.ret == 0 def test_update_targets_only_selected_parametrized_tests_for_update_dash_m( - run_testcases, + run_testcases, plugin_args ): updated_tests = { "test_used": ( @@ -205,7 +209,9 @@ def test_used(snapshot, actual): } testdir = run_testcases[1] testdir.makepyfile(**updated_tests) - result = testdir.runpytest("-v", "--snapshot-update", "-m", "parametrize") + result = testdir.runpytest( + "-v", "--snapshot-update", *plugin_args, "-m", "parametrize" + ) result.stdout.re_match_lines( ( r"1 snapshot passed\. 1 snapshot updated\. 1 unused snapshot deleted\.", @@ -218,7 +224,7 @@ def test_used(snapshot, actual): def test_update_targets_only_selected_parametrized_tests_for_update_dash_k( - run_testcases, + run_testcases, plugin_args ): updated_tests = { "test_used": ( @@ -233,7 +239,9 @@ def test_used(snapshot, actual): } testdir = run_testcases[1] testdir.makepyfile(**updated_tests) - result = testdir.runpytest("-v", "--snapshot-update", "-k", "test_used[2]") + result = testdir.runpytest( + "-v", "--snapshot-update", *plugin_args, "-k", "test_used[2]" + ) result.stdout.re_match_lines((r"1 snapshot updated\.")) assert "Deleted" not in result.stdout.str() snapshot_path = [testdir.tmpdir, "__snapshots__"] @@ -242,7 +250,7 @@ def test_used(snapshot, actual): def test_update_targets_only_selected_parametrized_tests_for_removal_dash_k( - run_testcases, + run_testcases, plugin_args ): updated_tests = { "test_used": ( @@ -257,7 +265,9 @@ def test_used(snapshot, actual): } testdir = run_testcases[1] testdir.makepyfile(**updated_tests) - result = testdir.runpytest("-v", "--snapshot-update", "-k", "test_used[") + result = testdir.runpytest( + "-v", "--snapshot-update", *plugin_args, "-k", "test_used[" + ) result.stdout.re_match_lines( ( r"2 snapshots passed\. 1 unused snapshot deleted\.", @@ -269,7 +279,7 @@ def test_used(snapshot, actual): assert Path(*snapshot_path, "test_updated_1.ambr").exists() -def test_update_targets_only_selected_class_tests_dash_k(testdir): +def test_update_targets_only_selected_class_tests_dash_k(testdir, plugin_args): test_content = """ import pytest @@ -285,12 +295,14 @@ def test_case_2(self, snapshot): testdir.runpytest("-v", "--snapshot-update") assert Path(testdir.tmpdir, "__snapshots__", "test_content.ambr").exists() - result = testdir.runpytest("test_content.py", "-v", "-k test_case_2") + result = testdir.runpytest( + "test_content.py", "-v", *plugin_args, "-k", "test_case_2" + ) result.stdout.re_match_lines((r"1 snapshot passed\.")) assert "snaphot unused" not in result.stdout.str() -def test_update_targets_only_selected_module_tests_dash_k(testdir): +def test_update_targets_only_selected_module_tests_dash_k(testdir, plugin_args): test_content = """ import pytest @@ -305,17 +317,21 @@ def test_case_2(snapshot): testdir.runpytest("-v", "--snapshot-update") assert Path(testdir.tmpdir, "__snapshots__", "test_content.ambr").exists() - result = testdir.runpytest("test_content.py", "-v", "-k test_case_2") + result = testdir.runpytest( + "test_content.py", "-v", *plugin_args, "-k", "test_case_2" + ) result.stdout.re_match_lines((r"1 snapshot passed\.")) assert "snaphot unused" not in result.stdout.str() -def test_update_targets_only_selected_module_tests_nodes(run_testcases): +def test_update_targets_only_selected_module_tests_nodes(run_testcases, plugin_args): testdir = run_testcases[1] snapfile_empty = Path("__snapshots__", "empty_snapfile.ambr") testdir.makefile(".ambr", **{str(snapfile_empty): ""}) testfile = Path(testdir.tmpdir, "test_used.py") - result = testdir.runpytest("-v", f"{testfile}::test_used", "--snapshot-update") + result = testdir.runpytest( + "-v", f"{testfile}::test_used", "--snapshot-update", *plugin_args + ) result.stdout.re_match_lines((r"3 snapshots passed\.")) assert "unused" not in result.stdout.str() assert "updated" not in result.stdout.str() @@ -324,13 +340,16 @@ def test_update_targets_only_selected_module_tests_nodes(run_testcases): assert snapfile_empty.exists() -def test_update_targets_only_selected_module_tests_nodes_pyargs(run_testcases): +def test_update_targets_only_selected_module_tests_nodes_pyargs( + run_testcases, plugin_args +): testdir = run_testcases[1] snapfile_empty = Path("__snapshots__", "empty_snapfile.ambr") testdir.makefile(".ambr", **{str(snapfile_empty): ""}) result = testdir.runpytest( "-v", "--snapshot-update", + *plugin_args, "--pyargs", "test_used::test_used", ) @@ -342,7 +361,9 @@ def test_update_targets_only_selected_module_tests_nodes_pyargs(run_testcases): assert snapfile_empty.exists() -def test_update_targets_only_selected_module_tests_file_for_update(run_testcases): +def test_update_targets_only_selected_module_tests_file_for_update( + run_testcases, plugin_args +): testdir = run_testcases[1] snapfile_empty = Path("__snapshots__", "empty_snapfile.ambr") testdir.makefile(".ambr", **{str(snapfile_empty): ""}) @@ -357,7 +378,7 @@ def test_used(snapshot, actual): """ ) ) - result = testdir.runpytest("-v", "test_used.py", "--snapshot-update") + result = testdir.runpytest("-v", "test_used.py", "--snapshot-update", *plugin_args) result.stdout.re_match_lines( ( r"3 snapshots passed\. 2 unused snapshots deleted\.", @@ -369,7 +390,9 @@ def test_used(snapshot, actual): assert Path("__snapshots__", "test_used.ambr").exists() -def test_update_targets_only_selected_module_tests_file_for_removal(run_testcases): +def test_update_targets_only_selected_module_tests_file_for_removal( + run_testcases, plugin_args +): testdir = run_testcases[1] testdir.makepyfile( test_used=( @@ -381,7 +404,7 @@ def test_used(snapshot): ) snapfile_empty = Path("__snapshots__", "empty_snapfile.ambr") testdir.makefile(".ambr", **{str(snapfile_empty): ""}) - result = testdir.runpytest("-v", "test_used.py", "--snapshot-update") + result = testdir.runpytest("-v", "test_used.py", "--snapshot-update", *plugin_args) result.stdout.re_match_lines( ( r"5 unused snapshots deleted\.", @@ -394,12 +417,12 @@ def test_used(snapshot): assert not Path("__snapshots__", "test_used.ambr").exists() -def test_update_removes_empty_snapshot_collection_only(run_testcases): +def test_update_removes_empty_snapshot_collection_only(run_testcases, plugin_args): testdir = run_testcases[1] snapfile_empty = Path("__snapshots__", "empty_snapfile.ambr") testdir.makefile(".ambr", **{str(snapfile_empty): ""}) assert snapfile_empty.exists() - result = testdir.runpytest("-v", "--snapshot-update") + result = testdir.runpytest("-v", "--snapshot-update", *plugin_args) result.stdout.re_match_lines( ( r"10 snapshots passed\. 1 unused snapshot deleted\.", @@ -412,13 +435,13 @@ def test_update_removes_empty_snapshot_collection_only(run_testcases): assert Path("__snapshots__", "test_used.ambr").exists() -def test_update_removes_hanging_snapshot_collection_file(run_testcases): +def test_update_removes_hanging_snapshot_collection_file(run_testcases, plugin_args): testdir = run_testcases[1] snapfile_used = Path("__snapshots__", "test_used.ambr") snapfile_hanging = Path("__snapshots__", "hanging_snapfile.abc") testdir.makefile(".abc", **{str(snapfile_hanging): ""}) assert snapfile_hanging.exists() - result = testdir.runpytest("-v", "--snapshot-update") + result = testdir.runpytest("-v", "--snapshot-update", *plugin_args) result.stdout.re_match_lines( ( r"10 snapshots passed\. 1 unused snapshot deleted\.", diff --git a/tests/integration/test_snapshot_option_warn_unused.py b/tests/integration/test_snapshot_option_warn_unused.py index f9a36134..40f3ee85 100644 --- a/tests/integration/test_snapshot_option_warn_unused.py +++ b/tests/integration/test_snapshot_option_warn_unused.py @@ -28,11 +28,11 @@ def run_testcases(testdir, testcases): return testdir, testcases -def test_unused_snapshots_failure(run_testcases): +def test_unused_snapshots_failure(run_testcases, plugin_args): testdir, testcases = run_testcases testdir.makepyfile(test_file=testcases["used"]) - result = testdir.runpytest("-v") + result = testdir.runpytest("-v", *plugin_args) result.stdout.re_match_lines( ( r"1 snapshot passed\. 1 snapshot unused\.", @@ -42,11 +42,11 @@ def test_unused_snapshots_failure(run_testcases): assert result.ret == 1 -def test_unused_snapshots_warning(run_testcases): +def test_unused_snapshots_warning(run_testcases, plugin_args): testdir, testcases = run_testcases testdir.makepyfile(test_file=testcases["used"]) - result = testdir.runpytest("-v", "--snapshot-warn-unused") + result = testdir.runpytest("-v", "--snapshot-warn-unused", *plugin_args) result.stdout.re_match_lines( ( r"1 snapshot passed\. 1 snapshot unused\.", @@ -56,11 +56,11 @@ def test_unused_snapshots_warning(run_testcases): assert result.ret == 0 -def test_unused_snapshots_deletion(run_testcases): +def test_unused_snapshots_deletion(run_testcases, plugin_args): testdir, testcases = run_testcases testdir.makepyfile(test_file=testcases["used"]) - result = testdir.runpytest("-v", "--snapshot-update") + result = testdir.runpytest("-v", "--snapshot-update", *plugin_args) result.stdout.re_match_lines( ( r"1 snapshot passed\. 1 unused snapshot deleted\.",