This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathTestCaseDiscoverer.cs
111 lines (99 loc) · 4.34 KB
/
TestCaseDiscoverer.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.Quantum.Simulation.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit.Abstractions;
using Xunit.Sdk;
namespace Microsoft.Quantum.Simulation.XUnit
{
public class TestCaseDiscoverer : IXunitTestCaseDiscoverer
{
/// <summary>
/// Test case we use to report errors of the test case enumeration process.
/// </summary>
private TestCase SkipErrorTestCase(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, string message)
{
return new TestCase(
DiagnosticMessageSink,
discoveryOptions.MethodDisplay() ?? TestMethodDisplay.Method,
testMethod,
new TestOperation()
{
assemblyName = "",
className = "", //when class name is empty the parent test case will be shown, so user can see there is a problem
fullClassName = "",
testCaseNamePrefix = "",
skipReason = message
});
}
/// <summary>
/// Implementation of <see cref="IXunitTestCaseDiscoverer"/>
/// </summary>
public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
{
List<IXunitTestCase> result = new List<IXunitTestCase>();
string assemblyName = factAttribute.GetNamedArgument<string>("AssemblyName");
System.Reflection.Assembly testAssembly = null;
if (assemblyName == null)
{
testAssembly = testMethod.TestClass.Class.ToRuntimeType().Assembly;
}
else
{
try
{
testAssembly = System.Reflection.Assembly.Load(assemblyName);
}
catch (Exception e)
{
result.Add(SkipErrorTestCase(discoveryOptions, testMethod, "Assembly load issue: " + e.Message));
return result;
}
}
string testNamespace = factAttribute.GetNamedArgument<string>("TestNamespace") ?? testMethod.TestClass.Class.ToRuntimeType().Namespace;
if (testNamespace == null)
{
result.Add(SkipErrorTestCase(discoveryOptions, testMethod, "Could not find the namespase with tests."));
return result;
}
string Suffix = factAttribute.GetNamedArgument<string>("Suffix");
string skipReason = factAttribute.GetNamedArgument<string>("Skip");
string testCasePrefix = factAttribute.GetNamedArgument<string>("TestCasePrefix") ?? "";
IEnumerable<Type> ourTypes =
from definedType in testAssembly.DefinedTypes
where definedType.Name.EndsWith(Suffix)
where definedType.Namespace == testNamespace
where typeof(AbstractCallable).IsAssignableFrom(definedType)
where typeof(ICallable<QVoid, QVoid>).IsAssignableFrom(definedType)
select definedType;
foreach (Type operationType in ourTypes)
{
result.Add(
new TestCase(DiagnosticMessageSink, discoveryOptions.MethodDisplay() ?? TestMethodDisplay.Method, testMethod,
new TestOperation()
{
assemblyName = testAssembly.FullName,
className = operationType.Name,
fullClassName = operationType.FullName,
skipReason = skipReason,
testCaseNamePrefix = testCasePrefix
})
);
}
if (result.Count == 0)
{
result.Add(SkipErrorTestCase(discoveryOptions, testMethod, "No tests were found."));
}
return result;
}
protected IMessageSink DiagnosticMessageSink { get; }
public TestCaseDiscoverer(IMessageSink diagnosticMessageSink)
{
DiagnosticMessageSink = diagnosticMessageSink;
}
}
}