Retry failed tests in MStest

27 Views Asked by At

Help me figure out how to restart tests for mstest (I just switched to it). I know that there is no built-in restart functionality, but I know that you can create custom attributes. I tried creating a "retry" attribute, but it doesn't rerun the tests if they fail.

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    public class RetryAttribute : TestMethodAttribute
    {
        private readonly int _tryCount;

        public RetryAttribute(int tryCount)
        {
            _tryCount = tryCount;
        }

        public override TestResult[] Execute(ITestMethod testMethod)
        {
            var results = new List<TestResult>();

            for (int i = 0; i <= _tryCount; i++)
            {
                TestResult result = base.Execute(testMethod)[0];
                results.Add(result);

                if (result.Outcome == UnitTestOutcome.Passed)
                    break;

                if (i < _tryCount)
                {
                    Logger.LogMessage($"Retry {i + 1}/{_tryCount} for test {testMethod.TestMethodName}");
                }
            }

            return results.ToArray();
        }
    }
0

There are 0 best solutions below