Skip to content

chore: add test retries - #2257

Merged
Max Schmitt (mxschmitt) merged 16 commits into
microsoft:mainfrom
mxschmitt:feature/add-test-retries
Aug 24, 2022
Merged

Max Schmitt (mxschmitt) merged 16 commits into
microsoft:mainfrom
mxschmitt:feature/add-test-retries

Conversation

@mxschmitt

@mxschmitt Max Schmitt (mxschmitt) commented Aug 17, 2022 •

Copy link
Copy Markdown
Contributor

This PR adds the basic infra to implement retries inside the Microsoft.Playwright.NUnit and Microsoft.Playwright.MSTest harnesses. It's done via C# attributes which override the execution logic and re-runs it until the max-retries is reached or the test has passed.

The Test2RetryCount data structure is a naive way of sharing the retry-count with the actual test execution (by full classname/method name as a key), which is required for implementing screenshot/video/tracing on-first-retry (follow-up work). We now use Test Properties for it.

Follow-up work related to retries implementation:

Relates #2214

Example MSTest output
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
/Users/max/development/playwright-dotnet/src/Playwright.TestingHarnessTest/bin/Debug/net6.0/Playwright.TestingHarnessTest.dll
NUnit Adapter 4.2.0.0: Test execution started
Running selected tests in /Users/max/development/playwright-dotnet/src/Playwright.TestingHarnessTest/bin/Debug/net6.0/Playwright.TestingHarnessTest.dll
 NUnit3TestExecutor discovered 0 of 0 NUnit test cases using Current Discovery mode, Explicit run
NUnit Adapter 4.2.0.0: Test execution complete
Passed Test [615 ms]
Standard Output Messages:
i-was-running-stdout4

================================================================================
Test was retried 4 times.
Failing test runs:
----------------------------------------
 Test: Playwright.TestingHarnessTest.MSTest.mstestretryspectsshouldretryafailedtestandstoponceitpassed (retry #0)
 Outcome: Failed
 Exception: 
   Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel.TestFailedException: Test method Playwright.TestingHarnessTest.MSTest.mstestretryspectsshouldretryafailedtestandstoponceitpassed.Test threw exception: 
   System.Exception: i-was-broken
    ---> System.Exception: i-was-broken
      at Playwright.TestingHarnessTest.MSTest.mstestretryspectsshouldretryafailedtestandstoponceitpassed.Test() in /Users/max/development/playwright-dotnet/src/Playwright.TestingHarnessTest/ExampleTests.cs:line 19
      --- End of inner exception stack trace ---
 Standard Output Messages: 
   i-was-running-stdout0
 Standard Error Messages: 
   i-was-running-stderr0
----------------------------------------
 Test: Playwright.TestingHarnessTest.MSTest.mstestretryspectsshouldretryafailedtestandstoponceitpassed (retry #1)
 Outcome: Failed
 Exception: 
   Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel.TestFailedException: Test method Playwright.TestingHarnessTest.MSTest.mstestretryspectsshouldretryafailedtestandstoponceitpassed.Test threw exception: 
   System.Exception: i-was-broken
    ---> System.Exception: i-was-broken
      at Playwright.TestingHarnessTest.MSTest.mstestretryspectsshouldretryafailedtestandstoponceitpassed.Test() in /Users/max/development/playwright-dotnet/src/Playwright.TestingHarnessTest/ExampleTests.cs:line 19
      --- End of inner exception stack trace ---
 Standard Output Messages: 
   i-was-running-stdout1
 Standard Error Messages: 
   i-was-running-stderr1
----------------------------------------
 Test: Playwright.TestingHarnessTest.MSTest.mstestretryspectsshouldretryafailedtestandstoponceitpassed (retry #2)
 Outcome: Failed
 Exception: 
   Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel.TestFailedException: Test method Playwright.TestingHarnessTest.MSTest.mstestretryspectsshouldretryafailedtestandstoponceitpassed.Test threw exception: 
   System.Exception: i-was-broken
    ---> System.Exception: i-was-broken
      at Playwright.TestingHarnessTest.MSTest.mstestretryspectsshouldretryafailedtestandstoponceitpassed.Test() in /Users/max/development/playwright-dotnet/src/Playwright.TestingHarnessTest/ExampleTests.cs:line 19
      --- End of inner exception stack trace ---
 Standard Output Messages: 
   i-was-running-stdout2
 Standard Error Messages: 
   i-was-running-stderr2
----------------------------------------
 Test: Playwright.TestingHarnessTest.MSTest.mstestretryspectsshouldretryafailedtestandstoponceitpassed (retry #3)
 Outcome: Failed
 Exception: 
   Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel.TestFailedException: Test method Playwright.TestingHarnessTest.MSTest.mstestretryspectsshouldretryafailedtestandstoponceitpassed.Test threw exception: 
   System.Exception: i-was-broken
    ---> System.Exception: i-was-broken
      at Playwright.TestingHarnessTest.MSTest.mstestretryspectsshouldretryafailedtestandstoponceitpassed.Test() in /Users/max/development/playwright-dotnet/src/Playwright.TestingHarnessTest/ExampleTests.cs:line 19
      --- End of inner exception stack trace ---
 Standard Output Messages: 
   i-was-running-stdout3
 Standard Error Messages: 
   i-was-running-stderr3
================================================================================
Standard Error Messages:
i-was-running-stderr4

Html test results file : /Users/max/development/playwright-dotnet/src/Playwright.TestingHarnessTest/TestResults/testResults.html

Test Run Successful.
Total tests: 1
   Passed: 1
Total time: 6.5994 Seconds

Example NUnit output
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
/Users/max/development/playwright-dotnet/src/Playwright.TestingHarnessTest/bin/Debug/net6.0/Playwright.TestingHarnessTest.dll
NUnit Adapter 4.2.0.0: Test execution started
Running selected tests in /Users/max/development/playwright-dotnet/src/Playwright.TestingHarnessTest/bin/Debug/net6.0/Playwright.TestingHarnessTest.dll
 NUnit3TestExecutor discovered 1 of 1 NUnit test cases using Current Discovery mode, Non-Explicit run
i-was-running-stderr0
i-was-running-stderr1
i-was-running-stderr2
i-was-running-stderr3
i-was-running-stderr4
i-was-running-stdout4

================================================================================
Test was retried 4 times.
Failing test runs:
----------------------------------------
Test: Playwright.TestingHarnessTest.NUnit.nunitretryspectsshouldretryafailedtestandstoponceitpassed.Test
Outcome: Failed:Error
Standard Output Messages: 
  i-was-running-stdout1
Message 
  System.Exception : i-was-broken
----------------------------------------
Test: Playwright.TestingHarnessTest.NUnit.nunitretryspectsshouldretryafailedtestandstoponceitpassed.Test
Outcome: Failed:Error
Standard Output Messages: 
  i-was-running-stdout2
Message 
  System.Exception : i-was-broken
----------------------------------------
Test: Playwright.TestingHarnessTest.NUnit.nunitretryspectsshouldretryafailedtestandstoponceitpassed.Test
Outcome: Failed:Error
Standard Output Messages: 
  i-was-running-stdout3
Message 
  System.Exception : i-was-broken
----------------------------------------
Test: Playwright.TestingHarnessTest.NUnit.nunitretryspectsshouldretryafailedtestandstoponceitpassed.Test
Outcome: Passed
Standard Output Messages: 
  i-was-running-stdout4
================================================================================
NUnit Adapter 4.2.0.0: Test execution complete
Passed Test [2 s]
Standard Output Messages:
i-was-running-stdout4

================================================================================
Test was retried 4 times.
Failing test runs:
----------------------------------------
 Test: Playwright.TestingHarnessTest.NUnit.nunitretryspectsshouldretryafailedtestandstoponceitpassed.Test
 Outcome: Failed:Error
 Standard Output Messages: 
   i-was-running-stdout1
 Message 
   System.Exception : i-was-broken
----------------------------------------
 Test: Playwright.TestingHarnessTest.NUnit.nunitretryspectsshouldretryafailedtestandstoponceitpassed.Test
 Outcome: Failed:Error
 Standard Output Messages: 
   i-was-running-stdout2
 Message 
   System.Exception : i-was-broken
----------------------------------------
 Test: Playwright.TestingHarnessTest.NUnit.nunitretryspectsshouldretryafailedtestandstoponceitpassed.Test
 Outcome: Failed:Error
 Standard Output Messages: 
   i-was-running-stdout3
 Message 
   System.Exception : i-was-broken
----------------------------------------
 Test: Playwright.TestingHarnessTest.NUnit.nunitretryspectsshouldretryafailedtestandstoponceitpassed.Test
 Outcome: Passed
 Standard Output Messages: 
   i-was-running-stdout4
================================================================================
Standard Error Messages:
i-was-running-stderr0
i-was-running-stderr1
i-was-running-stderr2
i-was-running-stderr3
i-was-running-stderr4

Html test results file : /Users/max/development/playwright-dotnet/src/Playwright.TestingHarnessTest/TestResults/testResults.html

Test Run Successful.
Total tests: 1
   Passed: 1
Total time: 3.4741 Seconds

@mxschmitt
Max Schmitt (mxschmitt) marked this pull request as ready for review August 19, 2022 20:38
}

public TestContext? TestContext { get; set; }
public TestContext TestContext { get; set; } = null!;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by fix.

}

// https://github.com/microsoft/vstest/blob/200a783858425e5ac6f4ebb8f87a7811b0ad39e3/src/vstest.console/Internal/ConsoleLogger.cs#L319-L343
function extractVstestMessages(stdout: string, prefix: string): string {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nunit/mstest or more in general the visual studio test platform will report the stdout/stderr after each test run with a prefix and two blank lines as a end. This parses them so we can easier process them. This seemed easier to trx, but can be changed most likely to get it from trx instead.

catch (Exception ex)
{
context.CurrentResult = context.CurrentTest.MakeTestResult();
context.CurrentResult.RecordException(ex);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have to handle this? I'd think that innerCommand should already do proper error reporting?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception handling needs to be done manually, otherwise it throws and the parent method does catch it.

See e.g. here: https://github.com/nunit/nunit/blob/master/src/NUnitFramework/framework/Attributes/RetryAttribute.cs#L75-L85

while (Test2RetryCount[key] < PlaywrightSettingsProvider.Retries)
{
Test2RetryCount[key]++;
results = base.Execute(testMethod);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we append to results here, so that line 64 returns all results?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a discussion with pf we wanted to implemented for now a primitive approach, since if we return a failed test inside that list, it will result with exitCode 1 which we want to prevent.

Comment on lines +74 to 77
public static int Retries
{
get => _settings?.Retries ?? 0;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static int Retries
{
get => _settings?.Retries ?? 0;
}
public static int Retries => _settings?.Retries ?? 0;

@@ -57,6 +57,10 @@ public PlaywrightSettingsXml(XmlReader reader)
reader.Read();
ExpectTimeout = float.Parse(reader.Value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will use the CurrentCulture is that intentional?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems unrelated to that PR do you mind filing an issue with more details or are you interested in contributing a fix to it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants