-
Notifications
You must be signed in to change notification settings - Fork 759
/
XunitTestFrameworkDiscoverer.cs
173 lines (150 loc) · 8.92 KB
/
XunitTestFrameworkDiscoverer.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Xunit.Abstractions;
namespace Xunit.Sdk
{
/// <summary>
/// The implementation of <see cref="ITestFrameworkDiscoverer"/> that supports discovery
/// of unit tests linked against xunit.core.dll, using xunit.execution.dll.
/// </summary>
public class XunitTestFrameworkDiscoverer : TestFrameworkDiscoverer
{
static Type XunitTestCaseType = typeof(XunitTestCase);
/// <summary>
/// Gets the display name of the xUnit.net v2 test framework.
/// </summary>
public static readonly string DisplayName = string.Format(CultureInfo.InvariantCulture, "xUnit.net {0}", new object[] { typeof(XunitTestFrameworkDiscoverer).GetTypeInfo().Assembly.GetName().Version });
/// <summary>
/// Initializes a new instance of the <see cref="XunitTestFrameworkDiscoverer"/> class.
/// </summary>
/// <param name="assemblyInfo">The test assembly.</param>
/// <param name="sourceProvider">The source information provider.</param>
/// <param name="diagnosticMessageSink">The message sink used to send diagnostic messages</param>
/// <param name="collectionFactory">The test collection factory used to look up test collections.</param>
public XunitTestFrameworkDiscoverer(IAssemblyInfo assemblyInfo,
ISourceInformationProvider sourceProvider,
IMessageSink diagnosticMessageSink,
IXunitTestCollectionFactory collectionFactory = null)
: base(assemblyInfo, sourceProvider, diagnosticMessageSink)
{
var collectionBehaviorAttribute = assemblyInfo.GetCustomAttributes(typeof(CollectionBehaviorAttribute)).SingleOrDefault();
var disableParallelization = collectionBehaviorAttribute != null && collectionBehaviorAttribute.GetNamedArgument<bool>("DisableTestParallelization");
string config = null;
#if NETFRAMEWORK
config = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
#endif
var testAssembly = new TestAssembly(assemblyInfo, config);
TestCollectionFactory = collectionFactory ?? ExtensibilityPointFactory.GetXunitTestCollectionFactory(diagnosticMessageSink, collectionBehaviorAttribute, testAssembly);
TestFrameworkDisplayName = $"{DisplayName} [{TestCollectionFactory.DisplayName}, {(disableParallelization ? "non-parallel" : "parallel")}]";
}
/// <summary>
/// Gets the mapping dictionary of fact attribute type to discoverer type.
/// </summary>
protected Dictionary<Type, Type> DiscovererTypeCache { get; } = new Dictionary<Type, Type>(); // key is a Type that is or derives from FactAttribute
/// <summary>
/// Gets the test collection factory that makes test collections.
/// </summary>
public IXunitTestCollectionFactory TestCollectionFactory { get; private set; }
/// <inheritdoc/>
protected internal override ITestClass CreateTestClass(ITypeInfo @class)
{
return new TestClass(TestCollectionFactory.Get(@class), @class);
}
internal ITestClass CreateTestClass(ITypeInfo @class, Guid testCollectionUniqueId)
{
// This method is called for special fact deserialization, to ensure that the test collection unique
// ID lines up with the ones that will be deserialized through normal mechanisms.
var discoveredTestCollection = TestCollectionFactory.Get(@class);
var testCollection = new TestCollection(discoveredTestCollection.TestAssembly, discoveredTestCollection.CollectionDefinition, discoveredTestCollection.DisplayName, testCollectionUniqueId);
return new TestClass(testCollection, @class);
}
/// <summary>
/// Finds the tests on a test method.
/// </summary>
/// <param name="testMethod">The test method.</param>
/// <param name="includeSourceInformation">Set to <c>true</c> to indicate that source information should be included.</param>
/// <param name="messageBus">The message bus to report discovery messages to.</param>
/// <param name="discoveryOptions">The options used by the test framework during discovery.</param>
/// <returns>Return <c>true</c> to continue test discovery, <c>false</c>, otherwise.</returns>
protected internal virtual bool FindTestsForMethod(ITestMethod testMethod, bool includeSourceInformation, IMessageBus messageBus, ITestFrameworkDiscoveryOptions discoveryOptions)
{
var factAttributes = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).CastOrToList();
if (factAttributes.Count > 1)
{
var message = $"Test method '{testMethod.TestClass.Class.Name}.{testMethod.Method.Name}' has multiple [Fact]-derived attributes";
var testCase = new ExecutionErrorTestCase(DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None, testMethod, message);
return ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus);
}
var factAttribute = factAttributes.FirstOrDefault();
if (factAttribute == null)
return true;
var factAttributeType = (factAttribute as IReflectionAttributeInfo)?.Attribute.GetType();
Type discovererType = null;
if (factAttributeType == null || !DiscovererTypeCache.TryGetValue(factAttributeType, out discovererType))
{
var testCaseDiscovererAttribute = factAttribute.GetCustomAttributes(typeof(XunitTestCaseDiscovererAttribute)).FirstOrDefault();
if (testCaseDiscovererAttribute != null)
{
var args = testCaseDiscovererAttribute.GetConstructorArguments().Cast<string>().ToList();
discovererType = SerializationHelper.GetType(args[1], args[0]);
}
if (factAttributeType != null)
DiscovererTypeCache[factAttributeType] = discovererType;
}
if (discovererType == null)
return true;
var discoverer = GetDiscoverer(discovererType);
if (discoverer == null)
return true;
foreach (var testCase in discoverer.Discover(discoveryOptions, testMethod, factAttribute))
if (!ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus))
return false;
return true;
}
/// <inheritdoc/>
protected override bool FindTestsForType(ITestClass testClass, bool includeSourceInformation, IMessageBus messageBus, ITestFrameworkDiscoveryOptions discoveryOptions)
{
foreach (var method in testClass.Class.GetMethods(true))
{
var testMethod = new TestMethod(testClass, method);
if (!FindTestsForMethod(testMethod, includeSourceInformation, messageBus, discoveryOptions))
return false;
}
return true;
}
/// <summary>
/// Gets the test case discover instance for the given discoverer type. The instances are cached
/// and reused, since they should not be stateful.
/// </summary>
/// <param name="discovererType">The discoverer type.</param>
/// <returns>Returns the test case discoverer instance.</returns>
protected IXunitTestCaseDiscoverer GetDiscoverer(Type discovererType)
{
try
{
return ExtensibilityPointFactory.GetXunitTestCaseDiscoverer(DiagnosticMessageSink, discovererType);
}
catch (Exception ex)
{
DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Discoverer type '{discovererType.FullName}' could not be created or does not implement IXunitTestCaseDiscoverer: {ex.Unwrap()}"));
return null;
}
}
/// <inheritdoc/>
public override string Serialize(ITestCase testCase)
{
if (testCase.GetType() == XunitTestCaseType)
{
var xunitTestCase = (XunitTestCase)testCase;
var className = testCase.TestMethod?.TestClass?.Class?.Name;
var methodName = testCase.TestMethod?.Method?.Name;
if (className != null && methodName != null && (xunitTestCase.TestMethodArguments == null || xunitTestCase.TestMethodArguments.Length == 0))
return $":F:{className.Replace(":", "::")}:{methodName.Replace(":", "::")}:{(int)xunitTestCase.DefaultMethodDisplay}:{(int)xunitTestCase.DefaultMethodDisplayOptions}:{testCase.TestMethod.TestClass.TestCollection.UniqueID.ToString("N")}";
}
return base.Serialize(testCase);
}
}
}