-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathResultReporter.cs
More file actions
280 lines (233 loc) · 11.7 KB
/
Copy pathResultReporter.cs
File metadata and controls
280 lines (233 loc) · 11.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
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
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
using System;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Xml;
using NUnit.Common;
namespace NUnit.TextDisplay
{
public class ResultReporter
{
private ResultReporterSettings _settings;
public ResultReporter()
{
_settings = new ResultReporterSettings();
}
public ResultReporter(ResultReporterSettings settings)
{
_settings = settings;
}
public static void WriteHeader(ExtendedTextWriter writer)
{
Assembly entryAssembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
var versionBlock = FileVersionInfo.GetVersionInfo(entryAssembly.ManifestModule.FullyQualifiedName);
var header = $"{versionBlock.ProductName} {versionBlock.ProductVersion}";
writer.WriteLine(ColorStyle.Header, header);
writer.WriteLine(ColorStyle.SubHeader, versionBlock.LegalCopyright ?? "No Copyright statement found");
writer.WriteLine(ColorStyle.SubHeader, DateTime.Now.ToString(CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern));
writer.WriteLine();
}
/// <summary>
/// Reports the results to the console
/// </summary>
public void ReportResults(ResultSummary summary, ExtendedTextWriter writer)
{
var topLevelResult = summary.ResultNode;
if (summary.ExplicitCount + summary.SkipCount + summary.IgnoreCount > 0)
WriteNotRunReport(topLevelResult, writer);
if (summary.OverallResult == "Failed" || summary.WarningCount > 0)
WriteErrorsFailuresAndWarningsReport(topLevelResult, writer);
WriteRunSettingsReport(topLevelResult, writer);
WriteSummaryReport(summary, writer);
}
internal static void WriteRunSettingsReport(XmlNode topLevelResult, ExtendedTextWriter writer)
{
var firstSuite = topLevelResult.SelectSingleNode("test-suite");
if (firstSuite is not null)
{
var settings = firstSuite.SelectNodes("settings/setting");
if (settings is not null && settings.Count > 0)
{
writer.WriteLine(ColorStyle.SectionHeader, "Run Settings");
foreach (XmlNode node in settings)
WriteSettingsNode(node, writer);
writer.WriteLine();
}
}
}
private static void WriteSettingsNode(XmlNode node, ExtendedTextWriter writer)
{
var items = node.SelectNodes("item");
var name = node.GetAttribute("name");
var val = node.GetAttribute("value") ?? string.Empty;
if (items is null || items.Count == 0)
writer.WriteLabelLine($" {name}:", $" |{val}|");
else
{
writer.WriteLabelLine($" {name}:", string.Empty);
foreach (XmlNode item in items)
{
var key = item.GetAttribute("key");
var value = item.GetAttribute("value");
writer.WriteLine(ColorStyle.Value, $" {key} -> |{value}|");
}
}
}
internal static void WriteSummaryReport(ResultSummary summary, ExtendedTextWriter writer)
{
const string INDENT4 = " ";
const string INDENT8 = " ";
var topLevelResult = summary.ResultNode;
ColorStyle resultColor = summary.OverallResult == "Passed"
? ColorStyle.Pass
: summary.OverallResult == "Failed" || summary.OverallResult == "Unknown"
? ColorStyle.Failure
: summary.OverallResult == "Warning"
? ColorStyle.Warning
: ColorStyle.Output;
writer.WriteLine(ColorStyle.SectionHeader, "Test Run Summary");
writer.WriteLabelLine(INDENT4 + "Overall result: ", summary.OverallResult, resultColor);
WriteSummaryCount(writer, INDENT4 + "Test Count: ", summary.TestCount);
WriteSummaryCount(writer, ", Pass: ", summary.PassCount);
WriteSummaryCount(writer, ", Fail: ", summary.FailedCount, ColorStyle.Failure);
WriteSummaryCount(writer, ", Warn: ", summary.WarningCount, ColorStyle.Warning);
WriteSummaryCount(writer, ", Inconclusive: ", summary.InconclusiveCount);
WriteSummaryCount(writer, ", Skip: ", summary.TotalSkipCount);
writer.WriteLine();
if (summary.FailedCount > 0)
{
WriteSummaryCount(writer, INDENT8 + "Failed Tests - Failures: ", summary.FailureCount);
WriteSummaryCount(writer, ", Errors: ", summary.ErrorCount, ColorStyle.Error);
WriteSummaryCount(writer, ", Invalid: ", summary.InvalidCount);
writer.WriteLine();
}
if (summary.TotalSkipCount > 0)
{
WriteSummaryCount(writer, INDENT8 + "Skipped Tests - Ignored: ", summary.IgnoreCount);
WriteSummaryCount(writer, ", Explicit: ", summary.ExplicitCount);
WriteSummaryCount(writer, ", Other: ", summary.SkipCount);
writer.WriteLine();
}
var duration = topLevelResult.GetAttribute("duration", 0.0);
var startTime = topLevelResult.GetAttribute("start-time", DateTime.MinValue);
var endTime = topLevelResult.GetAttribute("end-time", DateTime.MaxValue);
writer.WriteLabelLine(INDENT4 + "Start time: ", startTime.ToString("u"));
writer.WriteLabelLine(INDENT4 + "End time: ", endTime.ToString("u"));
writer.WriteLabelLine(INDENT4 + "Duration: ", string.Format(NumberFormatInfo.InvariantInfo, "{0:0.000} seconds", duration));
writer.WriteLine();
}
internal void WriteErrorsFailuresAndWarningsReport(XmlNode resultNode, ExtendedTextWriter writer)
{
int reportIndex = 0;
writer.WriteLine(ColorStyle.SectionHeader, "Errors, Failures and Warnings");
writer.WriteLine();
WriteErrorsFailuresAndWarnings(resultNode, writer, ref reportIndex);
if (_settings.StopOnError)
{
writer.WriteLine(ColorStyle.Failure, "Execution terminated after first error");
writer.WriteLine();
}
}
private static void WriteErrorsFailuresAndWarnings(XmlNode resultNode, ExtendedTextWriter writer, ref int reportIndex)
{
string? resultState = resultNode.GetAttribute("result");
switch (resultNode.Name)
{
case "test-case":
if (resultState == "Failed" || resultState == "Warning")
new ClientTestResult(resultNode, ++reportIndex).WriteResult(writer);
return;
case "test-run":
foreach (XmlNode childResult in resultNode.ChildNodes)
WriteErrorsFailuresAndWarnings(childResult, writer, ref reportIndex);
break;
case "test-suite":
if (resultState == "Failed" || resultState == "Warning")
{
var suiteType = resultNode.GetAttribute("type");
if (suiteType == "Theory")
{
// Report failure of the entire theory and then go on
// to list the individual cases that failed
new ClientTestResult(resultNode, ++reportIndex).WriteResult(writer);
}
else
{
// Where did this happen? Default is in the current test.
var site = resultNode.GetAttribute("site");
// Correct a problem in some framework versions, whereby warnings and some failures
// are promulgated to the containing suite without setting the FailureSite.
if (site is null)
{
if (resultNode.SelectSingleNode("reason/message")?.InnerText == "One or more child tests had warnings" ||
resultNode.SelectSingleNode("failure/message")?.InnerText == "One or more child tests had errors")
{
site = "Child";
}
else
site = "Test";
}
// Only report errors in the current test method, setup or teardown
if (site == "SetUp" || site == "TearDown" || site == "Test")
new ClientTestResult(resultNode, ++reportIndex).WriteResult(writer);
// Do not list individual "failed" tests after a one-time setup failure
if (site == "SetUp")
return;
}
}
foreach (XmlNode childResult in resultNode.ChildNodes)
WriteErrorsFailuresAndWarnings(childResult, writer, ref reportIndex);
break;
}
}
internal void WriteNotRunReport(XmlNode resultNode, ExtendedTextWriter writer)
{
int reportIndex = 0;
writer.WriteLine(ColorStyle.SectionHeader, "Tests Not Run");
writer.WriteLine();
WriteNotRunResults(resultNode, writer, ref reportIndex);
}
private void WriteNotRunResults(XmlNode resultNode, ExtendedTextWriter writer, ref int reportIndex)
{
switch (resultNode.Name)
{
case "test-case":
string? status = resultNode.GetAttribute("result");
if (status == "Skipped")
{
bool display = true;
string? label = resultNode.GetAttribute("label");
switch (label)
{
case "Explicit":
display = !_settings.OmitExplicitTests;
break;
case "Ignored":
display = !_settings.OmitIgnoredTests;
break;
}
if (display)
new ClientTestResult(resultNode, ++reportIndex).WriteResult(writer);
}
break;
case "test-suite":
case "test-run":
foreach (XmlNode childResult in resultNode.ChildNodes)
WriteNotRunResults(childResult, writer, ref reportIndex);
break;
}
}
private static void WriteSummaryCount(ExtendedTextWriter writer, string label, int count)
{
writer.WriteLabel(label, count.ToString(CultureInfo.CurrentUICulture));
}
private static void WriteSummaryCount(ExtendedTextWriter writer, string label, int count, ColorStyle color)
{
writer.WriteLabel(label, count.ToString(CultureInfo.CurrentUICulture), count > 0 ? color : ColorStyle.Value);
}
}
}