-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathNUnitFrameworkDriver.cs
More file actions
250 lines (216 loc) · 10.7 KB
/
Copy pathNUnitFrameworkDriver.cs
File metadata and controls
250 lines (216 loc) · 10.7 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
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
using NUnit.Engine.Extensibility;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
#if NETCOREAPP3_1_OR_GREATER
using NUnit.Engine.Internal;
#endif
namespace NUnit.Engine.Drivers
{
/// <summary>
/// NUnitFrameworkDriver is used by the test-runner to load and run
/// tests using the NUnit framework assembly, versions 3 and up.
/// </summary>
public class NUnitFrameworkDriver : IFrameworkDriver
{
private static readonly Version MINIMUM_NUNIT_VERSION = new(3, 2, 0);
private static readonly Logger log = InternalTrace.GetLogger(nameof(NUnitFrameworkDriver));
private readonly Version _nunitVersion;
#if NETFRAMEWORK
private readonly NUnitFrameworkApi _api;
/// <summary>
/// Construct an NUnitFrameworkDriver
/// </summary>
/// <param name="testDomain">The application domain in which to create the FrameworkController</param>
/// <param name="nunitRef">An AssemblyName referring to the test framework.</param>
public NUnitFrameworkDriver(AppDomain testDomain, string id, AssemblyName nunitRef)
{
Guard.ArgumentNotNull(testDomain);
Guard.ArgumentNotNullOrEmpty(id);
Guard.ArgumentNotNull(nunitRef);
ID = id;
_nunitVersion = nunitRef.Version.ShouldNotBeNull();
if (nunitRef.Version >= MINIMUM_NUNIT_VERSION)
{
API = "2018";
_api = (NUnitFrameworkApi)testDomain.CreateInstanceFromAndUnwrap(
Assembly.GetExecutingAssembly().Location,
"NUnit.Engine.Drivers.NUnitFrameworkApi2018",
false,
0,
null,
new object[] { ID, nunitRef },
null,
null).ShouldNotBeNull();
}
else
{
API = "2009";
_api = new NUnitFrameworkApi2009(testDomain, ID, nunitRef);
}
}
/// <summary>
/// Internal generic constructor used by our tests.
/// </summary>
/// <param name="testDomain">The application domain in which to create the FrameworkController</param>
/// <param name="nunitRef">An AssemblyName referring to the test framework.</param>
internal NUnitFrameworkDriver(AppDomain testDomain, string api, string id, AssemblyName nunitRef)
{
Guard.ArgumentNotNull(testDomain);
Guard.ArgumentNotNull(api);
Guard.ArgumentValid(api == "2009" || api == "2018", $"Invalid API specified: {api}", nameof(api));
Guard.ArgumentNotNullOrEmpty(id);
Guard.ArgumentNotNull(nunitRef);
_nunitVersion = nunitRef.Version.ShouldNotBeNull();
ID = id;
API = api;
_api = api == "2018"
? (NUnitFrameworkApi)testDomain.CreateInstanceFromAndUnwrap(
Assembly.GetExecutingAssembly().Location,
typeof(NUnitFrameworkApi2018).FullName!,
false,
0,
null,
new object[] { ID, nunitRef },
null,
null).ShouldNotBeNull()
: new NUnitFrameworkApi2009(testDomain, ID, nunitRef);
}
#else
private readonly NUnitFrameworkApi2018 _api;
/// <summary>
/// Construct an NUnitFrameworkDriver
/// </summary>
/// <param name="reference">An AssemblyName referring to the test framework.</param>
public NUnitFrameworkDriver(string id, AssemblyName nunitRef)
{
Guard.ArgumentNotNullOrEmpty(id);
Guard.ArgumentNotNull(nunitRef);
ID = id;
API = "2018";
_nunitVersion = nunitRef.Version.ShouldNotBeNull();
_api = new NUnitFrameworkApi2018(ID, nunitRef);
}
internal List<ResolutionStrategy>? ResolutionStrategies => _api?.ResolutionStrategies;
#endif
/// <summary>
/// String naming the API in use, for use by tests
/// </summary>
internal string API { get; } = string.Empty;
/// <summary>
/// An id prefix that will be passed to the test framework and used as part of the
/// test ids created.
/// </summary>
public string ID { get; }
/// <summary>
/// Loads the tests in an assembly.
/// </summary>
/// <param name="testAssemblyPath">The path to the test assembly</param>
/// <param name="settings">The test settings</param>
/// <returns>An XML string representing the loaded test</returns>
public string Load(string testAssemblyPath, IDictionary<string, object> settings)
=> _api.Load(testAssemblyPath, settings);
/// <summary>
/// Counts the number of test cases for the loaded test assembly
/// </summary>
/// <param name="filter">The XML test filter</param>
/// <returns>The number of test cases</returns>
public int CountTestCases(string filter) => _api.CountTestCases(filter);
/// <summary>
/// Executes the tests in an assembly.
/// </summary>
/// <param name="listener">An ITestEventHandler that receives progress notices</param>
/// <param name="filter">A filter that controls which tests are executed</param>
/// <returns>An Xml string representing the result</returns>
public string Run(ITestEventListener? listener, string filter) =>
_api.Run(listener is not null ? new EventInterceptor(listener) : null, filter);
/// <summary>
/// Executes the tests in an assembly asynchronously.
/// </summary>
/// <param name="callback">A callback that receives XML progress notices</param>
/// <param name="filter">A filter that controls which tests are executed</param>
public void RunAsync(ITestEventListener? listener, string filter) =>
_api.RunAsync(listener is not null ? new Action<string>(listener.OnTestEvent) : null, filter);
/// <summary>
/// Cancel the ongoing test run. If no test is running, the call is ignored.
/// </summary>
public void RequestStop() => _api.RequestStop();
/// <summary>
/// Force the current test run to stop, killing threads or processes if necessary.
/// If no tests are running, the call is ignored.
/// </summary>
public void ForcedStop()
{
if (_api.ForcedStopSupported)
_api.ForcedStop();
else
Process.GetCurrentProcess().Kill();
}
/// <summary>
/// Returns information about the tests in an assembly.
/// </summary>
/// <param name="filter">A filter indicating which tests to include</param>
/// <returns>An Xml string representing the tests</returns>
public string Explore(string filter) => _api.Explore(filter);
/// <summary>
/// Nested class used to intercept progress reports received from the test
/// framework and resend them to the actual listener, normally located in
/// the runner that is using the engine.
/// </summary>
/// <remarks>
/// This class is absolutely needed when the 2018 NUnit API is used under
/// the .NET Framework using Windows Remoting as a communication protocol.
/// In particular, the MarshalByRef object implementing the listener must
/// be convertible to ITestEventListener via the IConvertible interface.
///
/// In other cases, the interceptor is not essential, but we use it anyway
/// for several reasons:
///
/// 1. We have no control over the implementation of runners using the engine.
/// They may not implement IConvertible and may not even derive from
/// MarshaByRefObject.
///
/// 2. The interceptor provides a point of control for checking what events
/// are received from the framework and for possible future modifications to
/// the events before they are forwarded.
/// </remarks>
public class EventInterceptor : MarshalByRefObject, ITestEventListener, IConvertible
{
private ITestEventListener _listener;
public EventInterceptor(ITestEventListener listener)
{
_listener = listener;
}
#region ITestEventListener and IConvertible Implementations
void ITestEventListener.OnTestEvent(string report)
{
_listener.OnTestEvent(report);
}
// Conversion to ITestEventListener is the only one that makes sense
object IConvertible.ToType(Type conversionType, IFormatProvider? provider) =>
conversionType == typeof(ITestEventListener) ? this : InvalidCast(conversionType);
TypeCode IConvertible.GetTypeCode() => TypeCode.Object;
bool IConvertible.ToBoolean(IFormatProvider? provider) => InvalidCast<bool>();
char IConvertible.ToChar(IFormatProvider? provider) => InvalidCast<char>();
sbyte IConvertible.ToSByte(IFormatProvider? provider) => InvalidCast<sbyte>();
byte IConvertible.ToByte(IFormatProvider? provider) => InvalidCast<byte>();
short IConvertible.ToInt16(IFormatProvider? provider) => InvalidCast<short>();
ushort IConvertible.ToUInt16(IFormatProvider? provider) => InvalidCast<ushort>();
int IConvertible.ToInt32(IFormatProvider? provider) => InvalidCast<int>();
uint IConvertible.ToUInt32(IFormatProvider? provider) => InvalidCast<uint>();
long IConvertible.ToInt64(IFormatProvider? provider) => InvalidCast<long>();
ulong IConvertible.ToUInt64(IFormatProvider? provider) => InvalidCast<ulong>();
float IConvertible.ToSingle(IFormatProvider? provider) => InvalidCast<float>();
double IConvertible.ToDouble(IFormatProvider? provider) => InvalidCast<double>();
decimal IConvertible.ToDecimal(IFormatProvider? provider) => InvalidCast<decimal>();
DateTime IConvertible.ToDateTime(IFormatProvider? provider) => InvalidCast<DateTime>();
string IConvertible.ToString(IFormatProvider? provider) => InvalidCast<string>();
private static T InvalidCast<T>() => (T)InvalidCast(typeof(T));
private static object InvalidCast(Type type) =>
throw new InvalidCastException($"{nameof(EventInterceptor)} is not convertible to {nameof(type)}");
#endregion
}
}
}