Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TestExtensions.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NUnit.Framework.Interfaces;
  5. using NUnit.Framework.Internal;
  6. using UnityEngine.TestRunner.NUnitExtensions.Filters;
  7. using UnityEngine.TestTools;
  8. namespace UnityEngine.TestRunner.NUnitExtensions
  9. {
  10. internal static class TestExtensions
  11. {
  12. private static List<string> ExtractFixtureCategories(ITest test)
  13. {
  14. var fixtureCategories = test.Properties[PropertyNames.Category].Cast<string>().ToList();
  15. if (test.Parent != null)
  16. {
  17. fixtureCategories.AddRange(ExtractFixtureCategories(test.Parent));
  18. }
  19. return fixtureCategories;
  20. }
  21. public static List<string> GetAllCategoriesFromTest(this ITest test)
  22. {
  23. // Only mark tests as Uncategorized if the test fixture doesn't have a category,
  24. // otherwise the test inherits the Fixture category.
  25. // Recursively try checking until Parent is null - cause category can be set on higher level.
  26. var categories = ExtractFixtureCategories(test);
  27. if (categories.Count == 0 && test is TestMethod)
  28. {
  29. categories.Add(CategoryFilterExtended.k_DefaultCategory);
  30. }
  31. return categories;
  32. }
  33. public static void ParseForNameDuplicates(this ITest test)
  34. {
  35. var duplicates = new Dictionary<string, int>();
  36. for (var i = 0; i < test.Tests.Count; i++)
  37. {
  38. var child = test.Tests[i];
  39. int count;
  40. if (duplicates.TryGetValue(child.FullName, out count))
  41. {
  42. count++;
  43. child.Properties.Add("childIndex", count);
  44. duplicates[child.FullName] = count;
  45. }
  46. else
  47. {
  48. duplicates.Add(child.FullName, 1);
  49. }
  50. ParseForNameDuplicates(child);
  51. }
  52. }
  53. public static void ApplyPlatformToPropertyBag(this ITest test, TestPlatform testPlatform)
  54. {
  55. test.Properties.Set("platform", testPlatform);
  56. foreach (var child in test.Tests)
  57. {
  58. child.ApplyPlatformToPropertyBag(testPlatform);
  59. }
  60. }
  61. public static int GetChildIndex(this ITest test)
  62. {
  63. var index = test.Properties["childIndex"];
  64. return (int)index[0];
  65. }
  66. public static bool HasChildIndex(this ITest test)
  67. {
  68. var index = test.Properties["childIndex"];
  69. return index.Count > 0;
  70. }
  71. private static string GetAncestorPath(ITest test)
  72. {
  73. var path = "";
  74. var testParent = test.Parent;
  75. while (testParent != null && testParent.Parent != null && !string.IsNullOrEmpty(testParent.Name))
  76. {
  77. path = testParent.Name + "/" + path;
  78. testParent = testParent.Parent;
  79. }
  80. return path;
  81. }
  82. public static string GetUniqueName(this ITest test)
  83. {
  84. var id = GetAncestorPath(test) + GetFullName(test);
  85. if (test.HasChildIndex())
  86. {
  87. var index = test.GetChildIndex();
  88. if (index >= 0)
  89. id += index;
  90. }
  91. if (test.IsSuite)
  92. {
  93. id += "[suite]";
  94. }
  95. return id;
  96. }
  97. public static int GetRetryIteration(this ITest test)
  98. {
  99. if (test.Properties.ContainsKey("retryIteration"))
  100. {
  101. return test.Properties["retryIteration"].OfType<int>().First();
  102. }
  103. return 0;
  104. }
  105. public static int GetRepeatIteration(this ITest test)
  106. {
  107. if (test.Properties.ContainsKey("repeatIteration"))
  108. {
  109. return test.Properties["repeatIteration"].OfType<int>().First();
  110. }
  111. return 0;
  112. }
  113. public static string GetFullName(this ITest test)
  114. {
  115. var typeInfo = test.TypeInfo ?? test.Parent?.TypeInfo ?? test.Tests.FirstOrDefault()?.TypeInfo;
  116. if (typeInfo == null)
  117. {
  118. return "[" + test.Name + "]";
  119. }
  120. var assemblyId = typeInfo.Assembly.GetName().Name;
  121. if (assemblyId == test.Name)
  122. {
  123. return $"[{test.Name}]";
  124. }
  125. return string.Format("[{0}][{1}]", assemblyId, test.FullName);
  126. }
  127. public static string GetFullNameWithoutDllPath(this ITest test)
  128. {
  129. if (test.Parent == null)
  130. {
  131. return string.Empty;
  132. }
  133. var typeInfo = test.TypeInfo ?? test.Parent?.TypeInfo;
  134. if (typeInfo == null && IsAssembly(test))
  135. {
  136. return test.Name;
  137. }
  138. return test.FullName;
  139. }
  140. private static bool IsAssembly(this ITest test)
  141. {
  142. return test.Parent.Parent == null;
  143. }
  144. public static string GetSkipReason(this ITest test)
  145. {
  146. if (test.Properties.ContainsKey(PropertyNames.SkipReason))
  147. return (string)test.Properties.Get(PropertyNames.SkipReason);
  148. return null;
  149. }
  150. public static string GetParentId(this ITest test)
  151. {
  152. if (test.Parent != null)
  153. return test.Parent.Id;
  154. return null;
  155. }
  156. public static string GetParentFullName(this ITest test)
  157. {
  158. if (test.Parent != null)
  159. return test.Parent.FullName;
  160. return null;
  161. }
  162. public static string GetParentUniqueName(this ITest test)
  163. {
  164. if (test.Parent != null)
  165. return GetUniqueName(test.Parent);
  166. return null;
  167. }
  168. internal static string GetFullName(string testFullName, int childIndex)
  169. {
  170. return childIndex != -1 ? GetIndexedTestCaseName(testFullName, childIndex) : testFullName;
  171. }
  172. private static string GetIndexedTestCaseName(string fullName, int index)
  173. {
  174. var generatedTestSuffix = " GeneratedTestCase" + index;
  175. if (fullName.EndsWith(")"))
  176. {
  177. // Test names from generated TestCaseSource look like Test(TestCaseSourceType)
  178. // This inserts a unique test case index in the name, so that it becomes Test(TestCaseSourceType GeneratedTestCase0)
  179. return fullName.Substring(0, fullName.Length - 1) + generatedTestSuffix + fullName[fullName.Length - 1];
  180. }
  181. // In some cases there can be tests with duplicate names generated in other ways and they won't have () in their name
  182. // We just append a suffix at the end of the name in that case
  183. return fullName + generatedTestSuffix;
  184. }
  185. }
  186. }