11import datetime
22import typing
33
4+ import _pytest
5+ import _pytest .reports
46import freezegun
57
68from pytest_mergify import flaky_detection
@@ -22,6 +24,7 @@ def __init__(self) -> None:
2224 self .url = ""
2325 self .full_repository_name = ""
2426 self .mode = "new"
27+ self ._test_metrics = {}
2528
2629 def __post_init__ (self ) -> None :
2730 pass
@@ -59,6 +62,38 @@ def test_flaky_detector_get_duration_before_deadline() -> None:
5962 assert detector ._get_duration_before_deadline () == datetime .timedelta (seconds = 10 )
6063
6164
65+ def test_flaky_detector_detect_from_report () -> None :
66+ def make_report (
67+ nodeid : str , when : typing .Literal ["setup" , "call" , "teardown" ], duration : float
68+ ) -> _pytest .reports .TestReport :
69+ return _pytest .reports .TestReport (
70+ duration = duration ,
71+ keywords = {},
72+ location = ("" , None , "" ),
73+ longrepr = None ,
74+ nodeid = nodeid ,
75+ outcome = "passed" ,
76+ when = when ,
77+ )
78+
79+ detector = InitializedFlakyDetector ()
80+ detector ._context = _make_flaky_detection_context (max_test_name_length = 100 )
81+
82+ detector .detect_from_report (make_report (nodeid = "foo" , when = "setup" , duration = 1 ))
83+ detector .detect_from_report (make_report (nodeid = "foo" , when = "call" , duration = 2 ))
84+ detector .detect_from_report (make_report (nodeid = "foo" , when = "teardown" , duration = 3 ))
85+
86+ detector .detect_from_report (make_report (nodeid = "foo" , when = "setup" , duration = 4 ))
87+ detector .detect_from_report (make_report (nodeid = "foo" , when = "call" , duration = 5 ))
88+ detector .detect_from_report (make_report (nodeid = "foo" , when = "teardown" , duration = 6 ))
89+
90+ metrics = detector ._test_metrics .get ("foo" )
91+ assert metrics is not None
92+ assert metrics .initial_duration == datetime .timedelta (seconds = 6 )
93+ assert metrics .rerun_count == 2
94+ assert metrics .total_duration == datetime .timedelta (seconds = 21 )
95+
96+
6297def test_flaky_detector_count_remaining_tests () -> None :
6398 detector = InitializedFlakyDetector ()
6499 detector ._test_metrics = {
@@ -79,11 +114,11 @@ def test_flaky_detector_get_rerun_count_for_test() -> None:
79114 )
80115 detector ._test_metrics = {
81116 "foo" : flaky_detection ._TestMetrics (
82- initial_duration = datetime .timedelta (milliseconds = 10 ),
117+ initial_call_duration = datetime .timedelta (milliseconds = 10 ),
83118 is_processed = True ,
84119 ),
85120 "bar" : flaky_detection ._TestMetrics (
86- initial_duration = datetime .timedelta (milliseconds = 100 ),
121+ initial_call_duration = datetime .timedelta (milliseconds = 100 ),
87122 ),
88123 "baz" : flaky_detection ._TestMetrics (),
89124 }
@@ -103,11 +138,11 @@ def test_flaky_detector_get_rerun_count_for_test_with_slow_test() -> None:
103138 detector ._test_metrics = {
104139 "foo" : flaky_detection ._TestMetrics (
105140 # Can't be reran 5 times within the budget.
106- initial_duration = datetime .timedelta (seconds = 1 ),
141+ initial_call_duration = datetime .timedelta (seconds = 1 ),
107142 ),
108143 "bar" : flaky_detection ._TestMetrics (
109144 # This test should not be impacted by the previous one.
110- initial_duration = datetime .timedelta (milliseconds = 1 ),
145+ initial_call_duration = datetime .timedelta (milliseconds = 1 ),
111146 ),
112147 }
113148 detector .set_deadline ()
@@ -128,7 +163,7 @@ def test_flaky_detector_get_rerun_count_for_test_with_fast_test() -> None:
128163 detector ._test_metrics = {
129164 "foo" : flaky_detection ._TestMetrics (
130165 # Should only be reran 1000 times, freeing the rest of the budget for other tests.
131- initial_duration = datetime .timedelta (milliseconds = 1 ),
166+ initial_call_duration = datetime .timedelta (milliseconds = 1 ),
132167 ),
133168 }
134169 detector .set_deadline ()
@@ -146,7 +181,7 @@ def test_flaky_detector_get_rerun_count_for_test_with_timeout() -> None:
146181 )
147182 detector ._test_metrics = {
148183 "foo" : flaky_detection ._TestMetrics (
149- initial_duration = datetime .timedelta (milliseconds = 4 ),
184+ initial_call_duration = datetime .timedelta (milliseconds = 4 ),
150185 ),
151186 }
152187 detector .set_deadline ()
0 commit comments