-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathTestExecutionManager.cs
481 lines (402 loc) · 21.9 KB
/
TestExecutionManager.cs
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
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.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Class responsible for execution of tests at assembly level and sending tests via framework handle
/// </summary>
public class TestExecutionManager
{
/// <summary>
/// Specifies whether the test run is canceled or not
/// </summary>
private TestRunCancellationToken cancellationToken;
/// <summary>
/// Dictionary for test run parameters
/// </summary>
private IDictionary<string, object> sessionParameters;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline", Justification = "Need to over-write the keys in dictionary.")]
public TestExecutionManager()
{
this.TestMethodFilter = new TestMethodFilter();
this.sessionParameters = new Dictionary<string, object>();
}
/// <summary>
/// Gets or sets method filter for filtering tests
/// </summary>
private TestMethodFilter TestMethodFilter { get; set; }
/// <summary>
/// Gets or sets a value indicating whether any test executed has failed.
/// </summary>
private bool HasAnyTestFailed { get; set; }
/// <summary>
/// Runs the tests.
/// </summary>
/// <param name="tests">Tests to be run.</param>
/// <param name="runContext">Context to use when executing the tests.</param>
/// <param name="frameworkHandle">Handle to the framework to record results and to do framework operations.</param>
/// <param name="runCancellationToken">Test run cancellation tokenn</param>
public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle, TestRunCancellationToken runCancellationToken)
{
Debug.Assert(tests != null, "tests");
Debug.Assert(runContext != null, "runContext");
Debug.Assert(frameworkHandle != null, "frameworkHandle");
Debug.Assert(runCancellationToken != null, "runCancellationToken");
this.cancellationToken = runCancellationToken;
var isDeploymentDone = PlatformServiceProvider.Instance.TestDeployment.Deploy(tests, runContext, frameworkHandle);
// Placing this after deployment since we need information post deployment that we pass in as properties.
this.CacheSessionParameters(runContext, frameworkHandle);
// Execute the tests
this.ExecuteTests(tests, runContext, frameworkHandle, isDeploymentDone);
if (!this.HasAnyTestFailed)
{
PlatformServiceProvider.Instance.TestDeployment.Cleanup();
}
}
public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrameworkHandle frameworkHandle, TestRunCancellationToken cancellationToken)
{
this.cancellationToken = cancellationToken;
var discoverySink = new TestCaseDiscoverySink();
var tests = new List<TestCase>();
// deploy everything first.
foreach (var source in sources)
{
if (this.cancellationToken.Canceled)
{
break;
}
var logger = (IMessageLogger)frameworkHandle;
// discover the tests
this.GetUnitTestDiscoverer().DiscoverTestsInSource(source, logger, discoverySink, runContext);
tests.AddRange(discoverySink.Tests);
// Clear discoverSinksTests so that it just stores test for one source at one point of time
discoverySink.Tests.Clear();
}
bool isDeploymentDone = PlatformServiceProvider.Instance.TestDeployment.Deploy(tests, runContext, frameworkHandle);
// Placing this after deployment since we need information post deployment that we pass in as properties.
this.CacheSessionParameters(runContext, frameworkHandle);
// Run tests.
this.ExecuteTests(tests, runContext, frameworkHandle, isDeploymentDone);
if (!this.HasAnyTestFailed)
{
PlatformServiceProvider.Instance.TestDeployment.Cleanup();
}
}
/// <summary>
/// Execute the parameter tests
/// </summary>
/// <param name="tests">Tests to execute.</param>
/// <param name="runContext">The run context.</param>
/// <param name="frameworkHandle">Handle to record test start/end/results.</param>
/// <param name="isDeploymentDone">Indicates if deployment is done.</param>
internal virtual void ExecuteTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle, bool isDeploymentDone)
{
var testsBySource = from test in tests
group test by test.Source into testGroup
select new { Source = testGroup.Key, Tests = testGroup };
foreach (var group in testsBySource)
{
this.ExecuteTestsInSource(group.Tests, runContext, frameworkHandle, group.Source, isDeploymentDone);
}
}
internal virtual UnitTestDiscoverer GetUnitTestDiscoverer()
{
return new UnitTestDiscoverer();
}
internal void SendTestResults(TestCase test, UnitTestResult[] unitTestResults, DateTimeOffset startTime, DateTimeOffset endTime, ITestExecutionRecorder testExecutionRecorder)
{
if (!(unitTestResults?.Length > 0))
{
return;
}
foreach (var unitTestResult in unitTestResults)
{
if (test == null)
{
continue;
}
var testResult = unitTestResult.ToTestResult(test, startTime, endTime, MSTestSettings.CurrentSettings);
if (unitTestResult.DatarowIndex >= 0)
{
testResult.DisplayName = string.Format(CultureInfo.CurrentCulture, Resource.DataDrivenResultDisplayName, test.DisplayName, unitTestResult.DatarowIndex);
}
testExecutionRecorder.RecordEnd(test, testResult.Outcome);
if (testResult.Outcome == TestOutcome.Failed)
{
PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("MSTestExecutor:Test {0} failed. ErrorMessage:{1}, ErrorStackTrace:{2}.", testResult.TestCase.FullyQualifiedName, testResult.ErrorMessage, testResult.ErrorStackTrace);
this.HasAnyTestFailed = true;
}
try
{
testExecutionRecorder.RecordResult(testResult);
}
catch (TestCanceledException)
{
// Ignore this exception
}
}
}
private static bool MatchTestFilter(ITestCaseFilterExpression filterExpression, TestCase test, TestMethodFilter testMethodFilter)
{
if (filterExpression != null && filterExpression.MatchTestCase(test, p => testMethodFilter.PropertyValueProvider(test, p)) == false)
{
// Skip test if not fitting filter criteria.
return false;
}
return true;
}
/// <summary>
/// Execute the parameter tests present in parameter source
/// </summary>
/// <param name="tests">Tests to execute.</param>
/// <param name="runContext">The run context.</param>
/// <param name="frameworkHandle">Handle to record test start/end/results.</param>
/// <param name="source">The test container for the tests.</param>
/// <param name="isDeploymentDone">Indicates if deployment is done.</param>
private void ExecuteTestsInSource(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle, string source, bool isDeploymentDone)
{
Debug.Assert(!string.IsNullOrEmpty(source), "Source cannot be empty");
if (isDeploymentDone)
{
source = Path.Combine(PlatformServiceProvider.Instance.TestDeployment.GetDeploymentDirectory(), Path.GetFileName(source));
}
using (var isolationHost = PlatformServiceProvider.Instance.CreateTestSourceHost(source, runContext?.RunSettings, frameworkHandle))
{
// Create an instance of a type defined in adapter so that adapter gets loaded in the child app domain
var testRunner = isolationHost.CreateInstanceForType(
typeof(UnitTestRunner),
new object[] { MSTestSettings.CurrentSettings }) as UnitTestRunner;
// After loading adapter reset the chils-domain's appbase to point to test source location
isolationHost.UpdateAppBaseToTestSourceLocation();
PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("Created unit-test runner {0}", source);
// Default test set is filtered tests based on user provided filter criteria
IEnumerable<TestCase> testsToRun = Enumerable.Empty<TestCase>();
var filterExpression = this.TestMethodFilter.GetFilterExpression(runContext, frameworkHandle, out var filterHasError);
if (filterHasError)
{
// Bail out without processing everything else below.
return;
}
testsToRun = tests.Where(t => MatchTestFilter(filterExpression, t, this.TestMethodFilter));
// this is done so that appropriate values of testcontext properties are set at source level
// and are merged with session level parameters
var sourceLevelParameters = PlatformServiceProvider.Instance.SettingsProvider.GetProperties(source);
if (this.sessionParameters != null && this.sessionParameters.Count > 0)
{
sourceLevelParameters = sourceLevelParameters.Concat(this.sessionParameters).ToDictionary(x => x.Key, x => x.Value);
}
var sourceSettingsProvider = isolationHost.CreateInstanceForType(
typeof(TestAssemblySettingsProvider),
null) as TestAssemblySettingsProvider;
var sourceSettings = sourceSettingsProvider.GetSettings(source);
var parallelWorkers = sourceSettings.Workers;
var parallelScope = sourceSettings.Scope;
if (MSTestSettings.CurrentSettings.ParallelizationWorkers.HasValue)
{
// The runsettings value takes precedence over an assembly level setting. Reset the level.
parallelWorkers = MSTestSettings.CurrentSettings.ParallelizationWorkers.Value;
}
if (MSTestSettings.CurrentSettings.ParallelizationScope.HasValue)
{
// The runsettings value takes precedence over an assembly level setting. Reset the level.
parallelScope = MSTestSettings.CurrentSettings.ParallelizationScope.Value;
}
if (!MSTestSettings.CurrentSettings.DisableParallelization && sourceSettings.CanParallelizeAssembly && parallelWorkers > 0)
{
// Parallelization is enabled. Let's do further classification for sets.
var logger = (IMessageLogger)frameworkHandle;
logger.SendMessage(
TestMessageLevel.Informational,
string.Format(CultureInfo.CurrentCulture, Resource.TestParallelizationBanner, source, parallelWorkers, parallelScope));
// Create test sets for execution, we can execute them in parallel based on parallel settings
IEnumerable<IGrouping<bool, TestCase>> testsets = Enumerable.Empty<IGrouping<bool, TestCase>>();
// Parallel and not parallel sets.
testsets = testsToRun.GroupBy(t => t.GetPropertyValue<bool>(TestAdapter.Constants.DoNotParallelizeProperty, false));
var parallelizableTestSet = testsets.FirstOrDefault(g => g.Key == false);
var nonparallelizableTestSet = testsets.FirstOrDefault(g => g.Key == true);
if (parallelizableTestSet != null)
{
ConcurrentQueue<IEnumerable<TestCase>> queue = null;
// Chunk the sets into further groups based on parallel level
switch (parallelScope)
{
case ExecutionScope.MethodLevel:
queue = new ConcurrentQueue<IEnumerable<TestCase>>(parallelizableTestSet.Select(t => new[] { t }));
break;
case ExecutionScope.ClassLevel:
queue = new ConcurrentQueue<IEnumerable<TestCase>>(parallelizableTestSet.GroupBy(t => t.GetPropertyValue(TestAdapter.Constants.TestClassNameProperty) as string));
break;
}
var tasks = new List<Task>();
for (int i = 0; i < parallelWorkers; i++)
{
tasks.Add(Task.Factory.StartNew(
() =>
{
while (!queue.IsEmpty)
{
if (this.cancellationToken != null && this.cancellationToken.Canceled)
{
// if a cancellation has been requested, do not queue any more test runs.
break;
}
if (queue.TryDequeue(out IEnumerable<TestCase> testSet))
{
this.ExecuteTestsWithTestRunner(testSet, runContext, frameworkHandle, source, sourceLevelParameters, testRunner);
}
}
},
CancellationToken.None,
TaskCreationOptions.LongRunning,
TaskScheduler.Default));
}
Task.WaitAll(tasks.ToArray());
}
// Queue the non parallel set
if (nonparallelizableTestSet != null)
{
this.ExecuteTestsWithTestRunner(nonparallelizableTestSet, runContext, frameworkHandle, source, sourceLevelParameters, testRunner);
}
}
else
{
this.ExecuteTestsWithTestRunner(testsToRun, runContext, frameworkHandle, source, sourceLevelParameters, testRunner);
}
this.RunCleanup(frameworkHandle, testRunner);
PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo(
"Executed tests belonging to source {0}",
source);
}
}
private void ExecuteTestsWithTestRunner(
IEnumerable<TestCase> tests,
IRunContext runContext,
ITestExecutionRecorder testExecutionRecorder,
string source,
IDictionary<string, object> sourceLevelParameters,
UnitTestRunner testRunner)
{
foreach (var currentTest in tests)
{
if (this.cancellationToken != null && this.cancellationToken.Canceled)
{
break;
}
var unitTestElement = currentTest.ToUnitTestElement(source);
testExecutionRecorder.RecordStart(currentTest);
var startTime = DateTimeOffset.Now;
PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo(
"Executing test {0}",
unitTestElement.TestMethod.Name);
// Run single test passing test context properties to it.
var tcmProperties = TcmTestPropertiesProvider.GetTcmProperties(currentTest);
var testContextProperties = this.GetTestContextProperties(tcmProperties, sourceLevelParameters);
var unitTestResult = testRunner.RunSingleTest(unitTestElement.TestMethod, testContextProperties);
PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo(
"Executed test {0}",
unitTestElement.TestMethod.Name);
var endTime = DateTimeOffset.Now;
this.SendTestResults(currentTest, unitTestResult, startTime, endTime, testExecutionRecorder);
}
}
/// <summary>
/// Get test context properties.
/// </summary>
/// <param name="tcmProperties">Tcm properties.</param>
/// <param name="sourceLevelParameters">Source level parameters.</param>
/// <returns>Test context properties.</returns>
private IDictionary<string, object> GetTestContextProperties(IDictionary<TestProperty, object> tcmProperties, IDictionary<string, object> sourceLevelParameters)
{
var testContextProperties = new Dictionary<string, object>();
// Add tcm properties.
foreach (var propertyPair in tcmProperties)
{
testContextProperties[propertyPair.Key.Id] = propertyPair.Value;
}
// Add source level parameters.
foreach (var propertyPair in sourceLevelParameters)
{
testContextProperties[propertyPair.Key] = propertyPair.Value;
}
return testContextProperties;
}
private void RunCleanup(
ITestExecutionRecorder testExecutionRecorder,
UnitTestRunner testRunner)
{
// All cleanups (Class and Assembly) run at the end of test execution. Failures in these cleanup
// methods will be reported as Warnings.
PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("Executing cleanup methods.");
var cleanupResult = testRunner.RunCleanup();
PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("Executed cleanup methods.");
if (cleanupResult != null)
{
IList<string> warnings = cleanupResult.Warnings;
// Do not attach the standard output and error messages to any test result. It is not
// guaranteed that a test method of same class would have run last. We will end up
// adding stdout to test method of another class.
this.LogWarnings(testExecutionRecorder, warnings);
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle errors in user specified run parameters")]
private void CacheSessionParameters(IRunContext runContext, ITestExecutionRecorder testExecutionRecorder)
{
if (!string.IsNullOrEmpty(runContext?.RunSettings?.SettingsXml))
{
try
{
var testRunParameters = RunSettingsUtilities.GetTestRunParameters(runContext.RunSettings.SettingsXml);
if (testRunParameters != null)
{
// Clear sessionParameters to prevent key collisions of test run parameters in case
// "Keep Test Execution Engine Alive" is selected in VS.
this.sessionParameters.Clear();
foreach (var kvp in testRunParameters)
{
this.sessionParameters.Add(kvp);
}
}
}
catch (Exception ex)
{
testExecutionRecorder.SendMessage(TestMessageLevel.Error, ex.Message);
}
}
}
/// <summary>
/// Log the parameter warnings on the parameter logger
/// </summary>
/// <param name="testExecutionRecorder">Handle to record test start/end/results/messages.</param>
/// <param name="warnings">Any warnings during run operation.</param>
private void LogWarnings(ITestExecutionRecorder testExecutionRecorder, IEnumerable<string> warnings)
{
if (warnings == null)
{
return;
}
Debug.Assert(testExecutionRecorder != null, "Logger should not be null");
// log the warnings
foreach (string warning in warnings)
{
testExecutionRecorder.SendMessage(TestMessageLevel.Warning, warning);
}
}
}
}