/
TestInvoker.cs
321 lines (283 loc) · 12.1 KB
/
TestInvoker.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
using System;
using System.Globalization;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Xunit.Internal;
using Xunit.Sdk;
namespace Xunit.v3;
/// <summary>
/// A base class that provides default behavior to invoke a test method. This includes
/// support for async test methods ("async Task" and "async ValueTask" for C#/VB,
/// and async functions in F#) as well as creation and disposal of the test class. This class
/// is designed to be a singleton for performance reasons.
/// </summary>
/// <typeparam name="TContext">The context type used by the invoker</typeparam>
public abstract class TestInvoker<TContext>
where TContext : TestInvokerContext
{
/// <summary>
/// This method is called just after the test method has finished executing.
/// This method should NEVER throw; any exceptions should be placed into the aggregator in <paramref name="ctxt"/>.
/// </summary>
/// <param name="ctxt">The context that describes the current test</param>
protected virtual ValueTask AfterTestMethodInvokedAsync(TContext ctxt) =>
default;
/// <summary>
/// This method is called just before the test method is invoked.
/// This method should NEVER throw; any exceptions should be placed into the aggregator in <paramref name="ctxt"/>.
/// </summary>
/// <param name="ctxt">The context that describes the current test</param>
protected virtual ValueTask BeforeTestMethodInvokedAsync(TContext ctxt) =>
default;
/// <summary>
/// This method calls the test method via <see cref="MethodBase.Invoke(object, object[])"/>. This is an available override
/// point if you need to do some other form of invocation of the actual test method.
/// </summary>
/// <param name="ctxt">The context that describes the current test</param>
/// <param name="testClassInstance">The test class instance</param>
/// <returns>The return value from the test method invocation</returns>
protected virtual object? CallTestMethod(
TContext ctxt,
object? testClassInstance)
{
Guard.ArgumentNotNull(ctxt);
return ctxt.TestMethod.Invoke(testClassInstance, ctxt.TestMethodArguments);
}
/// <summary>
/// Creates the test class, unless the test method is static or there have already been errors. Note that
/// this method times the creation of the test class (using <see cref="Timer"/>). It is also responsible for
/// sending the <see cref="_TestClassConstructionStarting"/> and <see cref="_TestClassConstructionFinished"/>
/// messages, so if you override this method without calling the base, you are responsible for all of this behavior.
/// This method should NEVER throw; any exceptions should be placed into the aggregator inside <paramref name="ctxt"/>.
/// To override just the behavior of creating the instance of the test class,
/// override <see cref="CreateTestClassInstance"/> instead.
/// </summary>
/// <param name="ctxt">The context that describes the current test</param>
/// <returns>A tuple which includes the class instance (<c>null</c> if one was not created) as well as the elapsed
/// time spent creating the class</returns>
protected virtual object? CreateTestClass(TContext ctxt)
{
Guard.ArgumentNotNull(ctxt);
return ctxt.Aggregator.Run(() =>
{
if (!ctxt.TestMethod.IsStatic && !ctxt.Aggregator.HasExceptions)
{
var testAssemblyUniqueID = ctxt.Test.TestCase.TestCollection.TestAssembly.UniqueID;
var testCollectionUniqueID = ctxt.Test.TestCase.TestCollection.UniqueID;
var testClassUniqueID = ctxt.Test.TestCase.TestClass?.UniqueID;
var testMethodUniqueID = ctxt.Test.TestCase.TestMethod?.UniqueID;
var testCaseUniqueID = ctxt.Test.TestCase.UniqueID;
var testUniqueID = ctxt.Test.UniqueID;
var testClassConstructionStarting = new _TestClassConstructionStarting
{
AssemblyUniqueID = testAssemblyUniqueID,
TestCaseUniqueID = testCaseUniqueID,
TestClassUniqueID = testClassUniqueID,
TestCollectionUniqueID = testCollectionUniqueID,
TestMethodUniqueID = testMethodUniqueID,
TestUniqueID = testUniqueID
};
if (!ctxt.MessageBus.QueueMessage(testClassConstructionStarting))
ctxt.CancellationTokenSource.Cancel();
else
{
try
{
if (!ctxt.CancellationTokenSource.IsCancellationRequested)
return ctxt.Aggregator.Run(() => CreateTestClassInstance(ctxt), null);
}
finally
{
var testClassConstructionFinished = new _TestClassConstructionFinished
{
AssemblyUniqueID = testAssemblyUniqueID,
TestCaseUniqueID = testCaseUniqueID,
TestClassUniqueID = testClassUniqueID,
TestCollectionUniqueID = testCollectionUniqueID,
TestMethodUniqueID = testMethodUniqueID,
TestUniqueID = testUniqueID
};
if (!ctxt.MessageBus.QueueMessage(testClassConstructionFinished))
ctxt.CancellationTokenSource.Cancel();
}
}
}
return null;
}, null);
}
/// <summary>
/// Creates the instance of the test class. By default, uses <see cref="Activator.CreateInstance(Type, object[])"/>
/// with the <see cref="TestInvokerContext.TestClass"/> and <see cref="TestInvokerContext.ConstructorArguments"/> values
/// from <paramref name="ctxt"/>. You should override this in order to change the input values and/or use a factory
/// method other than Activator.CreateInstance.
/// </summary>
/// <param name="ctxt">The context that describes the current test</param>
protected virtual object? CreateTestClassInstance(TContext ctxt)
{
Guard.ArgumentNotNull(ctxt);
return Activator.CreateInstance(ctxt.TestClass, ctxt.ConstructorArguments);
}
/// <summary>
/// Invokes the test method on the given test class instance. This method fast fails any test marked as "async void",
/// ensures that the test method has the correct number of arguments, then calls <see cref="CallTestMethod"/>
/// to do the actual method invocation. It ensure that any async test method is fully completed before returning, and
/// returns the measured clock time that the invocation took. This method should NEVER throw; any exceptions should be
/// placed into the aggregator in <paramref name="ctxt"/>.
/// </summary>
/// <param name="ctxt">The context that describes the current test</param>
/// <param name="testClassInstance">The test class instance</param>
/// <returns>Returns the amount of time the test took to run, in seconds</returns>
protected virtual async ValueTask<decimal> InvokeTestMethodAsync(
TContext ctxt,
object? testClassInstance)
{
Guard.ArgumentNotNull(ctxt);
if (AsyncUtility.IsAsyncVoid(ctxt.TestMethod))
{
ctxt.Aggregator.Add(new InvalidOperationException("Tests marked as 'async void' are no longer supported. Please convert to 'async Task' or 'async ValueTask'."));
return 0m;
}
var elapsed = await ExecutionTimer.MeasureAsync(
() => ctxt.Aggregator.RunAsync(
async () =>
{
var parameterCount = ctxt.TestMethod.GetParameters().Length;
var valueCount = ctxt.TestMethodArguments is null ? 0 : ctxt.TestMethodArguments.Length;
if (parameterCount != valueCount)
{
ctxt.Aggregator.Add(
new InvalidOperationException(
string.Format(
CultureInfo.CurrentCulture,
"The test method expected {0} parameter value{1}, but {2} parameter value{3} {4} provided.",
parameterCount,
parameterCount == 1 ? "" : "s",
valueCount,
valueCount == 1 ? "" : "s",
valueCount == 1 ? "was" : "were"
)
)
);
}
else
{
var logEnabled = TestEventSource.Log.IsEnabled();
if (logEnabled)
TestEventSource.Log.TestStart(ctxt.Test.TestDisplayName);
try
{
var result = CallTestMethod(ctxt, testClassInstance);
var valueTask = AsyncUtility.TryConvertToValueTask(result);
if (valueTask.HasValue)
await valueTask.Value;
}
finally
{
if (logEnabled)
TestEventSource.Log.TestStop(ctxt.Test.TestDisplayName);
}
}
}
)
);
return (decimal)elapsed.TotalSeconds;
}
/// <summary>
/// Creates the test class (if necessary), and invokes the test method. It is assumed the context is fresh, so it is
/// initialized and disposed in this method, so disposing it elsewhere is unnecessary.
/// </summary>
/// <param name="ctxt">The invoker context</param>
/// <returns>Returns the time (in seconds) spent creating the test class, running
/// the test, and disposing of the test class.</returns>
protected async ValueTask<decimal> RunAsync(TContext ctxt)
{
Guard.ArgumentNotNull(ctxt);
return await ctxt.Aggregator.RunAsync(async () =>
{
if (ctxt.CancellationTokenSource.IsCancellationRequested)
return 0m;
SetTestContext(ctxt, TestEngineStatus.Initializing);
object? testClassInstance = null;
var elapsedTime = ExecutionTimer.Measure(() => { testClassInstance = CreateTestClass(ctxt); });
var asyncDisposable = testClassInstance as IAsyncDisposable;
var disposable = testClassInstance as IDisposable;
var testAssemblyUniqueID = ctxt.Test.TestCase.TestCollection.TestAssembly.UniqueID;
var testCollectionUniqueID = ctxt.Test.TestCase.TestCollection.UniqueID;
var testClassUniqueID = ctxt.Test.TestCase.TestClass?.UniqueID;
var testMethodUniqueID = ctxt.Test.TestCase.TestMethod?.UniqueID;
var testCaseUniqueID = ctxt.Test.TestCase.UniqueID;
var testUniqueID = ctxt.Test.UniqueID;
try
{
if (testClassInstance is IAsyncLifetime asyncLifetime)
elapsedTime += await ExecutionTimer.MeasureAsync(asyncLifetime.InitializeAsync);
try
{
if (!ctxt.CancellationTokenSource.IsCancellationRequested)
{
elapsedTime += await ExecutionTimer.MeasureAsync(() => BeforeTestMethodInvokedAsync(ctxt));
SetTestContext(ctxt, TestEngineStatus.Running);
if (!ctxt.CancellationTokenSource.IsCancellationRequested && !ctxt.Aggregator.HasExceptions)
elapsedTime += TimeSpan.FromSeconds((double)await InvokeTestMethodAsync(ctxt, testClassInstance));
SetTestContext(ctxt, TestEngineStatus.CleaningUp, TestResultState.FromException((decimal)elapsedTime.TotalSeconds, ctxt.Aggregator.ToException()));
elapsedTime += await ExecutionTimer.MeasureAsync(() => AfterTestMethodInvokedAsync(ctxt));
}
}
finally
{
if (asyncDisposable is not null || disposable is not null)
{
var testClassDisposeStarting = new _TestClassDisposeStarting
{
AssemblyUniqueID = testAssemblyUniqueID,
TestCaseUniqueID = testCaseUniqueID,
TestClassUniqueID = testClassUniqueID,
TestCollectionUniqueID = testCollectionUniqueID,
TestMethodUniqueID = testMethodUniqueID,
TestUniqueID = testUniqueID
};
if (!ctxt.MessageBus.QueueMessage(testClassDisposeStarting))
ctxt.CancellationTokenSource.Cancel();
}
if (asyncDisposable is not null)
elapsedTime += await ExecutionTimer.MeasureAsync(() => ctxt.Aggregator.RunAsync(asyncDisposable.DisposeAsync));
}
}
finally
{
if (disposable is not null)
elapsedTime += ExecutionTimer.Measure(() => ctxt.Aggregator.Run(disposable.Dispose));
if (asyncDisposable is not null || disposable is not null)
{
var testClassDisposeFinished = new _TestClassDisposeFinished
{
AssemblyUniqueID = testAssemblyUniqueID,
TestCaseUniqueID = testCaseUniqueID,
TestClassUniqueID = testClassUniqueID,
TestCollectionUniqueID = testCollectionUniqueID,
TestMethodUniqueID = testMethodUniqueID,
TestUniqueID = testUniqueID
};
if (!ctxt.MessageBus.QueueMessage(testClassDisposeFinished))
ctxt.CancellationTokenSource.Cancel();
}
}
return (decimal)elapsedTime.TotalSeconds;
}, 0m);
}
/// <summary>
/// Sets the test context for the given test state and engine status.
/// </summary>
/// <param name="ctxt">The invoker context</param>
/// <param name="testStatus">The current engine status for the test</param>
/// <param name="testState">The current test state</param>
protected virtual void SetTestContext(
TContext ctxt,
TestEngineStatus testStatus,
TestResultState? testState = null)
{
Guard.ArgumentNotNull(ctxt);
TestContext.SetForTest(ctxt.Test, testStatus, ctxt.CancellationTokenSource.Token, testState);
}
}