OuterUnityTestActionCommand.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using NUnit.Framework.Internal;
  5. using NUnit.Framework.Internal.Commands;
  6. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  7. namespace UnityEngine.TestTools
  8. {
  9. internal class OuterUnityTestActionCommand : BeforeAfterTestCommandBase<IOuterUnityTestAction>
  10. {
  11. public OuterUnityTestActionCommand(TestCommand innerCommand)
  12. : base(innerCommand, "BeforeTest", "AfterTest")
  13. {
  14. if (Test.TypeInfo.Type != null)
  15. {
  16. BeforeActions = GetUnityTestActionsFromMethod(Test.Method.MethodInfo);
  17. AfterActions = BeforeActions;
  18. }
  19. }
  20. private static IOuterUnityTestAction[] GetUnityTestActionsFromMethod(MethodInfo method)
  21. {
  22. var attributes = method.GetCustomAttributes(false);
  23. List<IOuterUnityTestAction> actions = new List<IOuterUnityTestAction>();
  24. foreach (var attribute in attributes)
  25. {
  26. if (attribute is IOuterUnityTestAction)
  27. actions.Add(attribute as IOuterUnityTestAction);
  28. }
  29. return actions.ToArray();
  30. }
  31. protected override IEnumerator InvokeBefore(IOuterUnityTestAction action, Test test, UnityTestExecutionContext context)
  32. {
  33. return action.BeforeTest(test);
  34. }
  35. protected override IEnumerator InvokeAfter(IOuterUnityTestAction action, Test test, UnityTestExecutionContext context)
  36. {
  37. return action.AfterTest(test);
  38. }
  39. protected override BeforeAfterTestCommandState GetState(UnityTestExecutionContext context)
  40. {
  41. return context.OuterUnityTestActionState;
  42. }
  43. }
  44. }