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
281
282
283
284
285
286
287
288
289
290
291
292
|
{
Copyright (C) 2006 Graeme Geldenhuys <graemeg@gmail.com>
This source is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
Purpose:
This unit contains an XML TestListener for use with the fpcUnit testing
framework. It uses the XMLWrite unit, which is part of FPC, to generate
the XML document. The benefit of using the XMLWrite unit is that the
data generated is valid XML, with reserved characters correctly escaped.
This allows the XML document to be further processed with XSLT etc without
any issues.
}
unit xmlreporter;
{$mode objfpc}{$H+}
interface
uses
Classes
,SysUtils
,fpcUnit
,TestUtils
,dom
,XMLWrite
;
type
{ XML Test Listener }
{ TXMLResultsWriter }
TXMLResultsWriter = class(TNoRefCountObject, ITestListener)
private
FDoc: TXMLDocument;
{ These TDOMNodes are for easy access and a bit of optimization }
FResults: TDOMNode;
FListing: TDOMNode;
FFailures: TDOMNode;
FIgnores: TDOMNode;
FErrors: TDOMNode;
FLastTestSuite: TDOMNode;
FStartCrono: TDateTime;
FskipTiming : Boolean;
{ Converts the actual test results into XML nodes. This gets called
by the public method WriteResult. }
procedure TestResultAsXML(pTestResult: TTestResult);
{ This gets called in the class constructor and sets up the starting nodes }
procedure WriteHeader;
public
constructor Create;
destructor Destroy; override;
procedure WriteResult(aResult: TTestResult);
{ ITestListener interface requirements }
procedure AddFailure(ATest: TTest; AFailure: TTestFailure);
procedure AddError(ATest: TTest; AError: TTestFailure);
procedure StartTest(ATest: TTest);
procedure EndTest(ATest: TTest);
procedure StartTestSuite(ATestSuite: TTestSuite);
procedure EndTestSuite(ATestSuite: TTestSuite);
{ A public property to the internal XML document }
property Document: TXMLDocument read FDoc;
Property SkipTiming : Boolean Read FSkipTiming Write FSkipTiming;
end;
implementation
{ TXMLResultsWriter }
procedure TXMLResultsWriter.TestResultAsXML(pTestResult: TTestResult);
var
n, lResults: TDOMNode;
begin
lResults := FDoc.FindNode('TestResults');
n := FDoc.CreateElement('NumberOfRunTests');
n.AppendChild(FDoc.CreateTextNode(IntToStr(pTestResult.RunTests)));
lResults.AppendChild(n);
n := FDoc.CreateElement('NumberOfErrors');
n.AppendChild(FDoc.CreateTextNode(IntToStr(pTestResult.NumberOfErrors)));
lResults.AppendChild(n);
n := FDoc.CreateElement('NumberOfFailures');
n.AppendChild(FDoc.CreateTextNode(IntToStr(pTestResult.NumberOfFailures)));
lResults.AppendChild(n);
n := FDoc.CreateElement('NumberOfIgnoredTests');
n.AppendChild(FDoc.CreateTextNode(IntToStr(pTestResult.NumberOfIgnoredTests)));
lResults.AppendChild(n);
{ We don't have access to TCustomResultsWriter so we cannot honour SkipTiming}
if not(SkipTiming) then
begin
n := FDoc.CreateElement('TotalElapsedTime');
n.AppendChild(FDoc.CreateTextNode(FormatDateTime('hh:nn:ss.zzz', Now - pTestResult.StartingTime)));
lResults.AppendChild(n);
end;
{ Summary of ISO 8601 http://www.cl.cam.ac.uk/~mgk25/iso-time.html }
n := FDoc.CreateElement('DateTimeRan');
n.AppendChild(FDoc.CreateTextNode(FormatDateTime('yyyy-mm-dd hh:mm:ss', Now)));
lResults.AppendChild(n);
end;
procedure TXMLResultsWriter.WriteHeader;
begin
FResults := FDoc.CreateElement('TestResults');
FResults.AppendChild(FDoc.CreateComment(' Generated using FPCUnit on '
+ FormatDateTime('yyyy-mm-dd hh:mm:ss', Now) ));
FDoc.AppendChild(FResults);
FListing := FDoc.CreateElement('TestListing');
FResults.AppendChild(FListing);
end;
constructor TXMLResultsWriter.Create;
begin
FDoc := TXMLDocument.Create;
FResults := nil;
FFailures := nil;
FIgnores := nil;
FErrors := nil;
FListing := nil;
FLastTestSuite := nil;
WriteHeader;
end;
destructor TXMLResultsWriter.Destroy;
begin
FResults := nil;
FFailures := nil;
FIgnores := nil;
FErrors := nil;
FListing := nil;
FLastTestSuite := nil;
FDoc.Free;
inherited Destroy;
end;
procedure TXMLResultsWriter.WriteResult(aResult: TTestResult);
begin
TestResultAsXML(aResult);
end;
procedure TXMLResultsWriter.AddFailure(ATest: TTest; AFailure: TTestFailure);
var
n: TDOMElement;
begin
if AFailure.IsIgnoredTest then
begin
{ Try and find the node first }
if not Assigned(FIgnores) then
FIgnores := FDoc.FindNode('ListOfIgnoredTests');
{ If we couldn't find it, create it }
if not Assigned(FIgnores) then
begin
FIgnores := FDoc.CreateElement('ListOfIgnoredTests');
FResults.AppendChild(FIgnores);
end;
n := FDoc.CreateElement('IgnoredTest');
n.AppendChild(FDoc.CreateElement('Message') ).AppendChild(FDoc.CreateTextNode(AFailure.AsString));
n.AppendChild(FDoc.CreateElement('ExceptionClass') ).AppendChild(FDoc.CreateTextNode(AFailure.ExceptionClassName));
n.AppendChild(FDoc.CreateElement('ExceptionMessage')).AppendChild(FDoc.CreateTextNode(AFailure.ExceptionMessage));
FIgnores.AppendChild(n);
end
else
begin
{ Try and find the node first }
if not Assigned(FFailures) then
FFailures := FDoc.FindNode('ListOfFailures');
{ If we couldn't find it, create it }
if not Assigned(FFailures) then
begin
FFailures := FDoc.CreateElement('ListOfFailures');
FResults.AppendChild(FFailures);
end;
n := FDoc.CreateElement('Failure');
n.AppendChild(FDoc.CreateElement('Message') ).AppendChild(FDoc.CreateTextNode(AFailure.AsString));
n.AppendChild(FDoc.CreateElement('ExceptionClass') ).AppendChild(FDoc.CreateTextNode(AFailure.ExceptionClassName));
n.AppendChild(FDoc.CreateElement('ExceptionMessage')).AppendChild(FDoc.CreateTextNode(AFailure.ExceptionMessage));
FFailures.AppendChild(n);
end;
end;
procedure TXMLResultsWriter.AddError(ATest: TTest; AError: TTestFailure);
var
n: TDOMElement;
begin
{ Try and find the node first }
if not Assigned(FErrors) then
FErrors := FDoc.FindNode('ListOfErrors');
{ If we couldn't find it, create it }
if not Assigned(FErrors) then
begin
FErrors := FDoc.CreateElement('ListOfErrors');
FResults.AppendChild(FErrors);
end;
n := FDoc.CreateElement('Error');
n.AppendChild(FDoc.CreateElement('Message') ).AppendChild(FDoc.CreateTextNode(AError.AsString));
n.AppendChild(FDoc.CreateElement('ExceptionClass') ).AppendChild(FDoc.CreateTextNode(AError.ExceptionClassName));
n.AppendChild(FDoc.CreateElement('ExceptionMessage')).AppendChild(FDoc.CreateTextNode(AError.ExceptionMessage));
n.AppendChild(FDoc.CreateElement('SourceUnitName') ).AppendChild(FDoc.CreateTextNode(AError.SourceUnitName));
n.AppendChild(FDoc.CreateElement('LineNumber') ).AppendChild(FDoc.CreateTextNode(IntToStr(AError.LineNumber)));
n.AppendChild(FDoc.CreateElement('FailedMethodName')).AppendChild(FDoc.CreateTextNode(AError.FailedMethodName));
FErrors.AppendChild(n);
end;
procedure TXMLResultsWriter.StartTest(ATest: TTest);
var
n: TDOMElement;
begin
n := FDoc.CreateElement('Test');
n['Name'] := ATest.TestSuiteName + '.' + ATest.TestName;
FLastTestSuite.AppendChild(n);
FStartCrono := Now;
end;
procedure TXMLResultsWriter.EndTest(ATest: TTest);
var
n: TDOMNode;
lNew: TDOMElement;
begin
n := FLastTestSuite.LastChild;
if not(SkipTiming) then
begin
lNew := FDoc.CreateElement('ElapsedTime');
lNew.AppendChild(FDoc.CreateTextNode(FormatDateTime('hh:nn:ss.zzz', Now - FStartCrono)));
n.AppendChild(lNew);
end;
end;
procedure TXMLResultsWriter.StartTestSuite(ATestSuite: TTestSuite);
var
n: TDOMElement;
begin
{ Try and find the Listings node first }
if not Assigned(FListing) then
FListing := FDoc.FindNode('TestListing');
{ If we couldn't find it, create it }
if not Assigned(FListing) then
begin
FListing := FDoc.CreateElement('TestListing');
FResults.AppendChild(FListing);
end;
{ The first TestSuite always seem to be blank/empty }
if ATestSuite.TestName <> '' then
begin
n := FDoc.CreateElement('TestSuite');
n['Name'] := ATestSuite.TestName;
FListing.AppendChild(n);
FLastTestSuite := n;
end;
end;
procedure TXMLResultsWriter.EndTestSuite(ATestSuite: TTestSuite);
begin
// do nothing
end;
end.
|