TestExtensions.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using NUnit.Framework.Interfaces;
  4. using NUnit.Framework.Internal;
  5. using UnityEngine.TestRunner.NUnitExtensions.Filters;
  6. namespace UnityEngine.TestRunner.NUnitExtensions
  7. {
  8. internal static class TestExtensions
  9. {
  10. private static IEnumerable<string> GetTestCategories(this ITest test)
  11. {
  12. var categories = test.Properties[PropertyNames.Category].Cast<string>().ToList();
  13. if (categories.Count == 0 && test is TestMethod)
  14. {
  15. // only mark tests as Uncategorized if the test fixture doesn't have a category,
  16. // otherwise the test inherits the Fixture category
  17. var fixtureCategories = test.Parent.Properties[PropertyNames.Category].Cast<string>().ToList();
  18. if (fixtureCategories.Count == 0)
  19. categories.Add(CategoryFilterExtended.k_DefaultCategory);
  20. }
  21. return categories;
  22. }
  23. public static bool HasCategory(this ITest test, string[] categoryFilter)
  24. {
  25. var categories = test.GetAllCategoriesFromTest().Distinct();
  26. return categoryFilter.Any(c => categories.Any(r => r == c));
  27. }
  28. public static List<string> GetAllCategoriesFromTest(this ITest test)
  29. {
  30. if (test.Parent == null)
  31. return test.GetTestCategories().ToList();
  32. var categories = GetAllCategoriesFromTest(test.Parent);
  33. categories.AddRange(test.GetTestCategories());
  34. return categories;
  35. }
  36. public static void ParseForNameDuplicates(this ITest test)
  37. {
  38. var duplicates = new Dictionary<string, int>();
  39. for (var i = 0; i < test.Tests.Count; i++)
  40. {
  41. var child = test.Tests[i];
  42. int count;
  43. if (duplicates.TryGetValue(child.FullName, out count))
  44. {
  45. count++;
  46. child.Properties.Add("childIndex", count);
  47. duplicates[child.FullName] = count;
  48. }
  49. else
  50. {
  51. duplicates.Add(child.FullName, 1);
  52. }
  53. ParseForNameDuplicates(child);
  54. }
  55. }
  56. public static int GetChildIndex(this ITest test)
  57. {
  58. var index = test.Properties["childIndex"];
  59. return (int)index[0];
  60. }
  61. public static bool HasChildIndex(this ITest test)
  62. {
  63. var index = test.Properties["childIndex"];
  64. return index.Count > 0;
  65. }
  66. static string GetAncestorPath(ITest test)
  67. {
  68. var path = "";
  69. var testParent = test.Parent;
  70. while (testParent != null && testParent.Parent != null && !string.IsNullOrEmpty(testParent.Name))
  71. {
  72. path = testParent.Name + "/" + path;
  73. testParent = testParent.Parent;
  74. }
  75. return path;
  76. }
  77. public static string GetUniqueName(this ITest test)
  78. {
  79. var id = GetAncestorPath(test) + GetFullName(test);
  80. if (test.HasChildIndex())
  81. {
  82. var index = test.GetChildIndex();
  83. if (index >= 0)
  84. id += index;
  85. }
  86. if (test.IsSuite)
  87. {
  88. id += "[suite]";
  89. }
  90. return id;
  91. }
  92. public static string GetFullName(ITest test)
  93. {
  94. var typeInfo = test.TypeInfo ?? test.Parent?.TypeInfo ?? test.Tests.FirstOrDefault()?.TypeInfo;
  95. if (typeInfo == null)
  96. {
  97. return "[" + test.Name + "]";
  98. }
  99. var assemblyId = typeInfo.Assembly.GetName().Name;
  100. if (assemblyId == test.Name)
  101. {
  102. return $"[{test.Name}]";
  103. }
  104. return string.Format("[{0}][{1}]", assemblyId, test.FullName);
  105. }
  106. public static string GetSkipReason(this ITest test)
  107. {
  108. if (test.Properties.ContainsKey(PropertyNames.SkipReason))
  109. return (string)test.Properties.Get(PropertyNames.SkipReason);
  110. return null;
  111. }
  112. public static string GetParentId(this ITest test)
  113. {
  114. if (test.Parent != null)
  115. return test.Parent.Id;
  116. return null;
  117. }
  118. public static string GetParentFullName(this ITest test)
  119. {
  120. if (test.Parent != null)
  121. return test.Parent.FullName;
  122. return null;
  123. }
  124. public static string GetParentUniqueName(this ITest test)
  125. {
  126. if (test.Parent != null)
  127. return GetUniqueName(test.Parent);
  128. return null;
  129. }
  130. }
  131. }