暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TestLauncherBase.cs 3.0KB

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