Skip to content

Commit a606407

Browse files
authored
fix(ci-insights): Filter existing tests that are not related to the session (#259)
The goal is to only keep tests related to the current pytest session. We need to do this because we are using the length of the existing tests to determine the budget to allocate to flaky detection.
1 parent 6331f0e commit a606407

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

pytest_mergify/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ def pytest_sessionstart(self, session: _pytest.main.Session) -> None:
139139
if self.mergify_ci.flaky_detector:
140140
self.mergify_ci.flaky_detector.set_deadline()
141141

142+
def pytest_collection_finish(self, session: _pytest.main.Session) -> None:
143+
if self.mergify_ci.flaky_detector:
144+
self.mergify_ci.flaky_detector.filter_existing_tests_with_session(session)
145+
142146
@pytest.hookimpl(hookwrapper=True)
143147
def pytest_sessionfinish(
144148
self,

pytest_mergify/flaky_detection.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import typing
55

66
import _pytest
7-
import _pytest.nodes
7+
import _pytest.main
88
import _pytest.reports
99
import requests
1010

@@ -123,6 +123,12 @@ def detect_from_report(self, report: _pytest.reports.TestReport) -> bool:
123123

124124
return True
125125

126+
def filter_existing_tests_with_session(self, session: _pytest.main.Session) -> None:
127+
session_tests = {item.nodeid for item in session.items}
128+
self._context.existing_test_names = [
129+
test for test in self._context.existing_test_names if test in session_tests
130+
]
131+
126132
def get_retry_count_for_new_test(self, test: str) -> int:
127133
metrics = self._new_test_metrics.get(test)
128134
if not metrics:

tests/test_ci_insights.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import typing
44

55
import _pytest.nodes
6+
import _pytest.pytester
67
import _pytest.reports
78
import pytest
89
import responses
@@ -413,6 +414,42 @@ def test_new():
413414
)
414415

415416

417+
@responses.activate
418+
def test_flaky_detector_filter_existing_tests_with_session(
419+
monkeypatch: pytest.MonkeyPatch,
420+
pytester: _pytest.pytester.Pytester,
421+
) -> None:
422+
_set_test_environment(monkeypatch)
423+
_make_quarantine_mock()
424+
_make_flaky_detection_context_mock(
425+
existing_test_names=[
426+
"test_flaky_detector_filter_existing_tests_with_session.py::test_foo",
427+
"test_flaky_detector_filter_existing_tests_with_session.py::test_bar",
428+
"test_flaky_detector_filter_existing_tests_with_session.py::test_baz", # Unknown test, should be filtered.
429+
]
430+
)
431+
432+
pytester.makepyfile(
433+
"""
434+
def test_foo():
435+
assert True
436+
437+
def test_bar():
438+
assert True
439+
"""
440+
)
441+
442+
plugin = pytest_mergify.PytestMergify()
443+
444+
result = pytester.runpytest_inprocess(plugins=[plugin])
445+
result.assert_outcomes(passed=2) # 2 existing tests.
446+
447+
assert plugin.mergify_ci.flaky_detector is not None
448+
449+
# Unknown test should have been filtered out after collection.
450+
assert len(plugin.mergify_ci.flaky_detector._context.existing_test_names) == 2
451+
452+
416453
def _get_span_counts(
417454
spans: typing.Dict[str, trace.ReadableSpan],
418455
) -> typing.Dict[str, int]:

0 commit comments

Comments
 (0)