UnityTestProtocolStarter.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Linq;
  3. using UnityEditor.Compilation;
  4. using UnityEditor.TestTools.TestRunner.Api;
  5. using UnityEngine;
  6. using UnityEngine.TestTools;
  7. namespace UnityEditor.TestTools.TestRunner.UnityTestProtocol
  8. {
  9. [InitializeOnLoad]
  10. internal static class UnityTestProtocolStarter
  11. {
  12. static UnityTestProtocolStarter()
  13. {
  14. var commandLineArgs = Environment.GetCommandLineArgs();
  15. if (commandLineArgs.Contains("-automated") && commandLineArgs.Contains("-runTests")) // wanna have it only for utr run
  16. {
  17. var api = ScriptableObject.CreateInstance<TestRunnerApi>();
  18. var listener = ScriptableObject.CreateInstance<UnityTestProtocolListener>();
  19. api.RegisterCallbacks(listener);
  20. CompilationPipeline.assemblyCompilationFinished += OnAssemblyCompilationFinished;
  21. }
  22. }
  23. public static void OnAssemblyCompilationFinished(string assembly, CompilerMessage[] messages)
  24. {
  25. bool checkCompileErrors = RecompileScripts.Current == null || RecompileScripts.Current.ExpectScriptCompilationSuccess;
  26. if (checkCompileErrors && messages.Any(x => x.type == CompilerMessageType.Error))
  27. {
  28. var compilerErrorMessages = messages.Where(x => x.type == CompilerMessageType.Error);
  29. var utpMessageReporter = new UtpMessageReporter(new UtpDebugLogger());
  30. utpMessageReporter.ReportAssemblyCompilationErrors(assembly, compilerErrorMessages);
  31. }
  32. }
  33. }
  34. }