Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomize the order of tests #5831

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions tracer/test/Datadog.Trace.TestHelpers/CustomTestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ protected override async Task<RunSummary> RunTestCollectionsAsync(IMessageBus me
})
.ToList();

var seed = new Random().Next();

DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Using seed {seed} to randomize tests order"));

var random = new Random(seed);

Shuffle(collections, random);
kevingosse marked this conversation as resolved.
Show resolved Hide resolved

foreach (var collection in collections)
{
Shuffle(collection.TestCases, random);
}

var summary = new RunSummary();

using var runner = new ConcurrentRunner();
Expand Down Expand Up @@ -161,6 +174,20 @@ private static bool IsParallelizationDisabled(ITestCollection collection)

return attr?.GetNamedArgument<bool>(nameof(CollectionDefinitionAttribute.DisableParallelization)) is true;
}

private static void Shuffle<T>(IList<T> list, Random rng)
{
int n = list.Count;

while (n > 1)
{
n--;
int k = rng.Next(n + 1);
var value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}

private class CustomTestCollectionRunner : XunitTestCollectionRunner
Expand Down
Loading