No Description
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.

TestLauncherBase.cs 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework.Interfaces;
  4. using Unity.Profiling;
  5. using UnityEngine;
  6. using UnityEngine.Profiling;
  7. using UnityEngine.TestTools;
  8. using UnityEngine.TestTools.Logging;
  9. using UnityEngine.TestTools.TestRunner;
  10. namespace UnityEditor.TestTools.TestRunner
  11. {
  12. internal abstract class TestLauncherBase
  13. {
  14. public abstract void Run();
  15. protected virtual RuntimePlatform? TestTargetPlatform
  16. {
  17. get { return Application.platform; }
  18. }
  19. protected bool ExecutePreBuildSetupMethods(ITest tests, ITestFilter testRunnerFilter)
  20. {
  21. using (new ProfilerMarker(nameof(ExecutePreBuildSetupMethods)).Auto()) {
  22. var attributeFinder = new PrebuildSetupAttributeFinder();
  23. var logString = "Executing setup for: {0}";
  24. return ExecuteMethods<IPrebuildSetup>(tests, testRunnerFilter, attributeFinder, logString, targetClass => targetClass.Setup(), TestTargetPlatform);
  25. }
  26. }
  27. public void ExecutePostBuildCleanupMethods(ITest tests, ITestFilter testRunnerFilter)
  28. {
  29. using (new ProfilerMarker(nameof(ExecutePostBuildCleanupMethods)).Auto())
  30. ExecutePostBuildCleanupMethods(tests, testRunnerFilter, TestTargetPlatform);
  31. }
  32. public static void ExecutePostBuildCleanupMethods(ITest tests, ITestFilter testRunnerFilter, RuntimePlatform? testTargetPlatform)
  33. {
  34. using (new ProfilerMarker(nameof(ExecutePostBuildCleanupMethods)).Auto()) {
  35. var attributeFinder = new PostbuildCleanupAttributeFinder();
  36. var logString = "Executing cleanup for: {0}";
  37. ExecuteMethods<IPostBuildCleanup>(tests, testRunnerFilter, attributeFinder, logString, targetClass => targetClass.Cleanup(), testTargetPlatform);
  38. }
  39. }
  40. private static bool ExecuteMethods<T>(ITest tests, ITestFilter testRunnerFilter, AttributeFinderBase attributeFinder, string logString, Action<T> action, RuntimePlatform? testTargetPlatform)
  41. {
  42. var exceptionsThrown = false;
  43. if (testTargetPlatform == null)
  44. {
  45. Debug.LogError("Could not determine test target platform from build target " + EditorUserBuildSettings.activeBuildTarget);
  46. return true;
  47. }
  48. foreach (var targetClassType in attributeFinder.Search(tests, testRunnerFilter, testTargetPlatform.Value))
  49. {
  50. try
  51. {
  52. var targetClass = (T)Activator.CreateInstance(targetClassType);
  53. Debug.LogFormat(logString, targetClassType.FullName);
  54. using (var logScope = new LogScope())
  55. {
  56. action(targetClass);
  57. if (logScope.AnyFailingLogs())
  58. {
  59. var failingLog = logScope.FailingLogs.First();
  60. throw new UnhandledLogMessageException(failingLog);
  61. }
  62. if (logScope.ExpectedLogs.Any())
  63. {
  64. var expectedLogs = logScope.ExpectedLogs.First();
  65. throw new UnexpectedLogMessageException(expectedLogs);
  66. }
  67. }
  68. }
  69. catch (InvalidCastException) {}
  70. catch (Exception e)
  71. {
  72. Debug.LogException(e);
  73. exceptionsThrown = true;
  74. }
  75. }
  76. return exceptionsThrown;
  77. }
  78. }
  79. }