TestActionCommand.cs 1.7 KB

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