UnityTestAssemblyRunner.cs
3.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
93
94
95
96
97
98
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using UnityEngine.TestTools;
using UnityEngine.TestTools.NUnitExtensions;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal interface IUnityTestAssemblyRunner
{
ITest LoadedTest { get; }
ITestResult Result { get; }
bool IsTestLoaded { get; }
bool IsTestRunning { get; }
bool IsTestComplete { get; }
UnityWorkItem TopLevelWorkItem { get; set; }
UnityTestExecutionContext GetCurrentContext();
ITest Load(Assembly[] assemblies, TestPlatform testPlatform, IDictionary<string, object> settings);
IEnumerable Run(ITestListener listener, ITestFilter filter);
void StopRun();
}
internal class UnityTestAssemblyRunner : IUnityTestAssemblyRunner
{
private readonly UnityTestAssemblyBuilder unityBuilder;
private readonly WorkItemFactory m_Factory;
protected UnityTestExecutionContext Context { get; set; }
public UnityTestExecutionContext GetCurrentContext()
{
return UnityTestExecutionContext.CurrentContext;
}
protected IDictionary<string, object> Settings { get; set; }
public ITest LoadedTest { get; protected set; }
public ITestResult Result
{
get { return TopLevelWorkItem == null ? null : TopLevelWorkItem.Result; }
}
public bool IsTestLoaded
{
get { return LoadedTest != null; }
}
public bool IsTestRunning
{
get { return TopLevelWorkItem != null && TopLevelWorkItem.State == NUnit.Framework.Internal.Execution.WorkItemState.Running; }
}
public bool IsTestComplete
{
get { return TopLevelWorkItem != null && TopLevelWorkItem.State == NUnit.Framework.Internal.Execution.WorkItemState.Complete; }
}
public UnityTestAssemblyRunner(UnityTestAssemblyBuilder builder, WorkItemFactory factory)
{
unityBuilder = builder;
m_Factory = factory;
Context = new UnityTestExecutionContext();
}
public ITest Load(Assembly[] assemblies, TestPlatform testPlatform, IDictionary<string, object> settings)
{
Settings = settings;
if (settings.ContainsKey(FrameworkPackageSettings.RandomSeed))
Randomizer.InitialSeed = (int)settings[FrameworkPackageSettings.RandomSeed];
return LoadedTest = unityBuilder.Build(assemblies, Enumerable.Repeat(testPlatform, assemblies.Length).ToArray(), settings);
}
public IEnumerable Run(ITestListener listener, ITestFilter filter)
{
TopLevelWorkItem = m_Factory.Create(LoadedTest, filter);
TopLevelWorkItem.InitializeContext(Context);
UnityTestExecutionContext.CurrentContext = Context;
Context.Listener = listener;
return TopLevelWorkItem.Execute();
}
public UnityWorkItem TopLevelWorkItem { get; set; }
public void StopRun()
{
if (IsTestRunning)
{
TopLevelWorkItem.Cancel(false);
}
}
}
}