CompositeWorkItem.cs
12.8 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using NUnit.Framework.Internal.Execution;
using UnityEngine.TestTools.Logging;
using UnityEngine.TestTools.TestRunner;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal class CompositeWorkItem : UnityWorkItem
{
private readonly TestSuite _suite;
private readonly TestSuiteResult _suiteResult;
private readonly ITestFilter _childFilter;
private TestCommand _setupCommand;
private TestCommand _teardownCommand;
public List<UnityWorkItem> Children { get; private set; }
private int _countOrder;
private CountdownEvent _childTestCountdown;
public CompositeWorkItem(TestSuite suite, ITestFilter childFilter, WorkItemFactory factory)
: base(suite, factory)
{
_suite = suite;
_suiteResult = Result as TestSuiteResult;
_childFilter = childFilter;
_countOrder = 0;
}
protected override IEnumerable PerformWork()
{
InitializeSetUpAndTearDownCommands();
if (UnityTestExecutionContext.CurrentContext != null && m_DontRunRestoringResult && EditModeTestCallbacks.RestoringTestContext != null)
{
EditModeTestCallbacks.RestoringTestContext();
}
if (!CheckForCancellation())
if (Test.RunState == RunState.Explicit && !_childFilter.IsExplicitMatch(Test))
SkipFixture(ResultState.Explicit, GetSkipReason(), null);
else
switch (Test.RunState)
{
default:
case RunState.Runnable:
case RunState.Explicit:
Result.SetResult(ResultState.Success);
CreateChildWorkItems();
if (Children.Count > 0)
{
if (!m_DontRunRestoringResult)
{
//This is needed to give the editor a chance to go out of playmode if needed before creating objects.
//If we do not, the objects could be automatically destroyed when exiting playmode and could result in errors later on
yield return null;
PerformOneTimeSetUp();
}
if (!CheckForCancellation())
{
switch (Result.ResultState.Status)
{
case TestStatus.Passed:
foreach (var child in RunChildren())
{
if (CheckForCancellation())
{
yield break;
}
yield return child;
}
break;
case TestStatus.Skipped:
case TestStatus.Inconclusive:
case TestStatus.Failed:
SkipChildren(_suite, Result.ResultState.WithSite(FailureSite.Parent), "OneTimeSetUp: " + Result.Message);
break;
}
}
if (Context.ExecutionStatus != TestExecutionStatus.AbortRequested && !m_DontRunRestoringResult)
{
PerformOneTimeTearDown();
}
}
break;
case RunState.Skipped:
SkipFixture(ResultState.Skipped, GetSkipReason(), null);
break;
case RunState.Ignored:
SkipFixture(ResultState.Ignored, GetSkipReason(), null);
break;
case RunState.NotRunnable:
SkipFixture(ResultState.NotRunnable, GetSkipReason(), GetProviderStackTrace());
break;
}
if (!ResultedInDomainReload)
{
WorkItemComplete();
}
}
private bool CheckForCancellation()
{
if (Context.ExecutionStatus != TestExecutionStatus.Running)
{
Result.SetResult(ResultState.Cancelled, "Test cancelled by user");
return true;
}
return false;
}
private void InitializeSetUpAndTearDownCommands()
{
List<SetUpTearDownItem> setUpTearDownItems = _suite.TypeInfo != null
? CommandBuilder.BuildSetUpTearDownList(_suite.TypeInfo.Type, typeof(OneTimeSetUpAttribute), typeof(OneTimeTearDownAttribute))
: new List<SetUpTearDownItem>();
var actionItems = new List<TestActionItem>();
foreach (ITestAction action in Actions)
{
bool applyToSuite = (action.Targets & ActionTargets.Suite) == ActionTargets.Suite
|| action.Targets == ActionTargets.Default && !(Test is ParameterizedMethodSuite);
bool applyToTest = (action.Targets & ActionTargets.Test) == ActionTargets.Test
&& !(Test is ParameterizedMethodSuite);
if (applyToSuite)
actionItems.Add(new TestActionItem(action));
if (applyToTest)
Context.UpstreamActions.Add(action);
}
_setupCommand = CommandBuilder.MakeOneTimeSetUpCommand(_suite, setUpTearDownItems, actionItems);
_teardownCommand = CommandBuilder.MakeOneTimeTearDownCommand(_suite, setUpTearDownItems, actionItems);
}
private void PerformOneTimeSetUp()
{
var logScope = new LogScope();
try
{
_setupCommand.Execute(Context);
}
catch (Exception ex)
{
if (ex is NUnitException || ex is TargetInvocationException)
ex = ex.InnerException;
Result.RecordException(ex, FailureSite.SetUp);
}
if (logScope.AnyFailingLogs())
{
Result.RecordException(new UnhandledLogMessageException(logScope.FailingLogs.First()));
}
logScope.Dispose();
}
private IEnumerable RunChildren()
{
int childCount = Children.Count;
if (childCount == 0)
throw new InvalidOperationException("RunChildren called but item has no children");
_childTestCountdown = new CountdownEvent(childCount);
foreach (UnityWorkItem child in Children)
{
if (CheckForCancellation())
{
yield break;
}
var unityTestExecutionContext = new UnityTestExecutionContext(Context);
child.InitializeContext(unityTestExecutionContext);
var enumerable = child.Execute().GetEnumerator();
while (true)
{
if (!enumerable.MoveNext())
{
break;
}
ResultedInDomainReload |= child.ResultedInDomainReload;
yield return enumerable.Current;
}
_suiteResult.AddResult(child.Result);
childCount--;
}
if (childCount > 0)
{
while (childCount-- > 0)
CountDownChildTest();
}
}
private void CreateChildWorkItems()
{
Children = new List<UnityWorkItem>();
var testSuite = _suite;
foreach (ITest test in testSuite.Tests)
{
if (_childFilter.Pass(test))
{
var child = m_Factory.Create(test, _childFilter);
if (test.Properties.ContainsKey(PropertyNames.Order))
{
Children.Insert(0, child);
_countOrder++;
}
else
{
Children.Add(child);
}
}
}
if (_countOrder != 0) SortChildren();
}
private class UnityWorkItemOrderComparer : IComparer<UnityWorkItem>
{
public int Compare(UnityWorkItem x, UnityWorkItem y)
{
var xKey = int.MaxValue;
var yKey = int.MaxValue;
if (x.Test.Properties.ContainsKey(PropertyNames.Order))
xKey = (int)x.Test.Properties[PropertyNames.Order][0];
if (y.Test.Properties.ContainsKey(PropertyNames.Order))
yKey = (int)y.Test.Properties[PropertyNames.Order][0];
return xKey.CompareTo(yKey);
}
}
private void SortChildren()
{
Children.Sort(0, _countOrder, new UnityWorkItemOrderComparer());
}
private void SkipFixture(ResultState resultState, string message, string stackTrace)
{
Result.SetResult(resultState.WithSite(FailureSite.SetUp), message, StackFilter.Filter(stackTrace));
SkipChildren(_suite, resultState.WithSite(FailureSite.Parent), "OneTimeSetUp: " + message);
}
private void SkipChildren(TestSuite suite, ResultState resultState, string message)
{
foreach (Test child in suite.Tests)
{
if (_childFilter.Pass(child))
{
Context.Listener.TestStarted(child);
TestResult childResult = child.MakeTestResult();
childResult.SetResult(resultState, message);
_suiteResult.AddResult(childResult);
if (child.IsSuite)
SkipChildren((TestSuite)child, resultState, message);
Context.Listener.TestFinished(childResult);
}
}
}
private void PerformOneTimeTearDown()
{
var logScope = new LogScope();
try
{
_teardownCommand.Execute(Context);
}
catch (Exception ex)
{
if (ex is NUnitException || ex is TargetInvocationException)
ex = ex.InnerException;
Result.RecordException(ex, FailureSite.SetUp);
}
if (logScope.AnyFailingLogs())
{
Result.RecordException(new UnhandledLogMessageException(logScope.FailingLogs.First()));
}
logScope.Dispose();
}
private string GetSkipReason()
{
return (string)Test.Properties.Get(PropertyNames.SkipReason);
}
private string GetProviderStackTrace()
{
return (string)Test.Properties.Get(PropertyNames.ProviderStackTrace);
}
private void CountDownChildTest()
{
_childTestCountdown.Signal();
if (_childTestCountdown.CurrentCount == 0)
{
if (Context.ExecutionStatus != TestExecutionStatus.AbortRequested)
PerformOneTimeTearDown();
foreach (var childResult in _suiteResult.Children)
if (childResult.ResultState == ResultState.Cancelled)
{
this.Result.SetResult(ResultState.Cancelled, "Cancelled by user");
break;
}
WorkItemComplete();
}
}
public override void Cancel(bool force)
{
if (Children == null)
return;
foreach (var child in Children)
{
var ctx = child.Context;
if (ctx != null)
ctx.ExecutionStatus = force ? TestExecutionStatus.AbortRequested : TestExecutionStatus.StopRequested;
if (child.State == WorkItemState.Running)
child.Cancel(force);
}
}
}
}