CaseOrder.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #if !(NET20 || NET40)
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Xunit.Abstractions;
  7. using Xunit.Sdk;
  8. namespace Padi.RedmineApi.Tests.Infrastructure
  9. {
  10. /// <summary>
  11. /// Custom xUnit test case orderer that uses the OrderAttribute
  12. /// </summary>
  13. public class CaseOrderer : ITestCaseOrderer
  14. {
  15. public const string TYPE_NAME = "redmine.net.api.Tests.Infrastructure.CaseOrderer";
  16. public const string ASSEMBY_NAME = "redmine-net-api.Tests";
  17. public static readonly ConcurrentDictionary<string, ConcurrentQueue<string>> QueuedTests = new ConcurrentDictionary<string, ConcurrentQueue<string>>();
  18. public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
  19. where TTestCase : ITestCase
  20. {
  21. return testCases.OrderBy(GetOrder);
  22. }
  23. private static int GetOrder<TTestCase>(TTestCase testCase)
  24. where TTestCase : ITestCase
  25. {
  26. // Enqueue the test name.
  27. QueuedTests
  28. .GetOrAdd(testCase.TestMethod.TestClass.Class.Name,key => new ConcurrentQueue<string>())
  29. .Enqueue(testCase.TestMethod.Method.Name);
  30. // Order the test based on the attribute.
  31. var attr = testCase.TestMethod.Method
  32. .ToRuntimeMethod()
  33. .GetCustomAttribute<OrderAttribute>();
  34. return attr != null ? attr.Index : 0;
  35. }
  36. }
  37. }
  38. #endif