Skip to content

Commit d306ac8

Browse files
authored
Add IterationDataAttribute (#4561)
1 parent 2e0000f commit d306ac8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using System.Reflection;
7+
using Xunit.Sdk;
8+
9+
namespace Microsoft.ML.TestFrameworkCommon.Attributes
10+
{
11+
/// <summary>
12+
/// xUnit data attribute that allows looping tests. The following example shows a test which will run 50 times.
13+
/// <code>
14+
/// [Theory, IterationData(50)]
15+
/// public void IteratingTest(int iteration)
16+
/// {
17+
/// }
18+
/// </code>
19+
/// </summary>
20+
public sealed class IterationDataAttribute : DataAttribute
21+
{
22+
public IterationDataAttribute(int iterations = 100)
23+
{
24+
Iterations = iterations;
25+
}
26+
27+
public int Iterations { get; }
28+
29+
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
30+
{
31+
for (var i = 0; i < Iterations; i++)
32+
{
33+
yield return new object[] { i };
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)