PlaymodeTestsController.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.TestRunner.NUnitExtensions;
  7. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  8. using UnityEngine.TestTools.NUnitExtensions;
  9. using UnityEngine.TestTools.Utils;
  10. namespace UnityEngine.TestTools.TestRunner
  11. {
  12. [Serializable]
  13. [AddComponentMenu("")]
  14. internal class PlaymodeTestsController : MonoBehaviour
  15. {
  16. private IEnumerator m_TestSteps;
  17. [SerializeField]
  18. private List<string> m_AssembliesWithTests;
  19. public List<string> AssembliesWithTests
  20. {
  21. get
  22. {
  23. return m_AssembliesWithTests;
  24. }
  25. set
  26. {
  27. m_AssembliesWithTests = value;
  28. }
  29. }
  30. [SerializeField]
  31. internal TestStartedEvent testStartedEvent = new TestStartedEvent();
  32. [SerializeField]
  33. internal TestFinishedEvent testFinishedEvent = new TestFinishedEvent();
  34. [SerializeField]
  35. internal RunStartedEvent runStartedEvent = new RunStartedEvent();
  36. [SerializeField]
  37. internal RunFinishedEvent runFinishedEvent = new RunFinishedEvent();
  38. internal const string kPlaymodeTestControllerName = "Code-based tests runner";
  39. [SerializeField]
  40. public PlaymodeTestsControllerSettings settings = new PlaymodeTestsControllerSettings();
  41. internal UnityTestAssemblyRunner m_Runner;
  42. public IEnumerator Start()
  43. {
  44. //Skip 2 frame because Unity.
  45. yield return null;
  46. yield return null;
  47. StartCoroutine(Run());
  48. }
  49. internal static bool IsControllerOnScene()
  50. {
  51. return GameObject.Find(kPlaymodeTestControllerName) != null;
  52. }
  53. internal static PlaymodeTestsController GetController()
  54. {
  55. return GameObject.Find(kPlaymodeTestControllerName).GetComponent<PlaymodeTestsController>();
  56. }
  57. public IEnumerator TestRunnerCoroutine()
  58. {
  59. while (m_TestSteps.MoveNext())
  60. {
  61. yield return m_TestSteps.Current;
  62. }
  63. if (m_Runner.IsTestComplete)
  64. {
  65. runFinishedEvent.Invoke(m_Runner.Result);
  66. Cleanup();
  67. yield return null;
  68. }
  69. }
  70. public IEnumerator Run()
  71. {
  72. CoroutineTestWorkItem.monoBehaviourCoroutineRunner = this;
  73. gameObject.hideFlags |= HideFlags.DontSave;
  74. if (settings.sceneBased)
  75. {
  76. SceneManager.LoadScene(1, LoadSceneMode.Additive);
  77. yield return null;
  78. }
  79. var testListUtil = new PlayerTestAssemblyProvider(new AssemblyLoadProxy(), m_AssembliesWithTests);
  80. m_Runner = new UnityTestAssemblyRunner(new UnityTestAssemblyBuilder(), new PlaymodeWorkItemFactory());
  81. var loadedTests = m_Runner.Load(testListUtil.GetUserAssemblies().Select(a => a.Assembly).ToArray(), TestPlatform.PlayMode, UnityTestAssemblyBuilder.GetNUnitTestBuilderSettings(TestPlatform.PlayMode));
  82. loadedTests.ParseForNameDuplicates();
  83. runStartedEvent.Invoke(m_Runner.LoadedTest);
  84. var testListenerWrapper = new TestListenerWrapper(testStartedEvent, testFinishedEvent);
  85. m_TestSteps = m_Runner.Run(testListenerWrapper, settings.BuildNUnitFilter()).GetEnumerator();
  86. yield return TestRunnerCoroutine();
  87. }
  88. public void Cleanup()
  89. {
  90. if (m_Runner != null)
  91. {
  92. m_Runner.StopRun();
  93. m_Runner = null;
  94. }
  95. if (Application.isEditor)
  96. {
  97. Destroy(gameObject);
  98. }
  99. }
  100. public static void TryCleanup()
  101. {
  102. var controller = GetController();
  103. if (controller != null)
  104. {
  105. controller.Cleanup();
  106. }
  107. }
  108. }
  109. }