-
Notifications
You must be signed in to change notification settings - Fork 780
/
DefaultTestCaseOrderer.cs
72 lines (62 loc) · 2.21 KB
/
DefaultTestCaseOrderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit.Internal;
using Xunit.Sdk;
namespace Xunit.v3
{
/// <summary>
/// Default implementation of <see cref="ITestCaseOrderer"/>. Orders tests in
/// an unpredictable but stable order, so that repeated test runs of the
/// identical test assembly run tests in the same order.
/// </summary>
public class DefaultTestCaseOrderer : ITestCaseOrderer
{
readonly _IMessageSink diagnosticMessageSink;
/// <summary>
/// Initializes a new instance of the <see cref="DefaultTestCaseOrderer"/> class.
/// </summary>
/// <param name="diagnosticMessageSink">The message sink which receives <see cref="_DiagnosticMessage"/> messages.</param>
public DefaultTestCaseOrderer(_IMessageSink diagnosticMessageSink)
{
this.diagnosticMessageSink = Guard.ArgumentNotNull(nameof(diagnosticMessageSink), diagnosticMessageSink);
}
/// <inheritdoc/>
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
where TTestCase : _ITestCase
{
var result = testCases.ToList();
try
{
result.Sort(Compare);
}
catch (Exception ex)
{
diagnosticMessageSink.OnMessage(new _DiagnosticMessage { Message = $"Exception thrown in DefaultTestCaseOrderer.OrderTestCases(); falling back to random order.{Environment.NewLine}{ex}" });
result = Randomize(result);
}
return result;
}
List<TTestCase> Randomize<TTestCase>(List<TTestCase> testCases)
{
var result = new List<TTestCase>(testCases.Count);
var randomizer = new Random();
while (testCases.Count > 0)
{
var next = randomizer.Next(testCases.Count);
result.Add(testCases[next]);
testCases.RemoveAt(next);
}
return result;
}
int Compare<TTestCase>(TTestCase x, TTestCase y)
where TTestCase : _ITestCase
{
Guard.ArgumentNotNull(nameof(x), x);
Guard.ArgumentNotNull(nameof(y), y);
Guard.ArgumentValid(nameof(x), $"Could not compare test case {x.DisplayName} because it has a null UniqueID", x.UniqueID != null);
Guard.ArgumentValid(nameof(y), $"Could not compare test case {y.DisplayName} because it has a null UniqueID", y.UniqueID != null);
return string.CompareOrdinal(x.UniqueID, y.UniqueID);
}
}
}