TestExtensions.cs 5.3 KB

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