UnityTestAssemblyRunner.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using NUnit;
  6. using NUnit.Framework.Interfaces;
  7. using NUnit.Framework.Internal;
  8. using UnityEngine.TestTools;
  9. using UnityEngine.TestTools.NUnitExtensions;
  10. namespace UnityEngine.TestRunner.NUnitExtensions.Runner
  11. {
  12. internal interface IUnityTestAssemblyRunner
  13. {
  14. ITest LoadedTest { get; }
  15. ITestResult Result { get; }
  16. bool IsTestLoaded { get; }
  17. bool IsTestRunning { get; }
  18. bool IsTestComplete { get; }
  19. UnityWorkItem TopLevelWorkItem { get; set; }
  20. UnityTestExecutionContext GetCurrentContext();
  21. ITest Load(Assembly[] assemblies, TestPlatform testPlatform, IDictionary<string, object> settings);
  22. IEnumerable Run(ITestListener listener, ITestFilter filter);
  23. void StopRun();
  24. }
  25. internal class UnityTestAssemblyRunner : IUnityTestAssemblyRunner
  26. {
  27. private readonly UnityTestAssemblyBuilder unityBuilder;
  28. private readonly WorkItemFactory m_Factory;
  29. protected UnityTestExecutionContext Context { get; set; }
  30. public UnityTestExecutionContext GetCurrentContext()
  31. {
  32. return UnityTestExecutionContext.CurrentContext;
  33. }
  34. protected IDictionary<string, object> Settings { get; set; }
  35. public ITest LoadedTest { get; protected set; }
  36. public ITestResult Result
  37. {
  38. get { return TopLevelWorkItem == null ? null : TopLevelWorkItem.Result; }
  39. }
  40. public bool IsTestLoaded
  41. {
  42. get { return LoadedTest != null; }
  43. }
  44. public bool IsTestRunning
  45. {
  46. get { return TopLevelWorkItem != null && TopLevelWorkItem.State == NUnit.Framework.Internal.Execution.WorkItemState.Running; }
  47. }
  48. public bool IsTestComplete
  49. {
  50. get { return TopLevelWorkItem != null && TopLevelWorkItem.State == NUnit.Framework.Internal.Execution.WorkItemState.Complete; }
  51. }
  52. public UnityTestAssemblyRunner(UnityTestAssemblyBuilder builder, WorkItemFactory factory)
  53. {
  54. unityBuilder = builder;
  55. m_Factory = factory;
  56. Context = new UnityTestExecutionContext();
  57. }
  58. public ITest Load(Assembly[] assemblies, TestPlatform testPlatform, IDictionary<string, object> settings)
  59. {
  60. Settings = settings;
  61. if (settings.ContainsKey(FrameworkPackageSettings.RandomSeed))
  62. Randomizer.InitialSeed = (int)settings[FrameworkPackageSettings.RandomSeed];
  63. return LoadedTest = unityBuilder.Build(assemblies, Enumerable.Repeat(testPlatform, assemblies.Length).ToArray(), settings);
  64. }
  65. public IEnumerable Run(ITestListener listener, ITestFilter filter)
  66. {
  67. TopLevelWorkItem = m_Factory.Create(LoadedTest, filter);
  68. TopLevelWorkItem.InitializeContext(Context);
  69. UnityTestExecutionContext.CurrentContext = Context;
  70. Context.Listener = listener;
  71. return TopLevelWorkItem.Execute();
  72. }
  73. public UnityWorkItem TopLevelWorkItem { get; set; }
  74. public void StopRun()
  75. {
  76. if (IsTestRunning)
  77. {
  78. TopLevelWorkItem.Cancel(false);
  79. }
  80. }
  81. }
  82. }