PageRenderTime 66ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Assets/UnityTestTools/IntegrationTestsFramework/TestRunner/Editor/Batch.cs

https://gitlab.com/zerohun/RaputaJump
C# | 200 lines | 174 code | 26 blank | 0 comment | 17 complexity | f645f3965f9c927ae442b5bd673b6588 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEditorInternal;
  7. using UnityEngine;
  8. using UnityTest.IntegrationTests;
  9. using UnityEditor.SceneManagement;
  10. namespace UnityTest
  11. {
  12. public static partial class Batch
  13. {
  14. const string k_ResultFilePathParam = "-resultFilePath=";
  15. private const string k_TestScenesParam = "-testscenes=";
  16. private const string k_OtherBuildScenesParam = "-includeBuildScenes=";
  17. const string k_TargetPlatformParam = "-targetPlatform=";
  18. const string k_ResultFileDirParam = "-resultsFileDirectory=";
  19. public static int returnCodeTestsOk = 0;
  20. public static int returnCodeTestsFailed = 2;
  21. public static int returnCodeRunError = 3;
  22. public static void RunIntegrationTests()
  23. {
  24. var targetPlatform = GetTargetPlatform();
  25. var otherBuildScenes = GetSceneListFromParam (k_OtherBuildScenesParam);
  26. var testScenes = GetSceneListFromParam(k_TestScenesParam);
  27. if (testScenes.Count == 0)
  28. testScenes = FindTestScenesInProject();
  29. RunIntegrationTests(targetPlatform, testScenes, otherBuildScenes);
  30. }
  31. public static void RunIntegrationTests(BuildTarget ? targetPlatform)
  32. {
  33. var sceneList = FindTestScenesInProject();
  34. RunIntegrationTests(targetPlatform, sceneList, new List<string>());
  35. }
  36. public static void RunIntegrationTests(BuildTarget? targetPlatform, List<string> testScenes, List<string> otherBuildScenes)
  37. {
  38. if (targetPlatform.HasValue)
  39. BuildAndRun(targetPlatform.Value, testScenes, otherBuildScenes);
  40. else
  41. RunInEditor(testScenes, otherBuildScenes);
  42. }
  43. private static void BuildAndRun(BuildTarget target, List<string> testScenes, List<string> otherBuildScenes)
  44. {
  45. var resultFilePath = GetParameterArgument(k_ResultFileDirParam);
  46. const int port = 0;
  47. var ipList = TestRunnerConfigurator.GetAvailableNetworkIPs();
  48. var config = new PlatformRunnerConfiguration
  49. {
  50. buildTarget = target,
  51. buildScenes = otherBuildScenes,
  52. testScenes = testScenes,
  53. projectName = "IntegrationTests",
  54. resultsDir = resultFilePath,
  55. sendResultsOverNetwork = InternalEditorUtility.inBatchMode,
  56. ipList = ipList,
  57. port = port
  58. };
  59. if (Application.isWebPlayer)
  60. {
  61. config.sendResultsOverNetwork = false;
  62. Debug.Log("You can't use WebPlayer as active platform for running integration tests. Switching to Standalone");
  63. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.StandaloneWindows);
  64. }
  65. PlatformRunner.BuildAndRunInPlayer(config);
  66. }
  67. private static void RunInEditor(List<string> testScenes, List<string> otherBuildScenes)
  68. {
  69. CheckActiveBuildTarget();
  70. NetworkResultsReceiver.StopReceiver();
  71. if (testScenes == null || testScenes.Count == 0)
  72. {
  73. Debug.Log("No test scenes on the list");
  74. EditorApplication.Exit(returnCodeRunError);
  75. return;
  76. }
  77. string previousScenesXml = "";
  78. var serializer = new System.Xml.Serialization.XmlSerializer(typeof(EditorBuildSettingsScene[]));
  79. using(StringWriter textWriter = new StringWriter())
  80. {
  81. serializer.Serialize(textWriter, EditorBuildSettings.scenes);
  82. previousScenesXml = textWriter.ToString();
  83. }
  84. EditorBuildSettings.scenes = (testScenes.Concat(otherBuildScenes).ToList()).Select(s => new EditorBuildSettingsScene(s, true)).ToArray();
  85. EditorSceneManager.OpenScene(testScenes.First());
  86. GuiHelper.SetConsoleErrorPause(false);
  87. var config = new PlatformRunnerConfiguration
  88. {
  89. resultsDir = GetParameterArgument(k_ResultFileDirParam),
  90. ipList = TestRunnerConfigurator.GetAvailableNetworkIPs(),
  91. port = PlatformRunnerConfiguration.TryToGetFreePort(),
  92. runInEditor = true
  93. };
  94. var settings = new PlayerSettingConfigurator(true);
  95. settings.AddConfigurationFile(TestRunnerConfigurator.integrationTestsNetwork, string.Join("\n", config.GetConnectionIPs()));
  96. settings.AddConfigurationFile(TestRunnerConfigurator.testScenesToRun, string.Join ("\n", testScenes.ToArray()));
  97. settings.AddConfigurationFile(TestRunnerConfigurator.previousScenes, previousScenesXml);
  98. NetworkResultsReceiver.StartReceiver(config);
  99. EditorApplication.isPlaying = true;
  100. }
  101. private static string GetParameterArgument(string parameterName)
  102. {
  103. foreach (var arg in Environment.GetCommandLineArgs())
  104. {
  105. if (arg.ToLower().StartsWith(parameterName.ToLower()))
  106. {
  107. return arg.Substring(parameterName.Length);
  108. }
  109. }
  110. return null;
  111. }
  112. static void CheckActiveBuildTarget()
  113. {
  114. var notSupportedPlatforms = new[] { "MetroPlayer", "WebPlayer", "WebPlayerStreamed" };
  115. if (notSupportedPlatforms.Contains(EditorUserBuildSettings.activeBuildTarget.ToString()))
  116. {
  117. Debug.Log("activeBuildTarget can not be "
  118. + EditorUserBuildSettings.activeBuildTarget +
  119. " use buildTarget parameter to open Unity.");
  120. }
  121. }
  122. private static BuildTarget ? GetTargetPlatform()
  123. {
  124. string platformString = null;
  125. BuildTarget buildTarget;
  126. foreach (var arg in Environment.GetCommandLineArgs())
  127. {
  128. if (arg.ToLower().StartsWith(k_TargetPlatformParam.ToLower()))
  129. {
  130. platformString = arg.Substring(k_ResultFilePathParam.Length);
  131. break;
  132. }
  133. }
  134. try
  135. {
  136. if (platformString == null) return null;
  137. buildTarget = (BuildTarget)Enum.Parse(typeof(BuildTarget), platformString);
  138. }
  139. catch
  140. {
  141. return null;
  142. }
  143. return buildTarget;
  144. }
  145. private static List<string> FindTestScenesInProject()
  146. {
  147. var integrationTestScenePattern = "*Test?.unity";
  148. return Directory.GetFiles("Assets", integrationTestScenePattern, SearchOption.AllDirectories).ToList();
  149. }
  150. private static List<string> GetSceneListFromParam(string param)
  151. {
  152. var sceneList = new List<string>();
  153. foreach (var arg in Environment.GetCommandLineArgs())
  154. {
  155. if (arg.ToLower().StartsWith(param.ToLower()))
  156. {
  157. var scenesFromParam = arg.Substring(param.Length).Split(',');
  158. foreach (var scene in scenesFromParam)
  159. {
  160. var sceneName = scene;
  161. if (!sceneName.EndsWith(".unity"))
  162. sceneName += ".unity";
  163. var foundScenes = Directory.GetFiles(Directory.GetCurrentDirectory(), sceneName, SearchOption.AllDirectories);
  164. if (foundScenes.Length == 1)
  165. sceneList.Add(foundScenes[0].Substring(Directory.GetCurrentDirectory().Length + 1));
  166. else
  167. Debug.Log(sceneName + " not found or multiple entries found");
  168. }
  169. }
  170. }
  171. return sceneList.Where(s => !string.IsNullOrEmpty(s)).Distinct().ToList();
  172. }
  173. }
  174. }