SetUpTearDownCommand.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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.Internal;
  8. using NUnit.Framework.Internal.Commands;
  9. using Unity.Profiling;
  10. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  11. namespace UnityEngine.TestTools
  12. {
  13. internal class SetUpTearDownCommand : BeforeAfterTestCommandBase<MethodInfo>
  14. {
  15. static readonly Dictionary<Type, List<MethodInfo>> m_BeforeActionsCache = new Dictionary<Type, List<MethodInfo>>();
  16. static readonly Dictionary<Type, List<MethodInfo>> m_AfterActionsCache = new Dictionary<Type, List<MethodInfo>>();
  17. public SetUpTearDownCommand(TestCommand innerCommand)
  18. : base(innerCommand, "SetUp", "TearDown", true)
  19. {
  20. using (new ProfilerMarker(nameof(SetUpTearDownCommand)).Auto())
  21. {
  22. if (Test.TypeInfo.Type != null)
  23. {
  24. BeforeActions = GetActions(m_BeforeActionsCache, Test.TypeInfo.Type, typeof(SetUpAttribute), typeof(void));
  25. AfterActions = GetActions(m_AfterActionsCache, Test.TypeInfo.Type, typeof(TearDownAttribute), typeof(void)).Reverse().ToArray();
  26. }
  27. }
  28. }
  29. protected override IEnumerator InvokeBefore(MethodInfo action, Test test, UnityTestExecutionContext context)
  30. {
  31. using (new ProfilerMarker(test.Name + ".Setup").Auto())
  32. Reflect.InvokeMethod(action, context.TestObject);
  33. yield return null;
  34. }
  35. protected override IEnumerator InvokeAfter(MethodInfo action, Test test, UnityTestExecutionContext context)
  36. {
  37. using (new ProfilerMarker(test.Name + ".TearDown").Auto())
  38. Reflect.InvokeMethod(action, context.TestObject);
  39. yield return null;
  40. }
  41. protected override BeforeAfterTestCommandState GetState(UnityTestExecutionContext context)
  42. {
  43. return null;
  44. }
  45. }
  46. }