| 1 |
From: Julian Gilbey <[email protected]> |
| 2 |
Date: Thu, 26 Mar 2026 20:42:51 +0100 |
| 3 |
Subject: Skip already passed tests on subsequent pytest runs |
| 4 |
|
| 5 |
Forwarded: not-needed |
| 6 |
Origin: https://github.com/spyder-ide/spyder/blob/5.x/conftest.py |
| 7 |
Last-Update: 2024-05-20 |
| 8 |
|
| 9 |
This is based on the mechanism in Spyder. |
| 10 |
--- |
| 11 |
pyproject.toml | 2 +- |
| 12 |
tests/conftest.py | 34 ++++++++++++++++++++++++++++++++++ |
| 13 |
2 files changed, 35 insertions(+), 1 deletion(-) |
| 14 |
|
| 15 |
diff --git a/pyproject.toml b/pyproject.toml |
| 16 |
index fcda69f..ab08712 100644 |
| 17 |
--- a/pyproject.toml |
| 18 |
+++ b/pyproject.toml |
| 19 |
@@ -119,7 +119,7 @@ minversion = "7.0" |
| 20 |
xfail_strict = true |
| 21 |
log_cli_level = "info" |
| 22 |
addopts = [ |
| 23 |
- "-raXs", "--durations=10", "--color=yes", "--doctest-modules", |
| 24 |
+ "-v", "--durations=10", "--color=no", "--doctest-modules", |
| 25 |
"--showlocals", "--strict-markers", "--strict-config" |
| 26 |
] |
| 27 |
testpaths = [ |
| 28 |
diff --git a/tests/conftest.py b/tests/conftest.py |
| 29 |
index b154b32..10ecf08 100644 |
| 30 |
--- a/tests/conftest.py |
| 31 |
+++ b/tests/conftest.py |
| 32 |
@@ -1,5 +1,6 @@ |
| 33 |
import asyncio |
| 34 |
import os |
| 35 |
+import re |
| 36 |
|
| 37 |
if os.name == "nt": |
| 38 |
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type:ignore |
| 39 |
@@ -16,3 +17,36 @@ pytest_plugins = ["pytest_jupyter", "pytest_jupyter.jupyter_client"] |
| 40 |
@pytest.fixture(autouse=True) |
| 41 |
def setup_environ(jp_environ): |
| 42 |
pass |
| 43 |
+ |
| 44 |
+ |
| 45 |
+def get_passed_tests(): |
| 46 |
+ """ |
| 47 |
+ Get the list of passed tests by inspecting the log generated by pytest. |
| 48 |
+ |
| 49 |
+ This is useful on CIs to restart the test suite from the point where a |
| 50 |
+ segfault was thrown by it. |
| 51 |
+ """ |
| 52 |
+ # This assumes the pytest log is placed next to this file. That's where |
| 53 |
+ # we put it on CIs. |
| 54 |
+ tests = set() |
| 55 |
+ if os.path.isfile('pytest_log.txt'): |
| 56 |
+ # Detect all tests that passed before. |
| 57 |
+ test_re = re.compile(r'(test.*) [^ ]*(SKIPPED|PASSED|XFAIL)') |
| 58 |
+ with open('pytest_log.txt') as logfile: |
| 59 |
+ for line in logfile: |
| 60 |
+ if match := test_re.match(line): |
| 61 |
+ tests.add(match.group(1)) |
| 62 |
+ |
| 63 |
+ return tests |
| 64 |
+ |
| 65 |
+ |
| 66 |
+def pytest_collection_modifyitems(config, items): |
| 67 |
+ """ |
| 68 |
+ Skip tests that passed on a previous run. |
| 69 |
+ """ |
| 70 |
+ passed_tests = get_passed_tests() |
| 71 |
+ skip_passed = pytest.mark.skip(reason="Test passed in previous runs") |
| 72 |
+ |
| 73 |
+ for item in items: |
| 74 |
+ if item.nodeid in passed_tests: |
| 75 |
+ item.add_marker(skip_passed) |