-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds repeat test attribute for flaky tests
Also attempt to fix test publishing #196
- Loading branch information
Showing
4 changed files
with
80 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
tests/Acoustics.Test/TestHelpers/RetryTestMethodAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// <copyright file="RetryTestMethodAttribute.cs" company="QutEcoacoustics"> | ||
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group). | ||
// </copyright> | ||
|
||
namespace Acoustics.Test.TestHelpers | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
public class RetryTestMethodAttribute : TestMethodAttribute | ||
{ | ||
public RetryTestMethodAttribute(int retryCount) | ||
{ | ||
if (retryCount < 0) | ||
{ | ||
throw new ArgumentException($"{nameof(retryCount)} cannot be less than 0", nameof(retryCount)); | ||
} | ||
|
||
this.Retrycount = retryCount; | ||
} | ||
|
||
public int Retrycount { get; } | ||
|
||
public override TestResult[] Execute(ITestMethod testMethod) | ||
{ | ||
// always run at least once, and then rety n times if fails | ||
int retryCount = this.Retrycount + 1; | ||
|
||
var result = new List<TestResult>(); | ||
|
||
var success = false; | ||
for (int count = 0; count < retryCount; count++) | ||
{ | ||
var testResults = base.Execute(testMethod); | ||
result.AddRange(testResults); | ||
|
||
var failed = testResults.FirstOrDefault((tr) => tr.Outcome == UnitTestOutcome.Failed); | ||
if (failed != null) | ||
{ | ||
failed.TestContextMessages += $"Test iteration {count + 1} of {retryCount} failed. Attempting to run again."; | ||
failed.Outcome = UnitTestOutcome.Inconclusive; | ||
continue; | ||
} | ||
|
||
success = true; | ||
break; | ||
} | ||
|
||
if (!success) | ||
{ | ||
result[^1].Outcome = UnitTestOutcome.Failed; | ||
} | ||
|
||
return result.ToArray(); | ||
} | ||
} | ||
} |