暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RiderTestRunner.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using JetBrains.Annotations;
  2. using UnityEngine;
  3. #if TEST_FRAMEWORK
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using UnityEditor;
  8. using UnityEditor.TestTools.TestRunner.Api;
  9. #else
  10. using System;
  11. #endif
  12. namespace Packages.Rider.Editor.UnitTesting
  13. {
  14. /// <summary>
  15. /// Is called by Rider Unity plugin via reflections
  16. /// </summary>
  17. [UsedImplicitly]
  18. public static class RiderTestRunner
  19. {
  20. #if TEST_FRAMEWORK
  21. private static readonly TestsCallback Callback = ScriptableObject.CreateInstance<TestsCallback>();
  22. private static string _sessionGuid;
  23. #endif
  24. /// <summary>
  25. /// Is called by Rider Unity plugin via reflections
  26. /// </summary>
  27. /// <param name="sessionId"></param>
  28. /// <param name="testMode"></param>
  29. /// <param name="assemblyNames"></param>
  30. /// <param name="testNames"></param>
  31. /// <param name="categoryNames"></param>
  32. /// <param name="groupNames"></param>
  33. /// <param name="buildTarget"></param>
  34. /// <param name="callbacksHandlerCodeBase"></param>
  35. /// <param name="callbacksHandlerTypeName"></param>
  36. /// <param name="callbacksHandlerDependencies"></param>
  37. [UsedImplicitly]
  38. public static void RunTestsWithSyncCallbacks(string sessionId, int testMode, string[] assemblyNames,
  39. string[] testNames, string[] categoryNames, string[] groupNames, int? buildTarget,
  40. string callbacksHandlerCodeBase, string callbacksHandlerTypeName, string[] callbacksHandlerDependencies)
  41. {
  42. #if !TEST_FRAMEWORK
  43. Debug.LogError("Update Test Framework package to v.1.1.8+ to run tests from Rider.");
  44. throw new NotSupportedException("Incompatible `Test Framework` package in Unity. Update to v.1.1.8+");
  45. #else
  46. SyncTestRunEventsHandler.instance.InitRun(sessionId, callbacksHandlerCodeBase, callbacksHandlerTypeName, callbacksHandlerDependencies);
  47. RunTests(testMode, assemblyNames, testNames, categoryNames, groupNames, buildTarget);
  48. #endif
  49. }
  50. /// <summary>
  51. /// Is called by Rider Unity plugin via reflections
  52. /// </summary>
  53. /// <param name="testMode"></param>
  54. /// <param name="assemblyNames"></param>
  55. /// <param name="testNames"></param>
  56. /// <param name="categoryNames"></param>
  57. /// <param name="groupNames"></param>
  58. /// <param name="buildTarget"></param>
  59. [UsedImplicitly]
  60. public static void RunTests(int testMode, string[] assemblyNames, string[] testNames, string[] categoryNames, string[] groupNames, int? buildTarget)
  61. {
  62. #if !TEST_FRAMEWORK
  63. Debug.LogError("Update Test Framework package to v.1.1.8+ to run tests from Rider.");
  64. throw new NotSupportedException("Incompatible `Test Framework` package in Unity. Update to v.1.1.8+");
  65. #else
  66. CallbackData.instance.isRider = true;
  67. var api = ScriptableObject.CreateInstance<TestRunnerApi>();
  68. var settings = new ExecutionSettings();
  69. var filter = new Filter
  70. {
  71. assemblyNames = assemblyNames,
  72. testNames = testNames,
  73. categoryNames = categoryNames,
  74. groupNames = groupNames,
  75. targetPlatform = (BuildTarget?) buildTarget
  76. };
  77. if (testMode > 0) // for future use - test-framework would allow running both Edit and Play test at once
  78. {
  79. filter.testMode = (TestMode) testMode;
  80. }
  81. api.RetrieveTestList(filter.testMode, adaptor =>
  82. {
  83. // start tests if there any, otherwise send a RunFinished signal // see RIDER-91705
  84. if (adaptor.Children.Any(a => a.IsTestAssembly && assemblyNames.Contains(Path.GetFileNameWithoutExtension(a.Name))))
  85. {
  86. settings.filters = new[]
  87. {
  88. filter
  89. };
  90. _sessionGuid = api.Execute(settings);
  91. api.UnregisterCallbacks(Callback); // avoid multiple registrations
  92. api.RegisterCallbacks(Callback); // receive information about when the test suite and individual tests starts and stops.
  93. }
  94. else
  95. {
  96. CallbackData.instance.isRider = false;
  97. CallbackData.instance.events.Add(
  98. new TestEvent(EventType.RunFinished, "", "", "", 0, NUnit.Framework.Interfaces.TestStatus.Inconclusive, ""));
  99. CallbackData.instance.RaiseChangedEvent();
  100. }
  101. });
  102. #endif
  103. }
  104. [UsedImplicitly]
  105. internal static void CancelTestRun()
  106. {
  107. #if !TEST_FRAMEWORK
  108. Debug.LogError("Update Test Framework package to v.1.1.8+ to run tests from Rider.");
  109. throw new NotSupportedException("Incompatible `Test Framework` package in Unity. Update to v.1.1.8+");
  110. #else
  111. var methodInfo = typeof(TestRunnerApi).GetMethod("CancelTestRun");
  112. if (methodInfo == null)
  113. methodInfo = typeof(TestRunnerApi).GetMethod("CancelTestRun", BindingFlags.Static | BindingFlags.NonPublic);
  114. methodInfo.Invoke(null, new object[] { _sessionGuid });
  115. #endif
  116. }
  117. }
  118. }