暫無描述
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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. logScope.EvaluateLogScope(true);
  58. }
  59. }
  60. catch (InvalidCastException) {}
  61. catch (Exception e)
  62. {
  63. Debug.LogException(e);
  64. exceptionsThrown = true;
  65. }
  66. }
  67. return exceptionsThrown;
  68. }
  69. }
  70. }