SetUpTearDownCommand.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Reflection;
  5. using NUnit.Framework;
  6. using NUnit.Framework.Internal;
  7. using NUnit.Framework.Internal.Commands;
  8. using NUnit.Framework.Internal.Execution;
  9. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  10. namespace UnityEngine.TestTools
  11. {
  12. internal class SetUpTearDownCommand : BeforeAfterTestCommandBase<MethodInfo>
  13. {
  14. public SetUpTearDownCommand(TestCommand innerCommand)
  15. : base(innerCommand, "SetUp", "TearDown", true)
  16. {
  17. if (Test.TypeInfo.Type != null)
  18. {
  19. BeforeActions = GetMethodsWithAttributeFromFixture(Test.TypeInfo.Type, typeof(SetUpAttribute));
  20. AfterActions = GetMethodsWithAttributeFromFixture(Test.TypeInfo.Type, typeof(TearDownAttribute)).Reverse().ToArray();
  21. }
  22. }
  23. private static MethodInfo[] GetMethodsWithAttributeFromFixture(Type fixtureType, Type setUpType)
  24. {
  25. MethodInfo[] methodsWithAttribute = Reflect.GetMethodsWithAttribute(fixtureType, setUpType, true);
  26. return methodsWithAttribute.Where(x => x.ReturnType == typeof(void)).ToArray();
  27. }
  28. protected override IEnumerator InvokeBefore(MethodInfo action, Test test, UnityTestExecutionContext context)
  29. {
  30. Reflect.InvokeMethod(action, context.TestObject);
  31. yield return null;
  32. }
  33. protected override IEnumerator InvokeAfter(MethodInfo action, Test test, UnityTestExecutionContext context)
  34. {
  35. Reflect.InvokeMethod(action, context.TestObject);
  36. yield return null;
  37. }
  38. protected override BeforeAfterTestCommandState GetState(UnityTestExecutionContext context)
  39. {
  40. return null;
  41. }
  42. }
  43. }