There was the same requirement, but the accepted answer code did not repeat the tests, so I adapted it to:
public sealed class RepeatAttribute : Xunit.Sdk.DataAttribute { private readonly int count; public RepeatAttribute(int count) { if (count < 1) { throw new System.ArgumentOutOfRangeException( paramName: nameof(count), message: "Repeat count must be greater than 0." ); } this.count = count; } public override System.Collections.Generic.IEnumerable<object[]> GetData(System.Reflection.MethodInfo testMethod) { foreach (var iterationNumber in Enumerable.Range(start: 1, count: this.count)) { yield return new object[] { iterationNumber }; } } }
Although Enumerable.Repeat was used in the previous example, the test was run only 1 time, since xUnit does not repeat the test. They probably changed something a while ago. Moving on to the foreach we can repeat each test, but we also provide an “iteration number”. When using it in a test function, you must add a parameter to your test function and style it as Theory as shown here:
[Theory(DisplayName = "It should work")] [Repeat(10)] public void It_should_work(int iterationNumber) { ... }
This works for xUnit 2.4.0.
I created the NuGet package to use in case anyone would be interested.
source share