-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathTestMethodInfo.cs
More file actions
1113 lines (978 loc) · 48.4 KB
/
TestMethodInfo.cs
File metadata and controls
1113 lines (978 loc) · 48.4 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
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#if NETFRAMEWORK
using System.Runtime.Remoting.Messaging;
#endif
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.Execution;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Extensions;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;
/// <summary>
/// Defines the TestMethod Info object.
/// </summary>
#pragma warning disable CA1852 // Seal internal types - This class is inherited in tests.
internal class TestMethodInfo : ITestMethod
{
/// <summary>
/// Specifies the timeout when it is not set in a test case.
/// </summary>
public const int TimeoutWhenNotSet = 0;
private object? _classInstance;
private bool _isTestContextSet;
private bool _isTestCleanupInvoked;
private ExecutionContext? _executionContext;
#if NETFRAMEWORK
private object? _hostContext;
#endif
internal TestMethodInfo(MethodInfo testMethod, TestClassInfo parent)
{
DebugEx.Assert(testMethod != null, "TestMethod should not be null");
DebugEx.Assert(parent != null, "Parent should not be null");
MethodInfo = testMethod;
Parent = parent;
RetryAttribute = GetRetryAttribute();
TimeoutInfo = GetTestTimeout();
Executor = GetTestMethodAttribute();
}
internal TimeoutInfo TimeoutInfo { get; /*For testing only*/set; }
internal TestMethodAttribute Executor { get; /*For testing only*/set; }
internal ITestContext TestContext
{
get => field ?? (ITestContext?)TestTools.UnitTesting.TestContext.Current ?? throw ApplicationStateGuard.Unreachable();
set;
}
/// <summary>
/// Gets a value indicating whether timeout is set.
/// </summary>
public bool IsTimeoutSet => TimeoutInfo.Timeout != TimeoutWhenNotSet;
/// <summary>
/// Gets the parameter types of the test method.
/// </summary>
public ParameterInfo[] ParameterTypes => MethodInfo.GetParameters();
/// <summary>
/// Gets the return type of the test method.
/// </summary>
public Type ReturnType => MethodInfo.ReturnType;
/// <summary>
/// Gets the name of the class declaring the test method.
/// </summary>
public string TestClassName => Parent.ClassType.FullName!;
/// <summary>
/// Gets the name of the test method.
/// </summary>
public string TestMethodName => MethodInfo.Name;
/// <summary>
/// Gets the methodInfo for test method.
/// </summary>
public MethodInfo MethodInfo { get; }
/// <summary>
/// Gets the arguments with which test method is invoked.
/// </summary>
public object?[]? Arguments { get; private set; }
/// <summary>
/// Gets the parent class Info object.
/// </summary>
internal TestClassInfo Parent { get; }
internal RetryBaseAttribute? RetryAttribute { get; }
/// <summary>
/// Gets all attributes of the test method.
/// </summary>
/// <returns>An array of the attributes.</returns>
public Attribute[]? GetAllAttributes()
=> [.. ReflectHelper.Instance.GetAttributes<Attribute>(MethodInfo)];
/// <summary>
/// Gets all attributes of the test method.
/// </summary>
/// <typeparam name="TAttributeType">The type of the attributes.</typeparam>
/// <returns>An array of the attributes.</returns>
public TAttributeType[] GetAttributes<TAttributeType>()
where TAttributeType : Attribute
=> [.. ReflectHelper.Instance.GetAttributes<TAttributeType>(MethodInfo)];
/// <summary>
/// Execute test method. Capture failures, handle async and return result.
/// </summary>
/// <param name="arguments">
/// Arguments to pass to test method. (E.g. For data driven).
/// </param>
/// <returns>Result of test method invocation.</returns>
public virtual async Task<TestResult> InvokeAsync(object?[]? arguments)
{
Stopwatch watch = new();
TestResult? result = null;
// check if arguments are set for data driven tests
arguments ??= Arguments;
watch.Start();
try
{
result = IsTimeoutSet
? await ExecuteInternalWithTimeoutAsync(arguments).ConfigureAwait(false)
: await ExecuteInternalAsync(arguments, null).ConfigureAwait(false);
}
finally
{
// Handle logs & debug traces.
watch.Stop();
if (result != null)
{
var testContextImpl = TestContext as TestContextImplementation;
result.LogOutput = testContextImpl?.GetAndClearOut();
result.LogError = testContextImpl?.GetAndClearErr();
result.DebugTrace = testContextImpl?.GetAndClearTrace();
result.TestContextMessages = TestContext?.GetAndClearDiagnosticMessages();
result.ResultFiles = TestContext?.GetResultFiles();
result.Duration = watch.Elapsed;
}
_executionContext?.Dispose();
_executionContext = null;
#if NETFRAMEWORK
_hostContext = null;
#endif
}
return result;
}
internal void SetArguments(object?[]? arguments) => Arguments = arguments == null ? null : ResolveArguments(arguments);
internal object?[] ResolveArguments(object?[] arguments)
{
ParameterInfo[] parametersInfo = MethodInfo.GetParameters();
int requiredParameterCount = 0;
bool hasParamsValue = false;
object? paramsValues = null;
foreach (ParameterInfo parameter in parametersInfo)
{
// If this is a params array parameter, create an instance to
// populate with any extra values provided. Don't increment
// required parameter count - params arguments are not actually required
if (parameter.GetCustomAttribute<ParamArrayAttribute>() != null)
{
hasParamsValue = true;
break;
}
// Count required parameters from method
if (!parameter.IsOptional)
{
requiredParameterCount++;
}
}
// If all the parameters are required, we have fewer arguments
// supplied than required, or more arguments than the method takes
// and it doesn't have a params parameter don't try and resolve anything
if (requiredParameterCount == parametersInfo.Length ||
arguments.Length < requiredParameterCount ||
(!hasParamsValue && arguments.Length > parametersInfo.Length))
{
return arguments;
}
object?[] newParameters = new object[parametersInfo.Length];
for (int argumentIndex = 0; argumentIndex < arguments.Length; argumentIndex++)
{
// We have reached the end of the regular parameters and any additional
// values will go in a params array
if (argumentIndex >= parametersInfo.Length - 1 && hasParamsValue)
{
// If this is the params parameter, instantiate a new object of that type
if (argumentIndex == parametersInfo.Length - 1)
{
paramsValues = PlatformServiceProvider.Instance.ReflectionOperations.CreateInstance(parametersInfo[argumentIndex].ParameterType, [arguments.Length - argumentIndex]);
newParameters[argumentIndex] = paramsValues;
}
// The params parameters is an array but the type is not known
// set the values as a generic array
if (paramsValues is Array paramsArray)
{
paramsArray.SetValue(arguments[argumentIndex], argumentIndex - (parametersInfo.Length - 1));
}
}
else
{
newParameters[argumentIndex] = arguments[argumentIndex];
}
}
// If arguments supplied are less than total possible arguments set
// the values supplied to the default values for those parameters
for (int parameterNotProvidedIndex = arguments.Length; parameterNotProvidedIndex < parametersInfo.Length; parameterNotProvidedIndex++)
{
// If this is the params parameters, set it to an empty
// array of that type as DefaultValue is DBNull
newParameters[parameterNotProvidedIndex] = hasParamsValue && parameterNotProvidedIndex == parametersInfo.Length - 1
? PlatformServiceProvider.Instance.ReflectionOperations.CreateInstance(parametersInfo[parameterNotProvidedIndex].ParameterType, [0])
: parametersInfo[parameterNotProvidedIndex].DefaultValue;
}
return newParameters;
}
/// <summary>
/// Gets the test timeout for the test method.
/// </summary>
/// <returns> The timeout value if defined in milliseconds. 0 if not defined. </returns>
private TimeoutInfo GetTestTimeout()
{
DebugEx.Assert(MethodInfo != null, "TestMethod should be non-null");
TimeoutAttribute? timeoutAttribute = ReflectHelper.Instance.GetFirstAttributeOrDefault<TimeoutAttribute>(MethodInfo);
if (timeoutAttribute is null)
{
return TimeoutInfo.FromTestTimeoutSettings();
}
if (!timeoutAttribute.HasCorrectTimeout)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorInvalidTimeout, MethodInfo.DeclaringType!.FullName, MethodInfo.Name);
throw new TypeInspectionException(message);
}
return TimeoutInfo.FromTimeoutAttribute(timeoutAttribute);
}
/// <summary>
/// Provides the Test Method Extension Attribute of the TestClass.
/// </summary>
/// <returns>Test Method Attribute.</returns>
private TestMethodAttribute GetTestMethodAttribute()
{
// Get the derived TestMethod attribute from reflection.
// It should be non-null as it was already validated by IsValidTestMethod.
TestMethodAttribute testMethodAttribute = ReflectHelper.Instance.GetSingleAttributeOrDefault<TestMethodAttribute>(MethodInfo)!;
// Get the derived TestMethod attribute from Extended TestClass Attribute
// If the extended TestClass Attribute doesn't have extended TestMethod attribute then base class returns back the original testMethod Attribute
return Parent.ClassAttribute.GetTestMethodAttribute(testMethodAttribute) ?? testMethodAttribute;
}
/// <summary>
/// Gets the number of retries this test method should make in case of failure.
/// </summary>
/// <returns>
/// The number of retries, which is always greater than or equal to 1.
/// If RetryAttribute is not present, returns 1.
/// </returns>
private RetryBaseAttribute? GetRetryAttribute()
{
IEnumerable<RetryBaseAttribute> attributes = ReflectHelper.Instance.GetAttributes<RetryBaseAttribute>(MethodInfo);
using IEnumerator<RetryBaseAttribute> enumerator = attributes.GetEnumerator();
if (!enumerator.MoveNext())
{
return null;
}
RetryBaseAttribute attribute = enumerator.Current;
if (enumerator.MoveNext())
{
ThrowMultipleAttributesException(nameof(RetryBaseAttribute));
}
return attribute;
}
[DoesNotReturn]
private void ThrowMultipleAttributesException(string attributeName)
{
// Note: even if the given attribute has AllowMultiple = false, we can
// still reach here if a derived attribute authored by the user re-defines AttributeUsage
string errorMessage = string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_MultipleAttributesOnTestMethod,
Parent.ClassType.FullName,
MethodInfo.Name,
attributeName);
throw new TypeInspectionException(errorMessage);
}
/// <summary>
/// Execute test without timeout.
/// </summary>
/// <param name="arguments">Arguments to be passed to the method.</param>
/// <param name="timeoutTokenSource">The timeout token source.</param>
/// <returns>The result of the execution.</returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle all kinds of user exceptions and message appropriately.")]
private async Task<TestResult> ExecuteInternalAsync(object?[]? arguments, CancellationTokenSource? timeoutTokenSource)
{
DebugEx.Assert(MethodInfo != null, "UnitTestExecuter.DefaultTestMethodInvoke: testMethod = null.");
var result = new TestResult();
Exception? testRunnerException = null;
_isTestCleanupInvoked = false;
try
{
try
{
// We invoke global test initialize methods before creating the test class instance.
// We consider the test class constructor as a "local" initialization.
// We want to invoke first the global initializations, then local ones, then test method.
// After that, we invoke local cleanups (including Dispose) and finally global cleanups at last.
foreach ((MethodInfo method, TimeoutInfo? timeoutInfo) in Parent.Parent.GlobalTestInitializations)
{
await InvokeGlobalInitializeMethodAsync(method, timeoutInfo, timeoutTokenSource).ConfigureAwait(false);
}
// TODO remove dry violation with TestMethodRunner
bool setTestContextSucessful = false;
if (_executionContext is null)
{
_classInstance = CreateTestClassInstance();
setTestContextSucessful = _classInstance != null && SetTestContext(_classInstance, result);
}
else
{
// The whole ExecuteInternalAsync method is already running on the execution context we got after class init.
// However, after we run global test initialize, it will need to capture the execution context (after it has finished).
// This is the case when executionContext is not null (this code path).
// In this case, we want to ensure the constructor and setting TestContext are both run on the correct execution context.
// Also we re-capture the execution context in case constructor or TestContext setter modifies an async local value.
ExecutionContextHelpers.RunOnContext(_executionContext, () =>
{
try
{
_classInstance = CreateTestClassInstance();
setTestContextSucessful = _classInstance != null && SetTestContext(_classInstance, result);
}
finally
{
_executionContext = ExecutionContext.Capture() ?? _executionContext;
#if NETFRAMEWORK
_hostContext = CallContext.HostContext;
#endif
}
});
}
if (setTestContextSucessful)
{
// For any failure after this point, we must run TestCleanup
_isTestContextSet = true;
if (await RunTestInitializeMethodAsync(_classInstance!, result, timeoutTokenSource).ConfigureAwait(false))
{
if (_executionContext is null)
{
Task? invokeResult = MethodInfo.GetInvokeResultAsync(_classInstance, arguments);
if (invokeResult is not null)
{
await invokeResult.ConfigureAwait(false);
}
}
else
{
var tcs = new TaskCompletionSource<object?>();
#pragma warning disable VSTHRD101 // Avoid unsupported async delegates
ExecutionContextHelpers.RunOnContext(_executionContext, async () =>
{
try
{
#if NETFRAMEWORK
CallContext.HostContext = _hostContext;
#endif
Task? invokeResult = MethodInfo.GetInvokeResultAsync(_classInstance, arguments);
if (invokeResult is not null)
{
await invokeResult.ConfigureAwait(false);
}
}
catch (Exception e)
{
tcs.SetException(e);
}
finally
{
_executionContext = ExecutionContext.Capture() ?? _executionContext;
#if NETFRAMEWORK
_hostContext = CallContext.HostContext;
#endif
tcs.TrySetResult(null);
}
});
#pragma warning restore VSTHRD101 // Avoid unsupported async delegates
await tcs.Task.ConfigureAwait(false);
}
result.Outcome = UnitTestOutcome.Passed;
}
}
}
catch (Exception ex)
{
Exception realException = GetRealException(ex);
if (realException.IsOperationCanceledExceptionFromToken(TestContext!.Context.CancellationTokenSource.Token))
{
result.Outcome = UnitTestOutcome.Timeout;
result.TestFailureException = new TestFailedException(
UnitTestOutcome.Timeout,
timeoutTokenSource?.Token.IsCancellationRequested == true
? string.Format(
CultureInfo.InvariantCulture,
Resource.Execution_Test_Timeout,
TestMethodName,
TimeoutInfo.Timeout)
: string.Format(
CultureInfo.InvariantCulture,
Resource.Execution_Test_Cancelled,
TestMethodName));
}
else
{
// This block should not throw. If it needs to throw, then handling of
// ThreadAbortException will need to be revisited. See comment in RunTestMethod.
result.TestFailureException ??= HandleMethodException(ex, realException, TestClassName, TestMethodName);
}
if (result.Outcome != UnitTestOutcome.Passed)
{
result.Outcome = ex is AssertInconclusiveException || ex.InnerException is AssertInconclusiveException
? UnitTestOutcome.Inconclusive
: UnitTestOutcome.Failed;
}
}
}
catch (Exception exception)
{
testRunnerException = exception;
}
// Update TestContext with outcome and exception so it can be used in the cleanup logic.
if (TestContext is { } testContext)
{
testContext.SetOutcome(result.Outcome);
// Uwnrap the exception if it's a TestFailedException
Exception? realException = result.TestFailureException is TestFailedException
? result.TestFailureException.InnerException
: result.TestFailureException;
testContext.SetException(realException);
}
// TestCleanup can potentially be a long running operation which shouldn't ideally be in a finally block.
// Pulling it out so extension writers can abort custom cleanups if need be. Having this in a finally block
// does not allow a thread abort exception to be raised within the block but throws one after finally is executed
// crashing the process. This was blocking writing an extension for Dynamic Timeout in VSO.
await RunTestCleanupMethodAsync(result, timeoutTokenSource).ConfigureAwait(false);
return testRunnerException != null ? throw testRunnerException : result;
}
private static Exception GetRealException(Exception ex)
{
if (ex is TargetInvocationException)
{
DebugEx.Assert(ex.InnerException != null, "Inner exception of TargetInvocationException is null. This should occur because we should have caught this case above.");
// Our reflected call will typically always get back a TargetInvocationException
// containing the real exception thrown by the test method as its inner exception
return ex.InnerException;
}
else
{
return ex;
}
}
/// <summary>
/// Handles the exception that is thrown by a test method. The exception can either
/// be expected or not expected.
/// </summary>
/// <param name="ex">Exception that was thrown.</param>
/// <param name="realException">Real exception thrown by the test method.</param>
/// <param name="className">The class name.</param>
/// <param name="methodName">The method name.</param>
/// <returns>Test framework exception with details.</returns>
private TestFailedException HandleMethodException(Exception ex, Exception realException, string className, string methodName)
{
DebugEx.Assert(ex != null, "exception should not be null.");
string errorMessage;
if (ex is TargetInvocationException && ex.InnerException == null)
{
errorMessage = string.Format(CultureInfo.CurrentCulture, Resource.UTA_FailedToGetTestMethodException, className, methodName);
return new TestFailedException(UnitTestOutcome.Error, errorMessage);
}
if (ex is TestFailedException testFailedException)
{
return testFailedException;
}
// If we are in hot reload context and the exception is a MissingMethodException and the first line of the stack
// trace contains the method name then it's likely that the current method was removed and the test is failing.
// For cases where the content of the test would throw a MissingMethodException, the first line of the stack trace
// would not be the test method name, so we can safely assume this is a proper test failure.
if (ex is MissingMethodException missingMethodException
&& RuntimeContext.IsHotReloadEnabled
&& missingMethodException.StackTrace?.IndexOf(Environment.NewLine, StringComparison.Ordinal) is { } lineReturnIndex
&& lineReturnIndex >= 0
#pragma warning disable IDE0057 // Use range operator
&& missingMethodException.StackTrace.Substring(0, lineReturnIndex).Contains($"{className}.{methodName}"))
#pragma warning restore IDE0057 // Use range operator
{
return new TestFailedException(UnitTestOutcome.NotFound, missingMethodException.Message, missingMethodException);
}
// Get the real exception thrown by the test method
if (realException.TryGetUnitTestAssertException(out UnitTestOutcome outcome, out string? exceptionMessage, out StackTraceInformation? exceptionStackTraceInfo))
{
return new TestFailedException(outcome, exceptionMessage, exceptionStackTraceInfo, realException);
}
errorMessage = _classInstance is null
? string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_InstanceCreationError,
TestClassName,
realException.GetFormattedExceptionMessage())
: string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_TestMethodThrows,
className,
methodName,
realException.GetFormattedExceptionMessage());
// Handle special case of UI objects in TestMethod to suggest UITestMethod
if (realException.HResult == -2147417842)
{
errorMessage = string.Format(CultureInfo.CurrentCulture, Resource.UTA_WrongThread, errorMessage);
}
StackTraceInformation? stackTrace = null;
// For ThreadAbortException (that can be thrown only by aborting a thread as there's no public constructor)
// there's no inner exception and exception itself contains reflection-related stack trace
// (_RuntimeMethodHandle.InvokeMethodFast <- _RuntimeMethodHandle.Invoke <- UnitTestExecuter.RunTestMethod)
// which has no meaningful info for the user. Thus, we do not show call stack for ThreadAbortException.
if (realException.GetType().Name != "ThreadAbortException")
{
stackTrace = realException.GetStackTraceInformation();
}
return new TestFailedException(UnitTestOutcome.Failed, errorMessage, stackTrace, realException);
}
/// <summary>
/// Runs TestCleanup methods of parent TestClass and base classes.
/// </summary>
/// <param name="result">Instance of TestResult.</param>
/// <param name="timeoutTokenSource">The timeout token source.</param>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle all kinds of user exceptions and message appropriately.")]
private async SynchronizationContextPreservingTask RunTestCleanupMethodAsync(TestResult result, CancellationTokenSource? timeoutTokenSource)
{
DebugEx.Assert(result != null, "result != null");
if (_classInstance is null || !_isTestContextSet || _isTestCleanupInvoked ||
// Fast check to see if we can return early.
// This avoids the code below that allocates CancellationTokenSource
!HasCleanupsToInvoke())
{
return;
}
_isTestCleanupInvoked = true;
MethodInfo? testCleanupMethod = Parent.TestCleanupMethod;
Exception? testCleanupException;
try
{
try
{
// Reset the cancellation token source to avoid cancellation of cleanup methods because of the init or test method cancellation.
TestContext.Context.CancellationTokenSource = new CancellationTokenSource();
// If we are running with a method timeout, we need to cancel the cleanup when the overall timeout expires. If it already expired, nothing to do.
if (timeoutTokenSource is { IsCancellationRequested: false })
{
timeoutTokenSource?.Token.Register(TestContext.Context.CancellationTokenSource.Cancel);
}
// Test cleanups are called in the order of discovery
// Current TestClass -> Parent -> Grandparent
testCleanupException = testCleanupMethod is not null
? await InvokeCleanupMethodAsync(testCleanupMethod, _classInstance, timeoutTokenSource).ConfigureAwait(false)
: null;
var baseTestCleanupQueue = new Queue<MethodInfo>(Parent.BaseTestCleanupMethodsQueue);
while (baseTestCleanupQueue.Count > 0 && testCleanupException is null)
{
testCleanupMethod = baseTestCleanupQueue.Dequeue();
testCleanupException = await InvokeCleanupMethodAsync(testCleanupMethod, _classInstance, timeoutTokenSource).ConfigureAwait(false);
}
}
finally
{
#if NET6_0_OR_GREATER
if (_classInstance is IAsyncDisposable classInstanceAsAsyncDisposable)
{
// If you implement IAsyncDisposable without calling the DisposeAsync this would result a resource leak.
await classInstanceAsAsyncDisposable.DisposeAsync().ConfigureAwait(false);
}
#endif
if (_classInstance is IDisposable classInstanceAsDisposable)
{
classInstanceAsDisposable.Dispose();
}
foreach ((MethodInfo method, TimeoutInfo? timeoutInfo) in Parent.Parent.GlobalTestCleanups)
{
await InvokeGlobalCleanupMethodAsync(method, timeoutInfo, timeoutTokenSource).ConfigureAwait(false);
}
}
}
catch (Exception ex)
{
testCleanupException = ex;
}
// If testCleanup was successful, then don't do anything
if (testCleanupException == null)
{
return;
}
Exception realException = testCleanupException.GetRealException();
UnitTestOutcome outcomeFromRealException = realException is AssertInconclusiveException ? UnitTestOutcome.Inconclusive : UnitTestOutcome.Failed;
result.Outcome = result.Outcome.GetMoreImportantOutcome(outcomeFromRealException);
realException = testCleanupMethod != null
? new TestFailedException(
outcomeFromRealException,
string.Format(CultureInfo.CurrentCulture, Resource.UTA_CleanupMethodThrows, TestClassName, testCleanupMethod.Name, realException.GetFormattedExceptionMessage()),
realException.TryGetStackTraceInformation(),
realException)
: new TestFailedException(
outcomeFromRealException,
string.Format(CultureInfo.CurrentCulture, Resource.UTA_CleanupMethodThrowsGeneralError, TestClassName, realException.GetFormattedExceptionMessage()),
realException.TryGetStackTraceInformation(),
realException);
result.TestFailureException = realException;
}
private bool HasCleanupsToInvoke() =>
Parent.TestCleanupMethod is not null ||
Parent.BaseTestCleanupMethodsQueue is { Count: > 0 } ||
_classInstance is IDisposable ||
#if NET6_0_OR_GREATER
_classInstance is IAsyncDisposable ||
#endif
Parent.Parent.GlobalTestCleanups is { Count: > 0 };
/// <summary>
/// Runs TestInitialize methods of parent TestClass and the base classes.
/// </summary>
/// <param name="classInstance">Instance of TestClass.</param>
/// <param name="result">Instance of TestResult.</param>
/// <param name="timeoutTokenSource">The timeout token source.</param>
/// <returns>True if the TestInitialize method(s) did not throw an exception.</returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle all kinds of user exceptions and message appropriately.")]
private async SynchronizationContextPreservingTask<bool> RunTestInitializeMethodAsync(object classInstance, TestResult result, CancellationTokenSource? timeoutTokenSource)
{
DebugEx.Assert(classInstance != null, "classInstance != null");
DebugEx.Assert(result != null, "result != null");
MethodInfo? testInitializeMethod = null;
Exception? testInitializeException = null;
try
{
// TestInitialize methods for base classes are called in reverse order of discovery
// Grandparent -> Parent -> Child TestClass
var baseTestInitializeStack = new Stack<MethodInfo>(Parent.BaseTestInitializeMethodsQueue);
while (baseTestInitializeStack.Count > 0)
{
testInitializeMethod = baseTestInitializeStack.Pop();
testInitializeException = testInitializeMethod is not null
? await InvokeInitializeMethodAsync(testInitializeMethod, classInstance, timeoutTokenSource).ConfigureAwait(false)
: null;
if (testInitializeException is not null)
{
break;
}
}
if (testInitializeException == null)
{
testInitializeMethod = Parent.TestInitializeMethod;
testInitializeException = testInitializeMethod is not null
? await InvokeInitializeMethodAsync(testInitializeMethod, classInstance, timeoutTokenSource).ConfigureAwait(false)
: null;
}
}
catch (Exception ex)
{
testInitializeException = ex;
}
// If testInitialization was successful, then don't do anything
if (testInitializeException == null)
{
return true;
}
// If the exception is already a `TestFailedException` we throw it as-is
if (testInitializeException is TestFailedException tfe)
{
result.Outcome = tfe.Outcome;
result.TestFailureException = testInitializeException;
return false;
}
Exception realException = testInitializeException.GetRealException();
// Prefix the exception message with the exception type name as prefix when exception is not assert exception.
string exceptionMessage = realException is UnitTestAssertException
? realException.TryGetMessage()
: realException.GetFormattedExceptionMessage();
string errorMessage = string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_InitMethodThrows,
TestClassName,
testInitializeMethod?.Name,
exceptionMessage);
StackTraceInformation? stackTrace = realException.GetStackTraceInformation();
result.Outcome = realException is AssertInconclusiveException
? UnitTestOutcome.Inconclusive
: UnitTestOutcome.Failed;
result.TestFailureException = new TestFailedException(
result.Outcome,
errorMessage,
stackTrace,
realException);
return false;
}
private async SynchronizationContextPreservingTask<TestFailedException?> InvokeInitializeMethodAsync(MethodInfo methodInfo, object classInstance, CancellationTokenSource? timeoutTokenSource)
{
TimeoutInfo? timeout = null;
if (Parent.TestInitializeMethodTimeoutMilliseconds.TryGetValue(methodInfo, out TimeoutInfo localTimeout))
{
timeout = localTimeout;
}
TestFailedException? result = await FixtureMethodRunner.RunWithTimeoutAndCancellationAsync(
async () =>
{
#if NETFRAMEWORK
CallContext.HostContext = _hostContext;
#endif
Task? task = methodInfo.GetInvokeResultAsync(classInstance, null);
if (task is not null)
{
await task.ConfigureAwait(false);
}
CaptureExecutionContextAfterFixtureIfNeeded(timeout);
#if NETFRAMEWORK
_hostContext = CallContext.HostContext;
#endif
},
TestContext.Context.CancellationTokenSource,
timeout,
methodInfo,
_executionContext,
Resource.TestInitializeWasCancelled,
Resource.TestInitializeTimedOut,
timeoutTokenSource is null
? null
: (timeoutTokenSource, TimeoutInfo.Timeout)).ConfigureAwait(false);
return result;
}
private async SynchronizationContextPreservingTask<TestFailedException?> InvokeGlobalInitializeMethodAsync(MethodInfo methodInfo, TimeoutInfo? timeoutInfo, CancellationTokenSource? timeoutTokenSource)
{
TestFailedException? result = await FixtureMethodRunner.RunWithTimeoutAndCancellationAsync(
async () =>
{
#if NETFRAMEWORK
CallContext.HostContext = _hostContext;
#endif
Task? task = methodInfo.GetInvokeResultAsync(null, [TestContext]);
if (task is not null)
{
await task.ConfigureAwait(false);
}
CaptureExecutionContextAfterFixtureIfNeeded(timeoutInfo);
#if NETFRAMEWORK
_hostContext = CallContext.HostContext;
#endif
},
TestContext.Context.CancellationTokenSource,
timeoutInfo: timeoutInfo,
methodInfo,
_executionContext,
Resource.TestInitializeWasCancelled,
Resource.TestInitializeTimedOut,
timeoutTokenSource is null
? null
: (timeoutTokenSource, TimeoutInfo.Timeout)).ConfigureAwait(false);
return result;
}
private async SynchronizationContextPreservingTask<TestFailedException?> InvokeCleanupMethodAsync(MethodInfo methodInfo, object classInstance, CancellationTokenSource? timeoutTokenSource)
{
TimeoutInfo? timeout = null;
if (Parent.TestCleanupMethodTimeoutMilliseconds.TryGetValue(methodInfo, out TimeoutInfo localTimeout))
{
timeout = localTimeout;
}
TestFailedException? result = await FixtureMethodRunner.RunWithTimeoutAndCancellationAsync(
async () =>
{
#if NETFRAMEWORK
CallContext.HostContext = _hostContext;
#endif
Task? task = methodInfo.GetInvokeResultAsync(classInstance, null);
if (task is not null)
{
await task.ConfigureAwait(false);
}
CaptureExecutionContextAfterFixtureIfNeeded(timeout);
#if NETFRAMEWORK
_hostContext = CallContext.HostContext;
#endif
},
TestContext.Context.CancellationTokenSource,
timeout,
methodInfo,
_executionContext,
Resource.TestCleanupWasCancelled,
Resource.TestCleanupTimedOut,
timeoutTokenSource is null
? null
: (timeoutTokenSource, TimeoutInfo.Timeout)).ConfigureAwait(false);
return result;
}
private async SynchronizationContextPreservingTask<TestFailedException?> InvokeGlobalCleanupMethodAsync(MethodInfo methodInfo, TimeoutInfo? timeoutInfo, CancellationTokenSource? timeoutTokenSource)
{
TestFailedException? result = await FixtureMethodRunner.RunWithTimeoutAndCancellationAsync(
async () =>
{
#if NETFRAMEWORK
CallContext.HostContext = _hostContext;
#endif
Task? task = methodInfo.GetInvokeResultAsync(null, [TestContext]);
if (task is not null)
{
await task.ConfigureAwait(false);
}
CaptureExecutionContextAfterFixtureIfNeeded(timeoutInfo);
#if NETFRAMEWORK
_hostContext = CallContext.HostContext;
#endif
},
TestContext.Context.CancellationTokenSource,
timeoutInfo: timeoutInfo,
methodInfo,
_executionContext,
Resource.TestCleanupWasCancelled,
Resource.TestCleanupTimedOut,
timeoutTokenSource is null
? null
: (timeoutTokenSource, TimeoutInfo.Timeout)).ConfigureAwait(false);
return result;
}
private void CaptureExecutionContextAfterFixtureIfNeeded(TimeoutInfo? timeoutInfo)
{
// After we execute a global test initialize, test initialize, test cleanup, or global test cleanup, we
// might need to capture the execution context.
// Generally, we do so only if the method has a timeout and that timeout is non-cooperative.
// For all other cases, we already use a custom task that preserves synchronization and execution contexts.
// NOTE: it seems that in .NET Framework, the synchronization context is part of the execution context.
// However, this doesn't appear to be the case in .NET (Core) where the synchronization context is strictly tied to current thread.
// In addition, if execution context was captured before (due to use of non-cooperative timeout in a previously run fixture), we still capture here again.
if (timeoutInfo?.CooperativeCancellation == false || _executionContext is not null)
{
_executionContext = ExecutionContext.Capture() ?? _executionContext;
}
}
/// <summary>
/// Sets the <see cref="TestContext"/> on <paramref name="classInstance"/>.
/// </summary>
/// <param name="classInstance">
/// Reference to instance of TestClass.
/// </param>
/// <param name="result">
/// Reference to instance of <see cref="TestResult"/>.
/// </param>
/// <returns>
/// True if there no exceptions during set context operation.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle all kinds of user exceptions and message appropriately.")]
private bool SetTestContext(object classInstance, TestResult result)
{
DebugEx.Assert(classInstance != null, "classInstance != null");
DebugEx.Assert(result != null, "result != null");
try
{
if (Parent.TestContextProperty != null && Parent.TestContextProperty.CanWrite)
{
Parent.TestContextProperty.SetValue(classInstance, TestContext);
}
return true;
}
catch (Exception ex)
{
Exception realException = ex.GetRealException();
string errorMessage = string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_TestContextSetError,
TestClassName,
realException.GetFormattedExceptionMessage());
result.Outcome = UnitTestOutcome.Failed;
StackTraceInformation? stackTraceInfo = realException.GetStackTraceInformation();
result.TestFailureException = new TestFailedException(UnitTestOutcome.Failed, errorMessage, stackTraceInfo);
}
return false;
}
/// <summary>
/// Creates an instance of TestClass. The TestMethod is invoked on this instance.
/// </summary>
/// <returns>
/// An instance of the TestClass.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle all kinds of user exceptions and message appropriately.")]
private object? CreateTestClassInstance()
=> Parent.Constructor.Invoke(Parent.IsParameterlessConstructor ? null : [TestContext]);
/// <summary>