CoroutineTestWorkItem.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections;
  3. using NUnit.Framework.Interfaces;
  4. using NUnit.Framework.Internal;
  5. using NUnit.Framework.Internal.Commands;
  6. using NUnit.Framework.Internal.Execution;
  7. using UnityEngine.TestTools.Utils;
  8. namespace UnityEngine.TestRunner.NUnitExtensions.Runner
  9. {
  10. internal class CoroutineTestWorkItem : UnityWorkItem
  11. {
  12. private static MonoBehaviour m_MonoBehaviourCoroutineRunner;
  13. private TestCommand m_Command;
  14. public static MonoBehaviour monoBehaviourCoroutineRunner
  15. {
  16. get
  17. {
  18. if (m_MonoBehaviourCoroutineRunner == null)
  19. {
  20. throw new NullReferenceException("MonoBehaviour coroutine runner not set");
  21. }
  22. return m_MonoBehaviourCoroutineRunner;
  23. }
  24. set { m_MonoBehaviourCoroutineRunner = value; }
  25. }
  26. public CoroutineTestWorkItem(TestMethod test, ITestFilter filter)
  27. : base(test, null)
  28. {
  29. m_Command = m_Command = TestCommandBuilder.BuildTestCommand(test, filter);
  30. }
  31. protected override IEnumerable PerformWork()
  32. {
  33. if (m_Command is SkipCommand)
  34. {
  35. m_Command.Execute(Context);
  36. Result = Context.CurrentResult;
  37. WorkItemComplete();
  38. yield break;
  39. }
  40. if (m_Command is ApplyChangesToContextCommand)
  41. {
  42. var applyChangesToContextCommand = (ApplyChangesToContextCommand)m_Command;
  43. applyChangesToContextCommand.ApplyChanges(Context);
  44. m_Command = applyChangesToContextCommand.GetInnerCommand();
  45. }
  46. var enumerableTestMethodCommand = (IEnumerableTestMethodCommand)m_Command;
  47. try
  48. {
  49. var executeEnumerable = enumerableTestMethodCommand.ExecuteEnumerable(Context).GetEnumerator();
  50. var coroutineRunner = new CoroutineRunner(monoBehaviourCoroutineRunner, Context);
  51. yield return coroutineRunner.HandleEnumerableTest(executeEnumerable);
  52. if (coroutineRunner.HasFailedWithTimeout())
  53. {
  54. Context.CurrentResult.SetResult(ResultState.Failure, string.Format("Test exceeded Timeout value of {0}ms", Context.TestCaseTimeout));
  55. }
  56. while (executeEnumerable.MoveNext()) {}
  57. Result = Context.CurrentResult;
  58. }
  59. finally
  60. {
  61. WorkItemComplete();
  62. }
  63. }
  64. }
  65. }