-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathUnitTestRunner.cs
More file actions
380 lines (334 loc) · 18.6 KB
/
UnitTestRunner.cs
File metadata and controls
380 lines (334 loc) · 18.6 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Security;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Extensions;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Extensions;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Logging;
using UnitTestOutcome = Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestOutcome;
using UTF = Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;
/// <summary>
/// The runner that runs a single unit test. Also manages the assembly and class cleanup methods at the end of the run.
/// </summary>
internal sealed class UnitTestRunner : MarshalByRefObject
{
private readonly ConcurrentDictionary<string, TestAssemblyInfo> _assemblyFixtureTests = new();
private readonly ConcurrentDictionary<string, TestClassInfo> _classFixtureTests = new();
private readonly TypeCache _typeCache;
private readonly ClassCleanupManager _classCleanupManager;
/// <summary>
/// Initializes a new instance of the <see cref="UnitTestRunner"/> class.
/// </summary>
/// <param name="settings"> Specifies adapter settings that need to be instantiated in the domain running these tests. </param>
/// <param name="testsToRun"> The tests to run. </param>
public UnitTestRunner(MSTestSettings? settings, UnitTestElement[] testsToRun)
: this(settings, testsToRun, ReflectHelper.Instance)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="UnitTestRunner"/> class.
/// </summary>
/// <param name="settings"> Specifies adapter settings. </param>
/// <param name="testsToRun"> The tests to run. </param>
/// <param name="reflectHelper"> The reflect Helper. </param>
internal UnitTestRunner(MSTestSettings? settings, UnitTestElement[] testsToRun, ReflectHelper reflectHelper)
{
// Populate the settings into the domain(Desktop workflow) performing discovery.
// This would just be resetting the settings to itself in non desktop workflows.
MSTestSettings.PopulateSettings(settings);
Logger.OnLogMessage += message => TestContextImplementation.CurrentTestContext?.WriteConsoleOut(message);
if (MSTestSettings.CurrentSettings.CaptureDebugTraces)
{
Console.SetOut(new ConsoleOutRouter(Console.Out));
Console.SetError(new ConsoleErrorRouter(Console.Error));
Trace.Listeners.Add(new TextWriterTraceListener(new TraceTextWriter()));
}
PlatformServiceProvider.Instance.TestRunCancellationToken ??= new TestRunCancellationToken();
_typeCache = new TypeCache(reflectHelper);
_classCleanupManager = new ClassCleanupManager(testsToRun);
}
#pragma warning disable CA1822 // Mark members as static
public void Cancel()
=> PlatformServiceProvider.Instance.TestRunCancellationToken?.Cancel();
#pragma warning restore CA1822 // Mark members as static
/// <summary>
/// Returns object to be used for controlling lifetime, null means infinite lifetime.
/// </summary>
/// <returns>
/// The <see cref="object"/>.
/// </returns>
[SecurityCritical]
#if NET5_0_OR_GREATER
[Obsolete]
#endif
public override object InitializeLifetimeService() => null!;
internal FixtureTestResult GetFixtureTestResult(TestMethod testMethod, string fixtureType)
{
// For the fixture methods, we need to return the appropriate result.
// Get matching testMethodInfo from the cache and return UnitTestOutcome for the fixture test.
if (fixtureType is EngineConstants.ClassInitializeFixtureTrait or EngineConstants.ClassCleanupFixtureTrait &&
_classFixtureTests.TryGetValue(testMethod.AssemblyName + testMethod.FullClassName, out TestClassInfo? testClassInfo))
{
UnitTestOutcome outcome = fixtureType switch
{
EngineConstants.ClassInitializeFixtureTrait => testClassInfo.IsClassInitializeExecuted ? GetOutcome(testClassInfo.ClassInitializationException) : UnitTestOutcome.Inconclusive,
EngineConstants.ClassCleanupFixtureTrait => testClassInfo.IsClassCleanupExecuted ? GetOutcome(testClassInfo.ClassCleanupException) : UnitTestOutcome.Inconclusive,
_ => throw ApplicationStateGuard.Unreachable(),
};
return new FixtureTestResult(true, outcome);
}
else if (fixtureType is EngineConstants.AssemblyInitializeFixtureTrait or EngineConstants.AssemblyCleanupFixtureTrait &&
_assemblyFixtureTests.TryGetValue(testMethod.AssemblyName, out TestAssemblyInfo? testAssemblyInfo))
{
Exception? exception = fixtureType switch
{
EngineConstants.AssemblyInitializeFixtureTrait => testAssemblyInfo.AssemblyInitializationException,
EngineConstants.AssemblyCleanupFixtureTrait => testAssemblyInfo.AssemblyCleanupException,
_ => throw ApplicationStateGuard.Unreachable(),
};
return new(true, GetOutcome(exception));
}
return new(false, UnitTestOutcome.Inconclusive);
// Local functions
static UnitTestOutcome GetOutcome(Exception? exception) => exception == null ? UnitTestOutcome.Passed : UnitTestOutcome.Failed;
}
// Task cannot cross app domains.
// For now, TestExecutionManager will call this sync method which is hacky.
// If we removed AppDomains in v4, we should use the async method and remove this one.
internal TestResult[] RunSingleTest(TestMethod testMethod, IDictionary<string, object?> testContextProperties, IMessageLogger messageLogger)
=> RunSingleTestAsync(testMethod, testContextProperties, messageLogger).GetAwaiter().GetResult();
/// <summary>
/// Runs a single test.
/// </summary>
/// <param name="testMethod"> The test Method. </param>
/// <param name="testContextProperties"> The test context properties. </param>
/// <param name="messageLogger"> The message logger. </param>
/// <returns> The <see cref="TestResult"/>. </returns>
internal async Task<TestResult[]> RunSingleTestAsync(TestMethod testMethod, IDictionary<string, object?> testContextProperties, IMessageLogger messageLogger)
{
Guard.NotNull(testMethod);
Guard.NotNull(testContextProperties);
ITestContext? testContextForTestExecution = null;
ITestContext? testContextForAssemblyInit = null;
ITestContext? testContextForClassInit = null;
ITestContext? testContextForClassCleanup = null;
ITestContext? testContextForAssemblyCleanup = null;
try
{
testContextForTestExecution = PlatformServiceProvider.Instance.GetTestContext(testMethod, null, testContextProperties, messageLogger, UTF.UnitTestOutcome.InProgress);
// Get the testMethod
TestMethodInfo? testMethodInfo = _typeCache.GetTestMethodInfo(
testMethod,
testContextForTestExecution);
TestResult[] result;
if (!IsTestMethodRunnable(testMethod, testMethodInfo, out TestResult[]? notRunnableResult))
{
result = notRunnableResult;
}
else
{
DebugEx.Assert(testMethodInfo is not null, "testMethodInfo should not be null.");
// Keep track of all non-runnable methods so that we can return the appropriate result at the end.
if (MSTestSettings.CurrentSettings.ConsiderFixturesAsSpecialTests)
{
_assemblyFixtureTests.TryAdd(testMethod.AssemblyName, testMethodInfo.Parent.Parent);
_classFixtureTests.TryAdd(testMethod.AssemblyName + testMethod.FullClassName, testMethodInfo.Parent);
}
testContextForAssemblyInit = PlatformServiceProvider.Instance.GetTestContext(testMethod: null, null, testContextProperties, messageLogger, testContextForTestExecution.Context.CurrentTestOutcome);
TestResult assemblyInitializeResult = await RunAssemblyInitializeIfNeededAsync(testMethodInfo, testContextForAssemblyInit).ConfigureAwait(false);
if (assemblyInitializeResult.Outcome != UTF.UnitTestOutcome.Passed)
{
result = [assemblyInitializeResult];
}
else
{
testContextForClassInit = PlatformServiceProvider.Instance.GetTestContext(testMethod: null, testMethod.FullClassName, testContextProperties, messageLogger, testContextForAssemblyInit.Context.CurrentTestOutcome);
TestResult classInitializeResult = await testMethodInfo.Parent.GetResultOrRunClassInitializeAsync(testContextForClassInit, assemblyInitializeResult.LogOutput, assemblyInitializeResult.LogError, assemblyInitializeResult.DebugTrace, assemblyInitializeResult.TestContextMessages).ConfigureAwait(false);
DebugEx.Assert(testMethodInfo.Parent.IsClassInitializeExecuted, "IsClassInitializeExecuted should be true after attempting to run it.");
if (classInitializeResult.Outcome != UTF.UnitTestOutcome.Passed)
{
result = [classInitializeResult];
}
else
{
// Run the test method
testContextForTestExecution.SetOutcome(testContextForClassInit.Context.CurrentTestOutcome);
RetryBaseAttribute? retryAttribute = testMethodInfo.RetryAttribute;
var testMethodRunner = new TestMethodRunner(testMethodInfo, testMethod, testContextForTestExecution);
result = await testMethodRunner.ExecuteAsync(classInitializeResult.LogOutput, classInitializeResult.LogError, classInitializeResult.DebugTrace, classInitializeResult.TestContextMessages).ConfigureAwait(false);
if (retryAttribute is not null && !RetryBaseAttribute.IsAcceptableResultForRetry(result))
{
RetryResult retryResult = await retryAttribute.ExecuteAsync(
new RetryContext(
async () => await testMethodRunner.ExecuteAsync(classInitializeResult.LogOutput, classInitializeResult.LogError, classInitializeResult.DebugTrace, classInitializeResult.TestContextMessages).ConfigureAwait(false),
result)).ConfigureAwait(false);
result = retryResult.TryGetLast() ?? throw ApplicationStateGuard.Unreachable();
}
}
}
}
testContextForClassCleanup = PlatformServiceProvider.Instance.GetTestContext(testMethod: null, testMethod.FullClassName, testContextProperties, messageLogger, testContextForTestExecution.Context.CurrentTestOutcome);
_classCleanupManager.MarkTestComplete(testMethod, out bool isLastTestInClass);
if (isLastTestInClass && testMethodInfo is not null)
{
await testMethodInfo.Parent.RunClassCleanupAsync(testContextForClassCleanup, result).ConfigureAwait(false);
// Mark the class as complete when all class cleanups are complete. When all classes are complete we progress to running assembly cleanup.
// Class is not complete until after all class cleanups are done, to prevent running assembly cleanup too early.
// Do not mark the class as complete when the last test method in the class completed. That is too early, we need to run class cleanups before marking class as complete.
_classCleanupManager.MarkClassComplete(testMethod.FullClassName);
}
if (testMethodInfo?.Parent.Parent.IsAssemblyInitializeExecuted == true &&
_classCleanupManager.ShouldRunEndOfAssemblyCleanup)
{
testContextForAssemblyCleanup = PlatformServiceProvider.Instance.GetTestContext(testMethod: null, null, testContextProperties, messageLogger, testContextForClassCleanup.Context.CurrentTestOutcome);
await RunAssemblyCleanupAsync(testContextForAssemblyCleanup, _typeCache, result).ConfigureAwait(false);
}
return result;
}
catch (TypeInspectionException ex)
{
// Catch any exception thrown while inspecting the test method and return failure.
return
[
new TestResult
{
Outcome = UnitTestOutcome.Failed,
IgnoreReason = ex.Message,
}
];
}
finally
{
(testContextForTestExecution as IDisposable)?.Dispose();
(testContextForAssemblyInit as IDisposable)?.Dispose();
(testContextForClassInit as IDisposable)?.Dispose();
(testContextForClassCleanup as IDisposable)?.Dispose();
(testContextForAssemblyCleanup as IDisposable)?.Dispose();
}
}
private static async Task<TestResult> RunAssemblyInitializeIfNeededAsync(TestMethodInfo testMethodInfo, ITestContext testContext)
{
var result = new TestResult { Outcome = UnitTestOutcome.Passed };
try
{
await testMethodInfo.Parent.Parent.RunAssemblyInitializeAsync(testContext.Context).ConfigureAwait(false);
}
catch (TestFailedException ex)
{
result = new TestResult { TestFailureException = ex, Outcome = ex.Outcome };
}
catch (Exception ex)
{
var testFailureException = new TestFailedException(UnitTestOutcome.Error, ex.TryGetMessage(), ex.TryGetStackTraceInformation());
result = new TestResult { TestFailureException = testFailureException, Outcome = UnitTestOutcome.Error };
}
finally
{
var testContextImpl = testContext.Context as TestContextImplementation;
result.LogOutput = testContextImpl?.GetOut();
result.LogError = testContextImpl?.GetErr();
result.DebugTrace = testContextImpl?.GetTrace();
result.TestContextMessages = testContext.GetAndClearDiagnosticMessages();
}
return result;
}
private static async Task RunAssemblyCleanupAsync(ITestContext testContext, TypeCache typeCache, TestResult[] results)
{
try
{
IEnumerable<TestAssemblyInfo> assemblyInfoCache = typeCache.AssemblyInfoListWithExecutableCleanupMethods;
foreach (TestAssemblyInfo assemblyInfo in assemblyInfoCache)
{
TestFailedException? ex = await assemblyInfo.ExecuteAssemblyCleanupAsync(testContext.Context).ConfigureAwait(false);
if (results.Length > 0 && ex is not null)
{
#pragma warning disable IDE0056 // Use index operator
TestResult lastResult = results[results.Length - 1];
#pragma warning restore IDE0056 // Use index operator
lastResult.Outcome = UTF.UnitTestOutcome.Error;
lastResult.TestFailureException = ex;
return;
}
}
}
finally
{
if (results.Length > 0)
{
#pragma warning disable IDE0056 // Use index operator
TestResult lastResult = results[results.Length - 1];
#pragma warning restore IDE0056 // Use index operator
var testContextImpl = testContext as TestContextImplementation;
lastResult.LogOutput += testContextImpl?.GetOut();
lastResult.LogError += testContextImpl?.GetErr();
lastResult.DebugTrace += testContextImpl?.GetTrace();
lastResult.TestContextMessages += testContext.GetAndClearDiagnosticMessages();
}
}
}
/// <summary>
/// Whether the given testMethod is runnable.
/// </summary>
/// <param name="testMethod">The testMethod.</param>
/// <param name="testMethodInfo">The testMethodInfo.</param>
/// <param name="notRunnableResult">The results to return if the test method is not runnable.</param>
/// <returns>whether the given testMethod is runnable.</returns>
private static bool IsTestMethodRunnable(
TestMethod testMethod,
TestMethodInfo? testMethodInfo,
[NotNullWhen(false)] out TestResult[]? notRunnableResult)
{
// If the specified TestMethod could not be found, return a NotFound result.
if (testMethodInfo == null)
{
{
notRunnableResult =
[
new TestResult
{
Outcome = UnitTestOutcome.NotFound,
IgnoreReason = string.Format(CultureInfo.CurrentCulture, Resource.TestNotFound, testMethod.Name),
},
];
return false;
}
}
// If test cannot be executed, then bail out.
if (!testMethodInfo.IsRunnable)
{
{
notRunnableResult =
[
new TestResult
{
Outcome = UnitTestOutcome.NotRunnable,
IgnoreReason = testMethodInfo.NotRunnableReason,
},
];
return false;
}
}
bool shouldIgnoreClass = testMethodInfo.Parent.ClassType.IsIgnored(out string? ignoreMessageOnClass);
bool shouldIgnoreMethod = testMethodInfo.MethodInfo.IsIgnored(out string? ignoreMessageOnMethod);
string? ignoreMessage = ignoreMessageOnClass;
if (StringEx.IsNullOrEmpty(ignoreMessage) && shouldIgnoreMethod)
{
ignoreMessage = ignoreMessageOnMethod;
}
if (shouldIgnoreClass || shouldIgnoreMethod)
{
notRunnableResult =
[TestResult.CreateIgnoredResult(ignoreMessage)];
return false;
}
notRunnableResult = null;
return true;
}
internal void ForceCleanup(IDictionary<string, object?> sourceLevelParameters, IMessageLogger logger) => ClassCleanupManager.ForceCleanup(_typeCache, sourceLevelParameters, logger);
}