RuntimeTestLauncherBase.cs
4.26 KB
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
83
84
85
86
87
88
89
90
91
92
using System;
using System.Linq;
using NUnit.Framework.Interfaces;
using UnityEditor.Events;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestRunner.NUnitExtensions.Runner;
using UnityEngine.TestTools;
using UnityEngine.TestTools.NUnitExtensions;
using UnityEngine.TestTools.TestRunner;
using UnityEngine.TestTools.Utils;
namespace UnityEditor.TestTools.TestRunner
{
internal abstract class RuntimeTestLauncherBase : TestLauncherBase
{
protected Scene CreateBootstrapScene(string sceneName, Action<PlaymodeTestsController> runnerSetup)
{
var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
var go = new GameObject(PlaymodeTestsController.kPlaymodeTestControllerName);
var editorLoadedTestAssemblyProvider = new EditorLoadedTestAssemblyProvider(new EditorCompilationInterfaceProxy(), new EditorAssembliesProxy());
var runner = go.AddComponent<PlaymodeTestsController>();
runnerSetup(runner);
runner.settings.bootstrapScene = sceneName;
runner.AssembliesWithTests = editorLoadedTestAssemblyProvider.GetAssembliesGroupedByType(TestPlatform.PlayMode).Select(x => x.Assembly.GetName().Name).ToList();
EditorSceneManager.MarkSceneDirty(scene);
AssetDatabase.SaveAssets();
EditorSceneManager.SaveScene(scene, sceneName, false);
return scene;
}
public string CreateSceneName()
{
return "Assets/InitTestScene" + DateTime.Now.Ticks + ".unity";
}
protected UnityTestAssemblyRunner LoadTests(ITestFilter filter)
{
var editorLoadedTestAssemblyProvider = new EditorLoadedTestAssemblyProvider(new EditorCompilationInterfaceProxy(), new EditorAssembliesProxy());
var assembliesWithTests = editorLoadedTestAssemblyProvider.GetAssembliesGroupedByType(TestPlatform.PlayMode).Select(x => x.Assembly.GetName().Name).ToList();
var nUnitTestAssemblyRunner = new UnityTestAssemblyRunner(new UnityTestAssemblyBuilder(), null);
var assemblyProvider = new PlayerTestAssemblyProvider(new AssemblyLoadProxy(), assembliesWithTests);
nUnitTestAssemblyRunner.Load(assemblyProvider.GetUserAssemblies().Select(a => a.Assembly).ToArray(), TestPlatform.PlayMode, UnityTestAssemblyBuilder.GetNUnitTestBuilderSettings(TestPlatform.PlayMode));
return nUnitTestAssemblyRunner;
}
protected static void ReopenOriginalScene(string originalSceneName)
{
EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);
if (!string.IsNullOrEmpty(originalSceneName))
{
EditorSceneManager.OpenScene(originalSceneName);
}
}
}
internal static class PlaymodeTestsControllerExtensions
{
internal static T AddEventHandlerMonoBehaviour<T>(this PlaymodeTestsController controller) where T : MonoBehaviour, ITestRunnerListener
{
var eventHandler = controller.gameObject.AddComponent<T>();
SetListeners(controller, eventHandler);
return eventHandler;
}
internal static T AddEventHandlerScriptableObject<T>(this PlaymodeTestsController controller) where T : ScriptableObject, ITestRunnerListener
{
var eventListener = ScriptableObject.CreateInstance<T>();
AddEventHandlerScriptableObject(controller, eventListener);
return eventListener;
}
internal static void AddEventHandlerScriptableObject(this PlaymodeTestsController controller, ITestRunnerListener obj)
{
SetListeners(controller, obj);
}
private static void SetListeners(PlaymodeTestsController controller, ITestRunnerListener eventHandler)
{
UnityEventTools.AddPersistentListener(controller.testStartedEvent, eventHandler.TestStarted);
UnityEventTools.AddPersistentListener(controller.testFinishedEvent, eventHandler.TestFinished);
UnityEventTools.AddPersistentListener(controller.runStartedEvent, eventHandler.RunStarted);
UnityEventTools.AddPersistentListener(controller.runFinishedEvent, eventHandler.RunFinished);
}
}
}