Keep track of the current attempt count and make it accessible in the TestContext when running tests with the new Retry Attribute #4692
Open
Description
Summary
I think it could be useful to have the current retry attempt count be accessibile from TestContext.
Background and Motivation
One of the test projects I have creates files as part of each test so that test runs can be easily analyzed without rerunning tests in debug mode (this is useful for finding errors in CI). The names of the files involve the test's display name as well as what the test is currently doing.
Proposed Feature
It'd be useful if the TestContext had an attribute that was able to keep track of the retry count.
[TestClass]
public class MyTestClass {
public TestContext TestContext {get; set; } = null!;
protected string GetFileName() {
var testName = (TestContext.TestName != TestContext.TestDisplayName) ? $"{TestContext.TestName}.{TestContext.TestDisplayName}" : $"{TestContext.TestDisplayName}";
// use new TestAttemptCount property so the name is unique and we don't have to manually track it for each test method:
return $"{testName}-{TestContext.TestAttemptCount}"
}
[TestMethod]
[Retry(3)]
public async TaskFlakeyTest() {
// send http request to external server/interact with an external service
// validate response
}
[TestCleanup]
public async Task Cleanup() {
if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed) {
string fileName = GetFileName();
// save response to a file (in my case failing Playwright traces are saved to a file)
}
}
}
Alternative Designs
Leave it as is, if people need to keep track of the Retry count then they can handle it themselves with a static variable.
As done here:
https://github.com/microsoft/testfx/blob/main/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/RetryTests.cs#L94