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.

SetUpTearDownCommand.cs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.ExceptionServices;
  7. using System.Threading.Tasks;
  8. using NUnit.Framework;
  9. using NUnit.Framework.Internal;
  10. using NUnit.Framework.Internal.Commands;
  11. using Unity.Profiling;
  12. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  13. namespace UnityEngine.TestTools
  14. {
  15. internal class SetUpTearDownCommand : BeforeAfterTestCommandBase<MethodInfo>
  16. {
  17. private static readonly Dictionary<Type, List<MethodInfo>> m_BeforeActionsCache = new Dictionary<Type, List<MethodInfo>>();
  18. private static readonly Dictionary<Type, List<MethodInfo>> m_AfterActionsCache = new Dictionary<Type, List<MethodInfo>>();
  19. public SetUpTearDownCommand(TestCommand innerCommand)
  20. : base(innerCommand, "SetUp", "TearDown")
  21. {
  22. using (new ProfilerMarker(nameof(SetUpTearDownCommand)).Auto())
  23. {
  24. if (Test.TypeInfo.Type != null)
  25. {
  26. BeforeActions = GetActions(m_BeforeActionsCache, Test.TypeInfo.Type, typeof(SetUpAttribute), new[] {typeof(void), typeof(Task)});
  27. AfterActions = GetActions(m_AfterActionsCache, Test.TypeInfo.Type, typeof(TearDownAttribute), new[] {typeof(void), typeof(Task)}).Reverse().ToArray();
  28. }
  29. }
  30. }
  31. protected override bool AllowFrameSkipAfterAction(MethodInfo action)
  32. {
  33. return action.ReturnType == typeof(Task);
  34. }
  35. protected override IEnumerator InvokeBefore(MethodInfo action, Test test, UnityTestExecutionContext context)
  36. {
  37. if (action.ReturnType == typeof(Task))
  38. {
  39. Task task;
  40. using (new ProfilerMarker(test.Name + ".Setup").Auto())
  41. task = HandleTaskCommand(action, context);
  42. while (!task.IsCompleted)
  43. {
  44. yield return null;
  45. }
  46. if (task.IsFaulted)
  47. {
  48. ExceptionDispatchInfo.Capture(task.Exception.InnerExceptions.Count == 1 ? task.Exception.InnerException : task.Exception).Throw();
  49. }
  50. }
  51. else
  52. {
  53. using (new ProfilerMarker(test.Name + ".Setup").Auto())
  54. Reflect.InvokeMethod(action, context.TestObject);
  55. yield return null;
  56. }
  57. }
  58. protected override IEnumerator InvokeAfter(MethodInfo action, Test test, UnityTestExecutionContext context)
  59. {
  60. if (action.ReturnType == typeof(Task))
  61. {
  62. Task task;
  63. using (new ProfilerMarker(test.Name + ".TearDown").Auto())
  64. task = HandleTaskCommand(action, context);
  65. while (!task.IsCompleted)
  66. {
  67. yield return null;
  68. }
  69. if (task.IsFaulted)
  70. {
  71. ExceptionDispatchInfo.Capture(task.Exception.InnerExceptions.Count == 1 ? task.Exception.InnerException : task.Exception).Throw();
  72. }
  73. }
  74. else
  75. {
  76. using (new ProfilerMarker(test.Name + ".TearDown").Auto())
  77. Reflect.InvokeMethod(action, context.TestObject);
  78. yield return null;
  79. }
  80. }
  81. private Task HandleTaskCommand(MethodInfo action, UnityTestExecutionContext context)
  82. {
  83. try
  84. {
  85. return Reflect.InvokeMethod(action, context.TestObject) as Task;
  86. }
  87. catch (TargetInvocationException e)
  88. {
  89. throw e;
  90. }
  91. }
  92. protected override BeforeAfterTestCommandState GetState(UnityTestExecutionContext context)
  93. {
  94. // Normal Setup/Teardown does not support domain reloads and will not need a persisted state.
  95. return new BeforeAfterTestCommandState();
  96. }
  97. }
  98. }