TestLauncherBase.cs 3.3 KB

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