No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BeforeAfterTestCommandBase.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using NUnit.Framework;
  7. using NUnit.Framework.Interfaces;
  8. using NUnit.Framework.Internal;
  9. using NUnit.Framework.Internal.Commands;
  10. using UnityEngine.TestRunner.NUnitExtensions;
  11. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  12. using UnityEngine.TestTools.Logging;
  13. using UnityEngine.TestTools.TestRunner;
  14. namespace UnityEngine.TestTools
  15. {
  16. internal abstract class BeforeAfterTestCommandBase<T> : DelegatingTestCommand, IEnumerableTestMethodCommand where T : class
  17. {
  18. private string m_BeforeErrorPrefix;
  19. private string m_AfterErrorPrefix;
  20. private bool m_SkipYieldAfterActions;
  21. protected BeforeAfterTestCommandBase(TestCommand innerCommand, string beforeErrorPrefix, string afterErrorPrefix, bool skipYieldAfterActions = false)
  22. : base(innerCommand)
  23. {
  24. m_BeforeErrorPrefix = beforeErrorPrefix;
  25. m_AfterErrorPrefix = afterErrorPrefix;
  26. m_SkipYieldAfterActions = skipYieldAfterActions;
  27. }
  28. internal Func<long> GetUtcNow = () => new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
  29. protected T[] BeforeActions = new T[0];
  30. protected T[] AfterActions = new T[0];
  31. protected static MethodInfo[] GetActions(IDictionary<Type, List<MethodInfo>> cacheStorage, Type fixtureType, Type attributeType, Type returnType)
  32. {
  33. if (cacheStorage.TryGetValue(fixtureType, out var result))
  34. {
  35. return result.ToArray();
  36. }
  37. cacheStorage[fixtureType] = GetMethodsWithAttributeFromFixture(fixtureType, attributeType, returnType);
  38. return cacheStorage[fixtureType].ToArray();
  39. }
  40. protected static T[] GetTestActions(IDictionary<MethodInfo, List<T>> cacheStorage, MethodInfo methodInfo)
  41. {
  42. if (cacheStorage.TryGetValue(methodInfo, out var result))
  43. {
  44. return result.ToArray();
  45. }
  46. var attributesForMethodInfo = new List<T>();
  47. var attributes = methodInfo.GetCustomAttributes(false);
  48. foreach (var attribute in attributes)
  49. {
  50. if (attribute is T attribute1)
  51. {
  52. attributesForMethodInfo.Add(attribute1);
  53. }
  54. }
  55. cacheStorage[methodInfo] = attributesForMethodInfo;
  56. return cacheStorage[methodInfo].ToArray();
  57. }
  58. private static List<MethodInfo> GetMethodsWithAttributeFromFixture(Type fixtureType, Type setUpType, Type returnType)
  59. {
  60. MethodInfo[] methodsWithAttribute = Reflect.GetMethodsWithAttribute(fixtureType, setUpType, true);
  61. return methodsWithAttribute.Where(x => x.ReturnType == returnType).ToList();
  62. }
  63. protected abstract IEnumerator InvokeBefore(T action, Test test, UnityTestExecutionContext context);
  64. protected abstract IEnumerator InvokeAfter(T action, Test test, UnityTestExecutionContext context);
  65. protected virtual bool MoveBeforeEnumerator(IEnumerator enumerator, Test test)
  66. {
  67. return enumerator.MoveNext();
  68. }
  69. protected virtual bool MoveAfterEnumerator(IEnumerator enumerator, Test test)
  70. {
  71. return enumerator.MoveNext();
  72. }
  73. protected abstract BeforeAfterTestCommandState GetState(UnityTestExecutionContext context);
  74. public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
  75. {
  76. var unityContext = (UnityTestExecutionContext)context;
  77. var state = GetState(unityContext);
  78. // When entering PlayMode state will incorrectly be seen as null. Looking at the hashcode to be certain that it is null.
  79. if (state?.GetHashCode() == null)
  80. {
  81. // We do not expect a state to exist in playmode
  82. state = ScriptableObject.CreateInstance<BeforeAfterTestCommandState>();
  83. }
  84. state.ApplyTestResult(context.CurrentResult);
  85. while (state.NextBeforeStepIndex < BeforeActions.Length)
  86. {
  87. state.Timestamp = GetUtcNow();
  88. var action = BeforeActions[state.NextBeforeStepIndex];
  89. IEnumerator enumerator;
  90. try
  91. {
  92. enumerator = InvokeBefore(action, Test, unityContext);
  93. }
  94. catch (Exception ex)
  95. {
  96. state.TestHasRun = true;
  97. context.CurrentResult.RecordPrefixedException(m_BeforeErrorPrefix, ex);
  98. break;
  99. }
  100. ActivePcHelper.SetEnumeratorPC(enumerator, state.NextBeforeStepPc);
  101. using (var logScope = new LogScope())
  102. {
  103. while (true)
  104. {
  105. try
  106. {
  107. if (!enumerator.MoveNext())
  108. {
  109. break;
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. state.TestHasRun = true;
  115. context.CurrentResult.RecordPrefixedException(m_BeforeErrorPrefix, ex);
  116. state.StoreTestResult(context.CurrentResult);
  117. break;
  118. }
  119. state.NextBeforeStepPc = ActivePcHelper.GetEnumeratorPC(enumerator);
  120. state.StoreTestResult(context.CurrentResult);
  121. if (m_SkipYieldAfterActions)
  122. {
  123. break;
  124. }
  125. else
  126. {
  127. yield return enumerator.Current;
  128. }
  129. if (GetUtcNow() - state.Timestamp > unityContext.TestCaseTimeout || CoroutineTimedOut(unityContext))
  130. {
  131. context.CurrentResult.RecordPrefixedError(m_BeforeErrorPrefix, new UnityTestTimeoutException(unityContext.TestCaseTimeout).Message);
  132. state.TestHasRun = true;
  133. break;
  134. }
  135. }
  136. if (logScope.AnyFailingLogs())
  137. {
  138. state.TestHasRun = true;
  139. context.CurrentResult.RecordPrefixedError(m_BeforeErrorPrefix, new UnhandledLogMessageException(logScope.FailingLogs.First()).Message);
  140. state.StoreTestResult(context.CurrentResult);
  141. }
  142. }
  143. state.NextBeforeStepIndex++;
  144. state.NextBeforeStepPc = 0;
  145. }
  146. if (!state.TestHasRun)
  147. {
  148. if (innerCommand is IEnumerableTestMethodCommand)
  149. {
  150. var executeEnumerable = ((IEnumerableTestMethodCommand)innerCommand).ExecuteEnumerable(context);
  151. foreach (var iterator in executeEnumerable)
  152. {
  153. state.StoreTestResult(context.CurrentResult);
  154. yield return iterator;
  155. }
  156. }
  157. else
  158. {
  159. context.CurrentResult = innerCommand.Execute(context);
  160. state.StoreTestResult(context.CurrentResult);
  161. }
  162. state.TestHasRun = true;
  163. }
  164. while (state.NextAfterStepIndex < AfterActions.Length)
  165. {
  166. state.Timestamp = GetUtcNow();
  167. state.TestAfterStarted = true;
  168. var action = AfterActions[state.NextAfterStepIndex];
  169. IEnumerator enumerator;
  170. try
  171. {
  172. enumerator = InvokeAfter(action, Test, unityContext);
  173. }
  174. catch (Exception ex)
  175. {
  176. context.CurrentResult.RecordPrefixedException(m_AfterErrorPrefix, ex);
  177. state.StoreTestResult(context.CurrentResult);
  178. break;
  179. }
  180. ActivePcHelper.SetEnumeratorPC(enumerator, state.NextAfterStepPc);
  181. using (var logScope = new LogScope())
  182. {
  183. while (true)
  184. {
  185. try
  186. {
  187. if (!enumerator.MoveNext())
  188. {
  189. break;
  190. }
  191. }
  192. catch (Exception ex)
  193. {
  194. context.CurrentResult.RecordPrefixedException(m_AfterErrorPrefix, ex);
  195. state.StoreTestResult(context.CurrentResult);
  196. break;
  197. }
  198. state.NextAfterStepPc = ActivePcHelper.GetEnumeratorPC(enumerator);
  199. state.StoreTestResult(context.CurrentResult);
  200. if (GetUtcNow() - state.Timestamp > unityContext.TestCaseTimeout || CoroutineTimedOut(unityContext))
  201. {
  202. context.CurrentResult.RecordPrefixedError(m_AfterErrorPrefix, new UnityTestTimeoutException(unityContext.TestCaseTimeout).Message);
  203. yield break;
  204. }
  205. if (m_SkipYieldAfterActions)
  206. {
  207. break;
  208. }
  209. else
  210. {
  211. yield return enumerator.Current;
  212. }
  213. }
  214. if (logScope.AnyFailingLogs())
  215. {
  216. state.TestHasRun = true;
  217. context.CurrentResult.RecordPrefixedError(m_AfterErrorPrefix, new UnhandledLogMessageException(logScope.FailingLogs.First()).Message);
  218. state.StoreTestResult(context.CurrentResult);
  219. }
  220. }
  221. state.NextAfterStepIndex++;
  222. state.NextAfterStepPc = 0;
  223. }
  224. state.Reset();
  225. }
  226. public override TestResult Execute(ITestExecutionContext context)
  227. {
  228. throw new NotImplementedException("Use ExecuteEnumerable");
  229. }
  230. private static TestCommandPcHelper pcHelper;
  231. private static bool CoroutineTimedOut(ITestExecutionContext unityContext)
  232. {
  233. if (string.IsNullOrEmpty(unityContext.CurrentResult.Message))
  234. {
  235. return false;
  236. }
  237. return unityContext.CurrentResult.ResultState.Equals(ResultState.Failure) &&
  238. unityContext.CurrentResult.Message.Contains(new UnityTestTimeoutException(unityContext.TestCaseTimeout).Message);
  239. }
  240. internal static TestCommandPcHelper ActivePcHelper
  241. {
  242. get
  243. {
  244. if (pcHelper == null)
  245. {
  246. pcHelper = new TestCommandPcHelper();
  247. }
  248. return pcHelper;
  249. }
  250. set
  251. {
  252. pcHelper = value;
  253. }
  254. }
  255. }
  256. }