-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathTestEventHandler.cs
216 lines (175 loc) · 6.27 KB
/
TestEventHandler.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
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
using System;
using System.Xml;
using NUnit.Engine;
using NUnit.TextDisplay;
namespace NUnit.ConsoleRunner
{
/// <summary>
/// TestEventHandler processes events from the running
/// test for the console runner.
/// </summary>
#if NETFRAMEWORK
public class TestEventHandler : MarshalByRefObject, ITestEventListener
#else
public class TestEventHandler : ITestEventListener
#endif
{
private readonly ExtendedTextWriter _outWriter;
private readonly bool _displayBeforeTest;
private readonly bool _displayAfterTest;
private readonly bool _displayBeforeOutput;
private string? _lastTestOutput;
private bool _wantNewLine = false;
public TestEventHandler(ExtendedTextWriter outWriter, string labelsOption)
{
_outWriter = outWriter;
labelsOption = labelsOption.ToUpperInvariant();
_displayBeforeTest = labelsOption == "BEFORE" || labelsOption == "BEFOREANDAFTER";
_displayAfterTest = labelsOption == "AFTER" || labelsOption == "BEFOREANDAFTER";
_displayBeforeOutput = _displayBeforeTest || _displayAfterTest || labelsOption == "ONOUTPUTONLY";
}
public void OnTestEvent(string report)
{
var doc = new XmlDocument();
doc.LoadXml(report);
var testEvent = doc.FirstChild;
if (testEvent == null)
return;
switch (testEvent.Name)
{
case "start-test":
TestStarted(testEvent);
break;
case "test-case":
TestFinished(testEvent);
break;
case "test-suite":
SuiteFinished(testEvent);
break;
case "test-output":
TestOutput(testEvent);
break;
}
}
private void TestStarted(XmlNode testResult)
{
var testName = testResult.Attributes?["fullname"]?.Value;
if (_displayBeforeTest && testName != null)
WriteLabelLine(testName);
}
private void TestFinished(XmlNode testResult)
{
var testName = testResult.Attributes?["fullname"]?.Value;
if (testName == null)
return;
var status = testResult.GetAttribute("label") ?? testResult.GetAttribute("result") ?? "Unknown";
var outputNode = testResult.SelectSingleNode("output");
if (outputNode != null)
{
if (_displayBeforeOutput)
WriteLabelLine(testName);
FlushNewLineIfNeeded();
WriteOutputLine(testName, outputNode.InnerText);
}
if (_displayAfterTest)
WriteLabelLineAfterTest(testName, status);
}
private void SuiteFinished(XmlNode testResult)
{
var suiteName = testResult.Attributes?["fullname"]?.Value;
var outputNode = testResult.SelectSingleNode("output");
if (suiteName != null && outputNode != null)
{
if (_displayBeforeOutput)
WriteLabelLine(suiteName);
FlushNewLineIfNeeded();
WriteOutputLine(suiteName, outputNode.InnerText);
}
}
private void TestOutput(XmlNode outputNode)
{
var testName = outputNode.GetAttribute("testname");
if (testName != null)
{
if (_displayBeforeOutput)
WriteLabelLine(testName);
var stream = outputNode.GetAttribute("stream");
WriteOutputLine(testName, outputNode.InnerText, stream == "Error" ? ColorStyle.Error : ColorStyle.Output);
}
}
private string? _currentLabel;
private void WriteLabelLine(string label)
{
if (label != _currentLabel)
{
FlushNewLineIfNeeded();
_lastTestOutput = label;
_outWriter.WriteLine(ColorStyle.SectionHeader, $"=> {label}");
_currentLabel = label;
}
}
private void WriteLabelLineAfterTest(string label, string status)
{
FlushNewLineIfNeeded();
_lastTestOutput = label;
if (status != null)
{
_outWriter.Write(GetColorForResultStatus(status), $"{status} ");
}
_outWriter.WriteLine(ColorStyle.SectionHeader, $"=> {label}");
_currentLabel = label;
}
private void WriteOutputLine(string testName, string text)
{
WriteOutputLine(testName, text, ColorStyle.Output);
}
private void WriteOutputLine(string testName, string text, ColorStyle color)
{
if (_lastTestOutput != testName)
{
FlushNewLineIfNeeded();
_lastTestOutput = testName;
}
_outWriter.Write(color, text);
// If the text we just wrote did not have a new line, flag that we should eventually emit one.
if (!text.EndsWith("\n"))
{
_wantNewLine = true;
}
}
private void FlushNewLineIfNeeded()
{
if (_wantNewLine)
{
_outWriter.WriteLine();
_wantNewLine = false;
}
}
private static ColorStyle GetColorForResultStatus(string status)
{
switch (status)
{
case "Passed":
return ColorStyle.Pass;
case "Failed":
return ColorStyle.Failure;
case "Error":
case "Invalid":
case "Cancelled":
return ColorStyle.Error;
case "Warning":
case "Ignored":
return ColorStyle.Warning;
default:
return ColorStyle.Output;
}
}
#if NETFRAMEWORK
public override object InitializeLifetimeService()
{
return null!;
}
#endif
}
}