zerlei
1809dbebf5
1. 写逻辑复杂的程序,单元测试必不可少,重构和修改更有信心 2. 单元测试每个测试函数应该是独立的,不相互依赖的,也应该是简单的,否则出现问题不好排查,它叫单元测试 3. 运行测试和debug的时候不要害怕😰😰😰😰😰🙃🙃🙃🙃🙃❤️❤️❤️❤️❤️,它帮你找到了错误,完全没有否定你的意思·····
42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using Xunit.Abstractions;
|
|
using Xunit.Sdk;
|
|
using XUnit.Project.Attributes;
|
|
|
|
namespace XUnit.Project.Orderers;
|
|
|
|
public class PriorityOrderer : ITestCaseOrderer
|
|
{
|
|
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(
|
|
IEnumerable<TTestCase> testCases) where TTestCase : ITestCase
|
|
{
|
|
string assemblyName = typeof(TestPriorityAttribute).AssemblyQualifiedName!;
|
|
var sortedMethods = new SortedDictionary<int, List<TTestCase>>();
|
|
foreach (TTestCase testCase in testCases)
|
|
{
|
|
int priority = testCase.TestMethod.Method
|
|
.GetCustomAttributes(assemblyName)
|
|
.FirstOrDefault()
|
|
?.GetNamedArgument<int>(nameof(TestPriorityAttribute.Priority)) ?? 0;
|
|
|
|
GetOrCreate(sortedMethods, priority).Add(testCase);
|
|
}
|
|
|
|
foreach (TTestCase testCase in
|
|
sortedMethods.Keys.SelectMany(
|
|
priority => sortedMethods[priority].OrderBy(
|
|
testCase => testCase.TestMethod.Method.Name)))
|
|
{
|
|
Console.WriteLine(testCase);
|
|
yield return testCase;
|
|
}
|
|
}
|
|
|
|
private static TValue GetOrCreate<TKey, TValue>(
|
|
IDictionary<TKey, TValue> dictionary, TKey key)
|
|
where TKey : struct
|
|
where TValue : new() =>
|
|
dictionary.TryGetValue(key, out TValue? result)
|
|
? result
|
|
: (dictionary[key] = new TValue());
|
|
}
|