@@ -100,33 +100,51 @@ public override TestResult Execute(TestExecutionContext context)
100100
101101 if ( resultMessage . Contains ( typeof ( TestTimeoutException ) . FullName ) )
102102 {
103- // retry once
104- context . CurrentResult = context . CurrentTest . MakeTestResult ( ) ;
105- context . CurrentResult = innerCommand . Execute ( context ) ;
103+ return HandleTestTimeout ( context , originalResult ) ;
104+ }
105+ }
106106
107- if ( IsTestFailed ( context ) )
108- {
109- context . CurrentResult . SetResult (
110- ResultState . Error ,
111- "The test timed out twice:" + Environment . NewLine +
112- $ "First attempt: { originalResult . Message } " + Environment . NewLine +
113- $ "Second attempt: { context . CurrentResult . Message } ") ;
114- }
115- else
116- {
117- context . CurrentResult . SetResult (
118- context . CurrentResult . ResultState ,
119- "Test timed out in initial run, but was retried successfully." ) ;
120- }
107+ CheckForIgnoredServiceErrors ( context ) ;
108+ return context . CurrentResult ;
109+ }
121110
111+ private TestResult HandleTestTimeout ( TestExecutionContext context , TestResult originalResult )
112+ {
113+ var results = new List < TestResult > { originalResult } ;
114+
115+ for ( int retryCount = 0 ; retryCount < 2 ; retryCount ++ )
116+ {
117+ // Create a new TestResult instance
118+ context . CurrentResult = context . CurrentTest . MakeTestResult ( ) ;
119+ // Run the test again
120+ context . CurrentResult = innerCommand . Execute ( context ) ;
121+ results . Add ( context . CurrentResult ) ;
122+
123+ if ( ! IsTestFailed ( context ) )
124+ {
125+ context . CurrentResult . SetResult (
126+ ResultState . Success ,
127+ ConstructRetryMessage ( "Test timed out initially, but passed on retry" , results ) ) ;
122128 return context . CurrentResult ;
123129 }
124130 }
125131
126- CheckForIgnoredServiceErrors ( context ) ;
132+ context . CurrentResult . SetResult (
133+ ResultState . Error ,
134+ ConstructRetryMessage ( "The test timed out on all attempts" , results ) ) ;
135+
127136 return context . CurrentResult ;
128137 }
129138
139+ private static string ConstructRetryMessage ( string header , IEnumerable < TestResult > results )
140+ {
141+ var attemptDetails = string . Join ( Environment . NewLine ,
142+ results . Select ( ( r , i ) =>
143+ $ "Attempt { i + 1 } : { r . Message ?? "Passed" } ") ) ;
144+
145+ return header + ":" + Environment . NewLine + attemptDetails + Environment . NewLine ;
146+ }
147+
130148 private void CheckForIgnoredServiceErrors ( TestExecutionContext context )
131149 {
132150 // Check if there are any service errors we should ignore.
0 commit comments