-
Notifications
You must be signed in to change notification settings - Fork 80
/
VsTestRunner.cs
708 lines (608 loc) · 25.5 KB
/
VsTestRunner.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Xunit.Abstractions;
using Xunit.Internal;
#if NETCOREAPP
using System.Text;
using Internal.Microsoft.Extensions.DependencyModel;
#endif
namespace Xunit.Runner.VisualStudio
{
[FileExtension(".dll")]
[FileExtension(".exe")]
[DefaultExecutorUri(Constants.ExecutorUri)]
[ExtensionUri(Constants.ExecutorUri)]
[Category("managed")]
public class VsTestRunner : ITestDiscoverer, ITestExecutor
{
static int printedHeader = 0;
#if NETCOREAPP
static readonly AppDomainSupport AppDomainDefaultBehavior = AppDomainSupport.Denied;
#else
static readonly AppDomainSupport AppDomainDefaultBehavior = AppDomainSupport.Required;
#endif
static readonly HashSet<string> PlatformAssemblies = new(StringComparer.OrdinalIgnoreCase)
{
// VSTest
"microsoft.visualstudio.testplatform.unittestframework.dll",
"microsoft.visualstudio.testplatform.core.dll",
"microsoft.visualstudio.testplatform.testexecutor.core.dll",
"microsoft.visualstudio.testplatform.extensions.msappcontaineradapter.dll",
"microsoft.visualstudio.testplatform.objectmodel.dll",
"microsoft.visualstudio.testplatform.utilities.dll",
"vstest.executionengine.appcontainer.exe",
"vstest.executionengine.appcontainer.x86.exe",
// xUnit.net VSTest adapter
"xunit.runner.visualstudio.testadapter.dll",
"xunit.runner.visualstudio.dotnetcore.testadapter.dll",
// xUnit.net v3 (core)
"xunit.v3.assert.dll",
"xunit.v3.common.dll",
"xunit.v3.core.dll",
// xUnit.net v3 (runners)
"xunit.v3.runner.common.dll",
"xunit.v3.runner.inproc.console.dll",
"xunit.v3.runner.tdnet.dll",
"xunit.v3.runner.utility.net472.dll",
"xunit.v3.runner.utility.netstandard20.dll",
// xUnit.net v2 (core)
"xunit.abstractions.dll",
"xunit.assert.dll",
"xunit.core.dll",
"xunit.execution.desktop.dll", // 2.0.0+
"xunit.execution.iOS-Universal.dll", // 2.0.0
"xunit.execution.MonoAndroid.dll", // 2.0.0
"xunit.execution.MonoTouch.dll", // 2.0.0
"xunit.execution.universal.dll", // 2.0.0
"xunit.execution.win8.dll", // 2.0.0
"xunit.execution.wp8.dll", // 2.0.0
"xunit.execution.dotnet.dll", // 2.1.0+
// xUnit.net v2 (runners)
"xunit.runner.reporters.desktop.dll", // 2.1.0
"xunit.runner.reporters.dotnet.dll", // 2.1.0
"xunit.runner.reporters.net452.dll", // 2.2.0+
"xunit.runner.reporters.netstandard11.dll", // 2.2.0+
"xunit.runner.reporters.netstandard15.dll", // 2.2.0+
"xunit.runner.reporters.netstandard20.dll", // 2.4.0
"xunit.runner.reporters.netcoreapp10.dll", // 2.3.0+
"xunit.runner.tdnet.dll",
"xunit.runner.utility.iOS-Universal.dll", // 2.0.0
"xunit.runner.utility.MonoAndroid.dll", // 2.0.0
"xunit.runner.utility.MonoTouch.dll", // 2.0.0
"xunit.runner.utility.universal.dll", // 2.0.0
"xunit.runner.utility.win8.dll", // 2.0.0
"xunit.runner.utility.wp8.dll", // 2.0.0
"xunit.runner.utility.desktop.dll", // 2.1.0
"xunit.runner.utility.dotnet.dll", // 2.1.0
"xunit.runner.utility.net35.dll", // 2.2.0+
"xunit.runner.utility.net452.dll", // 2.2.0+
"xunit.runner.utility.netstandard11.dll", // 2.2.0+
"xunit.runner.utility.netstandard15.dll", // 2.2.0+
"xunit.runner.utility.netcoreapp10.dll", // 2.3.0+
"xunit.runner.utility.netstandard20.dll", // 2.4.0
"xunit.runner.utility.uwp10.dll", // 2.4.0-2.4.2
// xUnit.net v1
"xunit.dll",
};
public static TestProperty SerializedTestCaseProperty { get; } = GetTestProperty();
bool cancelled;
public void Cancel()
{
cancelled = true;
}
void ITestDiscoverer.DiscoverTests(
IEnumerable<string> sources,
IDiscoveryContext discoveryContext,
IMessageLogger logger,
ITestCaseDiscoverySink discoverySink)
{
Guard.ArgumentNotNull(sources);
Guard.ArgumentNotNull(logger);
Guard.ArgumentNotNull(discoverySink);
var stopwatch = Stopwatch.StartNew();
var loggerHelper = new LoggerHelper(logger, stopwatch);
PrintHeader(loggerHelper);
var runSettings = RunSettings.Parse(discoveryContext.RunSettings?.SettingsXml);
if (!runSettings.IsMatchingTargetFramework())
return;
var testPlatformContext = new TestPlatformContext
{
// Force serialization because in either case (command line discovery or IDE discovery) we may be
// asked later to run specific test cases, so we can't rely on DesignMode to make the decision.
RequireSerialization = true,
RequireSourceInformation = runSettings.CollectSourceInformation,
};
var testCaseFilter = new TestCaseFilter(discoveryContext, loggerHelper);
DiscoverTests(
sources, loggerHelper, testPlatformContext, runSettings,
(source, discoverer, discoveryOptions) => new VsDiscoverySink(source, discoverer, loggerHelper, discoverySink, discoveryOptions, testPlatformContext, testCaseFilter, () => cancelled)
);
}
static void PrintHeader(LoggerHelper loggerHelper)
{
if (Interlocked.Exchange(ref printedHeader, 1) == 0)
loggerHelper.Log($"xUnit.net VSTest Adapter v{ThisAssembly.AssemblyInformationalVersion} ({IntPtr.Size * 8}-bit {RuntimeInformation.FrameworkDescription})");
}
void ITestExecutor.RunTests(
IEnumerable<string>? sources,
IRunContext? runContext,
IFrameworkHandle? frameworkHandle)
{
if (sources is null)
return;
var stopwatch = Stopwatch.StartNew();
var logger = new LoggerHelper(frameworkHandle, stopwatch);
PrintHeader(logger);
var runSettings = RunSettings.Parse(runContext?.RunSettings?.SettingsXml);
if (!runSettings.IsMatchingTargetFramework())
return;
var testPlatformContext = new TestPlatformContext
{
RequireSerialization = !runSettings.DisableSerialization,
RequireSourceInformation = runSettings.CollectSourceInformation,
};
RunTests(
runContext, frameworkHandle, logger, testPlatformContext, runSettings,
() => sources.Select(source => new AssemblyRunInfo(runSettings, GetAssemblyFileName(source))).ToList()
);
}
void ITestExecutor.RunTests(
IEnumerable<TestCase>? tests,
IRunContext? runContext,
IFrameworkHandle? frameworkHandle)
{
if (tests is null)
return;
var stopwatch = Stopwatch.StartNew();
var logger = new LoggerHelper(frameworkHandle, stopwatch);
var runSettings = RunSettings.Parse(runContext?.RunSettings?.SettingsXml);
PrintHeader(logger);
var testPlatformContext = new TestPlatformContext
{
RequireSerialization = !runSettings.DisableSerialization,
RequireSourceInformation = runSettings.CollectSourceInformation,
};
RunTests(
runContext, frameworkHandle, logger, testPlatformContext, runSettings,
() =>
tests
.GroupBy(testCase => testCase.Source)
.Select(group => new AssemblyRunInfo(runSettings, group.Key, group.ToList()))
.ToList()
);
}
// Helpers
void DiscoverTests<TVisitor>(
IEnumerable<string> sources,
LoggerHelper logger,
TestPlatformContext testPlatformContext,
RunSettings runSettings,
Func<string, ITestFrameworkDiscoverer, ITestFrameworkDiscoveryOptions, TVisitor> visitorFactory,
Action<string, ITestFrameworkDiscoverer, ITestFrameworkDiscoveryOptions, TVisitor>? visitComplete = null)
where TVisitor : IVsDiscoverySink, IDisposable
{
try
{
RemotingUtility.CleanUpRegisteredChannels();
var internalDiagnosticsMessageSink = DiagnosticMessageSink.ForInternalDiagnostics(logger, runSettings.InternalDiagnosticMessages ?? false);
using var _ = AssemblyHelper.SubscribeResolveForAssembly(typeof(VsTestRunner), MessageSinkAdapter.Wrap(internalDiagnosticsMessageSink));
foreach (var assemblyFileNameCanBeWithoutAbsolutePath in sources)
{
var assembly = new XunitProjectAssembly { AssemblyFilename = GetAssemblyFileName(assemblyFileNameCanBeWithoutAbsolutePath) };
runSettings.CopyTo(assembly.Configuration);
var fileName = Path.GetFileNameWithoutExtension(assembly.AssemblyFilename);
var shadowCopy = assembly.Configuration.ShadowCopyOrDefault;
var diagnosticSink = DiagnosticMessageSink.ForDiagnostics(logger, fileName, assembly.Configuration.DiagnosticMessagesOrDefault);
var appDomain = assembly.Configuration.AppDomain ?? AppDomainDefaultBehavior;
using var sourceInformationProvider = new VisualStudioSourceInformationProvider(assembly.AssemblyFilename, diagnosticSink);
using var controller = new XunitFrontController(appDomain, assembly.AssemblyFilename, shadowCopy: shadowCopy, sourceInformationProvider: sourceInformationProvider, diagnosticMessageSink: MessageSinkAdapter.Wrap(diagnosticSink));
if (!DiscoverTestsInSource(controller, logger, testPlatformContext, runSettings, visitorFactory, visitComplete, assembly))
break;
}
}
catch (Exception e)
{
logger.LogWarning("Exception discovering tests: {0}", e.Unwrap());
}
}
bool DiscoverTestsInSource<TVisitor>(
XunitFrontController controller,
LoggerHelper logger,
TestPlatformContext testPlatformContext,
RunSettings runSettings,
Func<string, ITestFrameworkDiscoverer, ITestFrameworkDiscoveryOptions, TVisitor> visitorFactory,
Action<string, ITestFrameworkDiscoverer, ITestFrameworkDiscoveryOptions, TVisitor>? visitComplete,
XunitProjectAssembly assembly)
where TVisitor : IVsDiscoverySink, IDisposable
{
if (cancelled)
return false;
var fileName = "(unknown assembly)";
try
{
var reporterMessageHandler = GetRunnerReporter(logger, runSettings, new[] { assembly.AssemblyFilename }).CreateMessageHandler(new VisualStudioRunnerLogger(logger));
fileName = Path.GetFileNameWithoutExtension(assembly.AssemblyFilename);
if (!IsXunitTestAssembly(assembly.AssemblyFilename))
{
if (assembly.Configuration.DiagnosticMessagesOrDefault)
logger.Log("Skipping: {0} (no reference to xUnit.net)", fileName);
}
else
{
var discoveryOptions = TestFrameworkOptions.ForDiscovery(assembly.Configuration);
using var visitor = visitorFactory(assembly.AssemblyFilename, controller, discoveryOptions);
var totalTests = 0;
var appDomain = assembly.Configuration.AppDomain ?? AppDomainDefaultBehavior;
var usingAppDomains = controller.CanUseAppDomains && appDomain != AppDomainSupport.Denied;
reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryStarting(assembly, usingAppDomains, assembly.Configuration.ShadowCopyOrDefault, discoveryOptions));
try
{
controller.Find(testPlatformContext.RequireSourceInformation, visitor, discoveryOptions);
totalTests = visitor.Finish();
visitComplete?.Invoke(assembly.AssemblyFilename, controller, discoveryOptions, visitor);
}
finally
{
reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryFinished(assembly, discoveryOptions, totalTests, totalTests));
}
}
}
catch (Exception e)
{
var ex = e.Unwrap();
if (ex is InvalidOperationException)
logger.LogWarning("Skipping: {0} ({1})", fileName, ex.Message);
else if (ex is FileNotFoundException fileNotFound)
logger.LogWarning("Skipping: {0} (could not find dependent assembly '{1}')", fileName, Path.GetFileNameWithoutExtension(fileNotFound.FileName));
else if (ex is FileLoadException fileLoad)
logger.LogWarning("Skipping: {0} (could not load dependent assembly '{1}'): {2}", fileName, Path.GetFileNameWithoutExtension(fileLoad.FileName), ex.Message);
else
logger.LogWarning("Exception discovering tests from {0}: {1}", fileName, ex);
}
return true;
}
static TestProperty GetTestProperty() =>
TestProperty.Register("XunitTestCase", "xUnit.net Test Case", typeof(string), typeof(VsTestRunner));
static bool IsXunitTestAssembly(string assemblyFileName)
{
// Don't try to load ourselves (or any test framework assemblies), since we fail (issue #47 in xunit/xunit).
if (PlatformAssemblies.Contains(Path.GetFileName(assemblyFileName)))
return false;
#if NETCOREAPP
return IsXunitPackageReferenced(assemblyFileName);
#else
var assemblyFolder = Path.GetDirectoryName(assemblyFileName);
if (assemblyFolder is null)
return false;
return File.Exists(Path.Combine(assemblyFolder, "xunit.dll"))
|| Directory.GetFiles(assemblyFolder, "xunit.execution.*.dll").Length > 0;
#endif
}
#if NETCOREAPP
static bool IsXunitPackageReferenced(string assemblyFileName)
{
var depsFile = assemblyFileName.Replace(".dll", ".deps.json");
if (!File.Exists(depsFile))
return false;
try
{
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(depsFile)));
var context = new DependencyContextJsonReader().Read(stream);
var xunitLibrary = context.RuntimeLibraries.Where(lib => lib.Name.Equals("xunit") || lib.Name.Equals("xunit.core")).FirstOrDefault();
return xunitLibrary is not null;
}
catch
{
return false;
}
}
#endif
static string GetAssemblyFileName(string source) =>
Path.GetFullPath(source);
void RunTests(
IRunContext? runContext,
IFrameworkHandle? frameworkHandle,
LoggerHelper logger,
TestPlatformContext testPlatformContext,
RunSettings runSettings,
Func<List<AssemblyRunInfo>> getRunInfos)
{
Guard.ArgumentNotNull(runContext);
Guard.ArgumentNotNull(frameworkHandle);
try
{
RemotingUtility.CleanUpRegisteredChannels();
cancelled = false;
var runInfos = getRunInfos();
var parallelizeAssemblies = runInfos.All(runInfo => runInfo.Assembly.Configuration.ParallelizeAssemblyOrDefault);
var reporter = GetRunnerReporter(logger, runSettings, runInfos.Select(ari => ari.Assembly.AssemblyFilename).ToList());
using var reporterMessageHandler = MessageSinkWithTypesAdapter.Wrap(reporter.CreateMessageHandler(new VisualStudioRunnerLogger(logger)));
using var internalDiagnosticsMessageSink = DiagnosticMessageSink.ForInternalDiagnostics(logger, runSettings.InternalDiagnosticMessages ?? false);
using (AssemblyHelper.SubscribeResolveForAssembly(typeof(VsTestRunner), MessageSinkAdapter.Wrap(internalDiagnosticsMessageSink)))
{
if (parallelizeAssemblies)
runInfos
.Select(runInfo => RunTestsInAssemblyAsync(runContext, frameworkHandle, logger, testPlatformContext, runSettings, reporterMessageHandler, runInfo))
.ToList()
.ForEach(@event => @event.WaitOne());
else
runInfos.ForEach(runInfo => RunTestsInAssembly(runContext, frameworkHandle, logger, testPlatformContext, runSettings, reporterMessageHandler, runInfo));
}
}
catch (Exception ex)
{
logger.LogError("Catastrophic failure: {0}", ex);
}
}
void RunTestsInAssembly(
IRunContext runContext,
IFrameworkHandle frameworkHandle,
LoggerHelper logger,
TestPlatformContext testPlatformContext,
RunSettings runSettings,
IMessageSinkWithTypes reporterMessageHandler,
AssemblyRunInfo runInfo)
{
if (cancelled)
return;
var assemblyDisplayName = "(unknown assembly)";
try
{
var assemblyFileName = runInfo.Assembly.AssemblyFilename;
assemblyDisplayName = Path.GetFileNameWithoutExtension(assemblyFileName);
var configuration = runInfo.Assembly.Configuration;
var shadowCopy = configuration.ShadowCopyOrDefault;
var appDomain = configuration.AppDomain ?? AppDomainDefaultBehavior;
var longRunningSeconds = configuration.LongRunningTestSecondsOrDefault;
var diagnosticSink = DiagnosticMessageSink.ForDiagnostics(logger, assemblyDisplayName, runInfo.Assembly.Configuration.DiagnosticMessagesOrDefault);
var diagnosticMessageSink = MessageSinkAdapter.Wrap(diagnosticSink);
using var sourceInformationProvider = new VisualStudioSourceInformationProvider(assemblyFileName, diagnosticSink);
using var controller = new XunitFrontController(appDomain, assemblyFileName, shadowCopy: shadowCopy, sourceInformationProvider: sourceInformationProvider, diagnosticMessageSink: diagnosticMessageSink);
var testCasesMap = new Dictionary<string, TestCase>();
var testCases = new List<ITestCase>();
if (runInfo.TestCases is null || !runInfo.TestCases.Any())
{
// Discover tests
var assemblyDiscoveredInfo = default(AssemblyDiscoveredInfo);
DiscoverTestsInSource(controller, logger, testPlatformContext, runSettings,
(source, discoverer, discoveryOptions) => new VsExecutionDiscoverySink(() => cancelled),
(source, discoverer, discoveryOptions, visitor) =>
{
if (discoveryOptions.GetInternalDiagnosticMessagesOrDefault())
foreach (var testCase in visitor.TestCases)
logger.Log(testCase, "Discovered [execution] test case '{0}' (ID = '{1}')",
testCase.DisplayName, testCase.UniqueID);
assemblyDiscoveredInfo = new AssemblyDiscoveredInfo(source, GetVsTestCases(source, discoverer, visitor, logger, testPlatformContext));
},
runInfo.Assembly
);
if (assemblyDiscoveredInfo is null || assemblyDiscoveredInfo.DiscoveredTestCases is null || !assemblyDiscoveredInfo.DiscoveredTestCases.Any())
{
if (configuration.InternalDiagnosticMessagesOrDefault)
logger.LogWarning("Skipping '{0}' since no tests were found during discovery [execution].", assemblyFileName);
return;
}
// Filter tests
var traitNames = new HashSet<string>(assemblyDiscoveredInfo.DiscoveredTestCases.SelectMany(testCase => testCase.TraitNames));
var filter = new TestCaseFilter(runContext, logger, assemblyDiscoveredInfo.AssemblyFileName, traitNames);
var filteredTestCases = assemblyDiscoveredInfo.DiscoveredTestCases.Where(dtc => dtc.VSTestCase is not null && filter.MatchTestCase(dtc.VSTestCase)).ToList();
foreach (var filteredTestCase in filteredTestCases)
{
var uniqueID = filteredTestCase.UniqueID;
if (testCasesMap.ContainsKey(uniqueID))
logger.LogWarning(filteredTestCase.TestCase, "Skipping test case with duplicate ID '{0}' ('{1}' and '{2}')", uniqueID, testCasesMap[uniqueID].DisplayName, filteredTestCase.VSTestCase?.DisplayName);
else if (filteredTestCase.VSTestCase is not null)
{
testCasesMap.Add(uniqueID, filteredTestCase.VSTestCase);
testCases.Add(filteredTestCase.TestCase);
}
}
}
else
{
try
{
var serializations =
runInfo
.TestCases
.Select(tc => tc.GetPropertyValue<string>(SerializedTestCaseProperty, null))
.ToList();
if (configuration.InternalDiagnosticMessagesOrDefault)
logger.LogWithSource(
runInfo.Assembly.AssemblyFilename,
"Deserializing {0} test case(s):{1}{2}",
serializations.Count,
Environment.NewLine,
string.Join(Environment.NewLine, serializations.Select(x => $" {x}"))
);
var deserializedTestCasesByUniqueId = controller.BulkDeserialize(serializations);
if (deserializedTestCasesByUniqueId is null)
logger.LogErrorWithSource(assemblyFileName, "Received null response from BulkDeserialize");
else
{
for (var idx = 0; idx < runInfo.TestCases.Count; ++idx)
{
try
{
var kvp = deserializedTestCasesByUniqueId[idx];
var vsTestCase = runInfo.TestCases[idx];
if (kvp.Value is null)
{
logger.LogErrorWithSource(assemblyFileName, "Test case {0} failed to deserialize: {1}", vsTestCase.DisplayName, kvp.Key);
}
else
{
testCasesMap.Add(kvp.Key, vsTestCase);
testCases.Add(kvp.Value);
}
}
catch (Exception ex)
{
logger.LogErrorWithSource(assemblyFileName, "Catastrophic error deserializing item #{0}: {1}", idx, ex);
}
}
}
}
catch (Exception ex)
{
logger.LogWarningWithSource(assemblyFileName, "Catastrophic error during deserialization: {0}", ex);
}
}
// Execute tests
var executionOptions = TestFrameworkOptions.ForExecution(runInfo.Assembly.Configuration);
if (!runInfo.Assembly.Configuration.ParallelizeTestCollectionsOrDefault)
{
executionOptions.SetSynchronousMessageReporting(true);
executionOptions.SetDisableParallelization(true);
}
reporterMessageHandler.OnMessage(new TestAssemblyExecutionStarting(runInfo.Assembly, executionOptions));
using var vsExecutionSink = new VsExecutionSink(reporterMessageHandler, frameworkHandle, logger, testCasesMap, () => cancelled);
IExecutionSink resultsSink = vsExecutionSink;
if (longRunningSeconds > 0)
resultsSink = new DelegatingLongRunningTestDetectionSink(resultsSink, TimeSpan.FromSeconds(longRunningSeconds), diagnosticSink);
if (configuration.FailSkipsOrDefault)
resultsSink = new DelegatingFailSkipSink(resultsSink);
controller.RunTests(testCases, resultsSink, executionOptions);
resultsSink.Finished.WaitOne();
reporterMessageHandler.OnMessage(new TestAssemblyExecutionFinished(runInfo.Assembly, executionOptions, resultsSink.ExecutionSummary));
if ((resultsSink.ExecutionSummary.Failed != 0 || resultsSink.ExecutionSummary.Errors != 0) && executionOptions.GetStopOnTestFailOrDefault())
{
logger.Log("Canceling due to test failure...");
cancelled = true;
}
}
catch (Exception ex)
{
logger.LogError("{0}: Catastrophic failure: {1}", assemblyDisplayName, ex);
}
}
ManualResetEvent RunTestsInAssemblyAsync(
IRunContext runContext,
IFrameworkHandle frameworkHandle,
LoggerHelper logger,
TestPlatformContext testPlatformContext,
RunSettings runSettings,
IMessageSinkWithTypes reporterMessageHandler,
AssemblyRunInfo runInfo)
{
var @event = new ManualResetEvent(initialState: false);
void handler()
{
try
{
RunTestsInAssembly(runContext, frameworkHandle, logger, testPlatformContext, runSettings, reporterMessageHandler, runInfo);
}
finally
{
@event.Set();
}
}
ThreadPool.QueueUserWorkItem(_ => handler());
return @event;
}
public static IRunnerReporter GetRunnerReporter(
LoggerHelper? logger,
RunSettings runSettings,
IReadOnlyList<string> assemblyFileNames)
{
var reporter = default(IRunnerReporter);
var availableReporters = new Lazy<IReadOnlyList<IRunnerReporter>>(() => GetAvailableRunnerReporters(logger, assemblyFileNames));
try
{
if (!string.IsNullOrEmpty(runSettings.ReporterSwitch))
{
reporter = availableReporters.Value.FirstOrDefault(r => string.Equals(r.RunnerSwitch, runSettings.ReporterSwitch, StringComparison.OrdinalIgnoreCase));
if (reporter is null && logger is not null)
logger.LogWarning("Could not find requested reporter '{0}'", runSettings.ReporterSwitch);
}
if (reporter is null && !(runSettings.NoAutoReporters ?? false))
reporter = availableReporters.Value.FirstOrDefault(r => r.IsEnvironmentallyEnabled);
}
catch { }
return reporter ?? new DefaultRunnerReporterWithTypes();
}
public static IReadOnlyList<IRunnerReporter> GetAvailableRunnerReporters(
LoggerHelper? logger,
IReadOnlyList<string> sources)
{
var result = new List<IRunnerReporter>();
var adapterAssembly = typeof(VsTestRunner).Assembly;
// We need to combine the source folders with our folder to find all potential runners
var folders =
sources
.Select(s => Path.GetDirectoryName(Path.GetFullPath(s)))
.WhereNotNull()
.Concat(new[] { Path.GetDirectoryName(adapterAssembly.GetLocalCodeBase()) })
.Distinct();
foreach (var folder in folders)
{
result.AddRange(RunnerReporterUtility.GetAvailableRunnerReporters(folder, out var messages));
if (logger is not null)
foreach (var message in messages)
logger.LogWarning(message);
}
return result;
}
static IList<DiscoveredTestCase> GetVsTestCases(
string source,
ITestFrameworkDiscoverer discoverer,
VsExecutionDiscoverySink visitor,
LoggerHelper logger,
TestPlatformContext testPlatformContext)
{
var descriptorProvider = (discoverer as ITestCaseDescriptorProvider) ?? new DefaultTestCaseDescriptorProvider(discoverer);
var testCases = visitor.TestCases;
var descriptors = descriptorProvider.GetTestCaseDescriptors(testCases, false);
var results = new List<DiscoveredTestCase>(descriptors.Count);
for (var idx = 0; idx < descriptors.Count; ++idx)
{
var testCase = new DiscoveredTestCase(source, descriptors[idx], testCases[idx], logger, testPlatformContext);
if (testCase.VSTestCase is not null)
results.Add(testCase);
}
return results;
}
class AssemblyDiscoveredInfo
{
public AssemblyDiscoveredInfo(
string assemblyFileName,
IList<DiscoveredTestCase> discoveredTestCases)
{
AssemblyFileName = assemblyFileName;
DiscoveredTestCases = discoveredTestCases;
}
public string AssemblyFileName { get; }
public IList<DiscoveredTestCase> DiscoveredTestCases { get; }
}
class DiscoveredTestCase
{
public string Name { get; }
public IEnumerable<string> TraitNames { get; }
public TestCase? VSTestCase { get; }
public ITestCase TestCase { get; }
public string UniqueID { get; }
public DiscoveredTestCase(
string source,
TestCaseDescriptor descriptor,
ITestCase testCase,
LoggerHelper logger,
TestPlatformContext testPlatformContext)
{
Name = $"{descriptor.ClassName}.{descriptor.MethodName} ({descriptor.UniqueID})";
TestCase = testCase;
UniqueID = descriptor.UniqueID;
VSTestCase = VsDiscoverySink.CreateVsTestCase(source, descriptor, logger, testPlatformContext);
TraitNames = descriptor.Traits.Keys;
}
}
}
}