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