forked from allure-framework/allure-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllureXunitHelper.cs
246 lines (206 loc) · 9.6 KB
/
AllureXunitHelper.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Allure.Net.Commons;
using Allure.XUnit;
using Allure.Xunit.Attributes;
using Xunit.Abstractions;
using Xunit.Sdk;
namespace Allure.Xunit
{
public static class AllureXunitHelper
{
static AllureXunitHelper()
{
const string allureConfigEnvVariable = "ALLURE_CONFIG";
const string allureConfigName = "allureConfig.json";
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(allureConfigEnvVariable)))
{
return;
}
var allureConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, allureConfigName);
Environment.SetEnvironmentVariable(allureConfigEnvVariable, allureConfigPath);
}
public static void StartTestContainer(ITestCaseStarting testCaseStarting)
{
if (testCaseStarting.TestCase is not ITestResultAccessor testResults)
{
return;
}
StartTestContainer(testCaseStarting.TestClass, testResults);
}
public static void StartTestCase(ITestCaseMessage testCaseMessage)
{
if (testCaseMessage.TestCase is not ITestResultAccessor testResults)
{
return;
}
var testCase = testCaseMessage.TestCase;
var uuid = NewUuid(testCase.DisplayName);
testResults.TestResult = new()
{
uuid = uuid,
name = testCase.DisplayName,
historyId = testCase.DisplayName,
fullName = testCase.DisplayName,
labels = new()
{
Label.Thread(),
Label.Host(),
Label.TestClass(testCase.TestMethod.TestClass.Class.Name),
Label.TestMethod(testCase.DisplayName),
Label.Package(testCase.TestMethod.TestClass.Class.Name)
}
};
UpdateTestDataFromAttributes(testResults.TestResult, testCase);
AllureLifecycle.Instance.StartTestCase(testResults.TestResultContainer.uuid, testResults.TestResult);
}
public static void MarkTestCaseAsFailedOrBroken(ITestFailed testFailed)
{
if (testFailed.TestCase is not ITestResultAccessor testResults)
{
return;
}
var statusDetails = testResults.TestResult.statusDetails ??= new();
statusDetails.trace = string.Join('\n', testFailed.StackTraces);
statusDetails.message = string.Join('\n', testFailed.Messages);
if (testFailed.ExceptionTypes.Any(exceptionType => !exceptionType.StartsWith("Xunit.Sdk.")))
{
testResults.TestResult.status = Status.broken;
return;
}
testResults.TestResult.status = Status.failed;
}
public static void MarkTestCaseAsPassed(ITestPassed testPassed)
{
if (testPassed.TestCase is not ITestResultAccessor testResults)
{
return;
}
var statusDetails = testResults.TestResult.statusDetails ??= new();
statusDetails.message = testPassed.Output;
testResults.TestResult.status = Status.passed;
}
public static void MarkTestCaseAsSkipped(ITestCaseMessage testCaseMessage)
{
if (testCaseMessage.TestCase is not ITestResultAccessor testResults)
{
return;
}
var statusDetails = testResults.TestResult.statusDetails ??= new();
statusDetails.message = testCaseMessage.TestCase.SkipReason;
testResults.TestResult.status = Status.skipped;
}
public static void FinishTestCase(ITestCaseMessage testCaseMessage)
{
if (testCaseMessage.TestCase is not ITestResultAccessor testResults)
{
return;
}
AllureLifecycle.Instance.StopTestCase(testResults.TestResult.uuid);
AllureLifecycle.Instance.WriteTestCase(testResults.TestResult.uuid);
AllureLifecycle.Instance.StopTestContainer(testResults.TestResultContainer.uuid);
AllureLifecycle.Instance.WriteTestContainer(testResults.TestResultContainer.uuid);
}
private static void StartTestContainer(ITestClass testClass, ITestResultAccessor testResult)
{
var uuid = NewUuid(testClass.Class.Name);
testResult.TestResultContainer = new()
{
uuid = uuid,
name = testClass.Class.Name
};
AllureLifecycle.Instance.StartTestContainer(testResult.TestResultContainer);
}
private static string NewUuid(string name)
{
var uuid = string.Concat(Guid.NewGuid().ToString(), "-", name);
return uuid;
}
internal static void Log(string message)
{
AllureMessageBus.TestOutputHelper.Value.WriteLine("╬════════════════════════");
AllureMessageBus.TestOutputHelper.Value.WriteLine($"║ {message}");
AllureMessageBus.TestOutputHelper.Value.WriteLine("╬═══════════════");
}
private static void AddDistinct(this List<Label> labels, Label labelToInsert, bool overwrite)
{
if (overwrite)
{
labels.RemoveAll(label => label.name == labelToInsert.name);
}
labels.Add(labelToInsert);
}
private static void AddDistinct(this List<Label> labels, string labelName, string[] values, bool overwrite)
{
if (overwrite)
{
labels.RemoveAll(label => label.name == labelName);
}
foreach (var value in values)
{
labels.Add(new Label {name = labelName, value = value});
}
}
private static void UpdateTestDataFromAttributes(TestResult testResult, ITestCase testCase)
{
var classAttributes = testCase.TestMethod.TestClass.Class.GetCustomAttributes(typeof(IAllureInfo));
var methodAttributes = testCase.TestMethod.Method.GetCustomAttributes(typeof(IAllureInfo));
foreach (var attribute in classAttributes.Concat(methodAttributes))
{
switch (((IReflectionAttributeInfo) attribute).Attribute)
{
case AllureFeatureAttribute featureAttribute:
testResult.labels.AddDistinct("feature", featureAttribute.Features, featureAttribute.Overwrite);
break;
case AllureLinkAttribute linkAttribute:
testResult.links.Add(linkAttribute.Link);
break;
case AllureIssueAttribute issueAttribute:
testResult.links.Add(issueAttribute.IssueLink);
break;
case AllureOwnerAttribute ownerAttribute:
testResult.labels.AddDistinct(Label.Owner(ownerAttribute.Owner), ownerAttribute.Overwrite);
break;
case AllureSuiteAttribute suiteAttribute:
testResult.labels.AddDistinct(Label.Suite(suiteAttribute.Suite), suiteAttribute.Overwrite);
break;
case AllureSubSuiteAttribute subSuiteAttribute:
testResult.labels.AddDistinct(Label.SubSuite(subSuiteAttribute.SubSuite), subSuiteAttribute.Overwrite);
break;
case AllureEpicAttribute epicAttribute:
testResult.labels.AddDistinct(Label.Epic(epicAttribute.Epic), epicAttribute.Overwrite);
break;
case AllureTagAttribute tagAttribute:
testResult.labels.AddDistinct("tag", tagAttribute.Tags, tagAttribute.Overwrite);
break;
case AllureSeverityAttribute severityAttribute:
testResult.labels.AddDistinct(Label.Severity(severityAttribute.Severity), true);
break;
case AllureParentSuiteAttribute parentSuiteAttribute:
testResult.labels.AddDistinct(Label.ParentSuite(parentSuiteAttribute.ParentSuite), parentSuiteAttribute.Overwrite);
break;
case AllureStoryAttribute storyAttribute:
testResult.labels.AddDistinct("story", storyAttribute.Stories, storyAttribute.Overwrite);
break;
case AllureDescriptionAttribute descriptionAttribute:
testResult.description = descriptionAttribute.Description;
break;
case AllureIdAttribute allureIdAttribute:
var allureIdLabel = new Label {name = "ALLURE_ID", value = allureIdAttribute.AllureId};
testResult.labels.AddDistinct(allureIdLabel, false);
break;
case AllureLabelAttribute labelAttribute:
var label = new Label()
{
name = labelAttribute.Label,
value = labelAttribute.Value
};
testResult.labels.AddDistinct(label, labelAttribute.Overwrite);
break;
}
}
}
}
}