From: Julian Gilbey Date: Thu, 26 Mar 2026 20:42:51 +0100 Subject: Skip already passed tests on subsequent pytest runs Forwarded: not-needed Origin: https://github.com/spyder-ide/spyder/blob/5.x/conftest.py Last-Update: 2024-05-20 This is based on the mechanism in Spyder. --- pyproject.toml | 2 +- tests/conftest.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index fcda69f..ab08712 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -119,7 +119,7 @@ minversion = "7.0" xfail_strict = true log_cli_level = "info" addopts = [ - "-raXs", "--durations=10", "--color=yes", "--doctest-modules", + "-v", "--durations=10", "--color=no", "--doctest-modules", "--showlocals", "--strict-markers", "--strict-config" ] testpaths = [ diff --git a/tests/conftest.py b/tests/conftest.py index b154b32..10ecf08 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ import asyncio import os +import re if os.name == "nt": asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type:ignore @@ -16,3 +17,36 @@ pytest_plugins = ["pytest_jupyter", "pytest_jupyter.jupyter_client"] @pytest.fixture(autouse=True) def setup_environ(jp_environ): pass + + +def get_passed_tests(): + """ + Get the list of passed tests by inspecting the log generated by pytest. + + This is useful on CIs to restart the test suite from the point where a + segfault was thrown by it. + """ + # This assumes the pytest log is placed next to this file. That's where + # we put it on CIs. + tests = set() + if os.path.isfile('pytest_log.txt'): + # Detect all tests that passed before. + test_re = re.compile(r'(test.*) [^ ]*(SKIPPED|PASSED|XFAIL)') + with open('pytest_log.txt') as logfile: + for line in logfile: + if match := test_re.match(line): + tests.add(match.group(1)) + + return tests + + +def pytest_collection_modifyitems(config, items): + """ + Skip tests that passed on a previous run. + """ + passed_tests = get_passed_tests() + skip_passed = pytest.mark.skip(reason="Test passed in previous runs") + + for item in items: + if item.nodeid in passed_tests: + item.add_marker(skip_passed)