/
XmlTestRunner.pas
446 lines (383 loc) · 12.2 KB
/
XmlTestRunner.pas
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
(*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is DUnit.
*
* The Initial Developers of the Original Code are Kent Beck, Erich Gamma,
* and Juancarlo Aez.
* Portions created The Initial Developers are Copyright (C) 1999-2000.
* Portions created by The DUnit Group are Copyright (C) 2000-2003.
* All rights reserved.
*
* Contributor(s):
* Kent Beck <kentbeck@csi.com>
* Erich Gamma <Erich_Gamma@oti.com>
* Juanco Aez <juanco@users.sourceforge.net>
* Chris Morris <chrismo@users.sourceforge.net>
* Jeff Moore <JeffMoore@users.sourceforge.net>
* Kris Golko <neuromancer@users.sourceforge.net>
* The DUnit group at SourceForge <http://dunit.sourceforge.net>
*)
{
Contributor : Martin Gondermann <magicmonty@pagansoft.de>
}
unit XMLTestRunner;
interface
uses
SysUtils,
Classes,
TestFramework;
const
DEFAULT_FILENAME = 'dunit-report.xml';
type
TXMLTestListener = class(TInterfacedObject, ITestListener, ITestListenerX)
private
FOutputFile: TextFile;
FOutputFileName: string;
FFileOpen: Boolean;
FStartTime: Cardinal;
FDtStartTime: TDateTime;
FSuiteStack: TStringList;
class var FConsoleOutput: string;
class var FFailureOutput: string;
class var FErrorOutput: string;
procedure WriteReport(const AValue: string);
function GetCurrentSuiteName: string;
function Text2Sgml(const AText: string): string;
public
// implement the ITestListener interface
procedure AddSuccess(ATest: ITest); virtual;
procedure AddError(AError: TTestFailure); virtual;
procedure AddFailure(AFailure: TTestFailure); virtual;
function ShouldRunTest(ATest: ITest):boolean; virtual;
procedure StartSuite(ASuite: ITest); virtual;
procedure EndSuite(ASuite: ITest); virtual;
procedure StartTest(ATest: ITest); virtual;
procedure EndTest(ATest: ITest); virtual;
procedure TestingStarts; virtual;
procedure TestingEnds(ATestResult: TTestResult); virtual;
procedure Status(ATest: ITest; const Msg: string);
procedure Warning(ATest: ITest; const Msg: string);
constructor Create; overload;
constructor Create(const AOutputFile: string); overload;
destructor Destroy; override;
class function RunTest(const ASuite: ITest; const AOutputFile: string): TTestResult; overload;
class function RunRegisteredTests(const AOutputFile: string): TTestResult;
property OutputFileName: string read FOutputFileName write FOutputFileName;
end;
// Run the given test suite
function RunTest(const ASuite: ITest): TTestResult; overload;
function RunRegisteredTests: TTestResult; overload;
function TestsWereSuccessful: Boolean;
procedure RunTestsAndClose;
implementation
uses Forms, Windows;
const
CRLF = #13#10;
MAX_DEEP = 5;
TrueFalse : array[Boolean] of string = ('False', 'True');
function OutputFile: string;
begin
if (ParamCount = 2) and DirectoryExists(ExtractFilePath(ParamStr(2))) then
Result := ParamStr(2)
else
Result := DEFAULT_FILENAME;
end;
function TestsWereSuccessful: Boolean;
begin
Result := RunRegisteredTests.WasSuccessful;
end;
function RunRegisteredTests: TTestResult;
begin
Result := TXMLTestListener.RunRegisteredTests(OutputFile);
end;
function RunTest(const ASuite: ITest): TTestResult;
begin
Result := TXMLTestListener.RunTest(ASuite, OutputFile);
end;
procedure RunTestsAndClose;
begin
try
if not TestsWereSuccessful then
Halt(1);
except
on E: Exception do
Writeln(Format('%s: %s', [E.ClassName, E.Message]));
end;
end;
{$region 'TXMLTestListener'}
class function TXMLTestListener.RunTest(
const ASuite: ITest; const AOutputFile: string): TTestResult;
begin
Result := TestFramework.RunTest(ASuite, [TXMLTestListener.Create(AOutputFile)]);
end;
class function TXMLTestListener.RunRegisteredTests(
const AOutputFile: string): TTestResult;
begin
Result := TXMLTestListener.RunTest(registeredTests, AOutputFile);
end;
constructor TXMLTestListener.Create;
begin
Create(DEFAULT_FILENAME);
end;
constructor TXMLTestListener.Create(const AOutputFile: string);
begin
inherited Create;
FOutputFileName := AOutputFile;
FSuiteStack := TStringList.Create;
FFileOpen := False;
FConsoleOutput := '';
FFailureOutput := '';
FErrorOutput := '';
end;
destructor TXMLTestListener.Destroy;
begin
FreeAndNil(FSuiteStack);
inherited Destroy;
end;
{Write F in the report file or on standard output if none specified}
procedure TXMLTestListener.WriteReport(const AValue: string);
begin
if FFileOpen then
begin
if TTextRec(FOutputFile).Mode = fmOutput then
writeln(FOutputFile, AValue)
else
writeln(AValue);
end;
end;
procedure TXMLTestListener.AddSuccess(ATest: ITest);
begin
if ATest.Tests.Count<=0 then
begin
WriteReport(
Format(
'<test-case name="%s%s" executed="%s" success="True" time="%1.3f" result="Pass"/>',
[
GetCurrentSuiteName,
ATest.GetName,
TrueFalse[ATest.Enabled],
ATest.ElapsedTestTime / 1000
]
)
);
end;
end;
procedure TXMLTestListener.AddError(AError: TTestFailure);
var
tmp: string;
begin
WriteReport(
Format(
'<test-case name="%s%s" execute="%s" success="False" time="%1.3f" result="Error">',
[
GetCurrentSuiteName,
AError.FailedTest.GetName,
TrueFalse[AError.FailedTest.Enabled],
AError.FailedTest.ElapsedTestTime / 1000
]
)
);
WriteReport(
Format(
'<failure name="%s" location="%s"/>',
[AError.ThrownExceptionName, AError.LocationInfo]
)
);
WriteReport('<message>' + Text2Sgml(AError.ThrownExceptionMessage) + '</message>');
WriteReport('</test-case>');
tmp := GetCurrentSuiteName + AError.FailedTest.Name;
if Trim(AError.LocationInfo) <> EmptyStr then
tmp := tmp + ' (' + AError.LocationInfo + ')';
tmp := tmp + ':' + CRLF + #9 + AError.ThrownExceptionMessage + CRLF;
FErrorOutput := FErrorOutput + CRLF + tmp;
end;
procedure TXMLTestListener.AddFailure(AFailure: TTestFailure);
var
tmp: string;
begin
WriteReport(
Format(
'<test-case name="%s%s" execute="%s" success="False" time="%1.3f" result="Failure">',
[
GetCurrentSuiteName,
AFailure.FailedTest.GetName,
TrueFalse[AFailure.FailedTest.Enabled],
AFailure.FailedTest.ElapsedTestTime / 1000
]
)
);
WriteReport(
Format(
'<failure name="%s" location="%s"/>',
[AFailure.ThrownExceptionName, AFailure.LocationInfo]
)
);
WriteReport('<message>' + Text2Sgml(AFailure.ThrownExceptionMessage) + '</message>');
WriteReport('</test-case>');
tmp := GetCurrentSuiteName + AFailure.FailedTest.Name;
if Trim(AFailure.LocationInfo) <> EmptyStr then
tmp := tmp + ' (' + AFailure.LocationInfo + ')';
tmp := tmp + ': ' + CRLF + #9 + AFailure.ThrownExceptionMessage + CRLF;
FFailureOutput := FFailureOutput + tmp + CRLF;
end;
procedure TXMLTestListener.StartTest(ATest: ITest);
begin
end;
procedure TXMLTestListener.EndTest(ATest: ITest);
begin
end;
procedure TXMLTestListener.TestingStarts;
begin
FStartTime := GetTickCount;
FDtStartTime := Now;
FConsoleOutput := '';
FErrorOutput := '';
FFailureOutput := '';
try
if FOutputFileName<>'' then
begin
AssignFile(FOutputFile, FOutputFileName);
Rewrite(FOutputFile);
end;
except
on E:EInOutError do
FFileOpen := False;
end;
WriteReport('<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>');
WriteReport(
Format(
'<test-results total="%d" notrun="%d" date="%s" time="%s">',
[
RegisteredTests.CountTestCases,
RegisteredTests.CountTestCases - RegisteredTests.CountEnabledTestCases,
DateToStr(Now),
TimeToStr(Now)
]
)
);
end;
procedure TXMLTestListener.TestingEnds(ATestResult: TTestResult);
var
RunTime : Double;
SuccessRate : Integer;
RunCount, ErrorCount, FailureCount: Integer;
begin
RunTime := (GetTickCount - FStartTime) / 1000;
try
RunCount := ATestResult.RunCount;
except
RunCount := 0;
end;
try
if not ATestResult.WasSuccessful then
FailureCount := ATestResult.FailureCount
else
FailureCount := 0;
except
FailureCount := 0;
end;
try
if not ATestResult.WasSuccessful then
ErrorCount := ATestResult.ErrorCount
else
ErrorCount := 0;
except
ErrorCount := 0;
end;
if RunCount > 0 then
SuccessRate := Trunc(((RunCount - FailureCount - ErrorCount) / RunCount) *100)
else
SuccessRate := 100;
WriteReport('<statistics>' + CRLF +
'<stat name="tests" value="' + IntToStr(RunCount) + '" />' + CRLF +
'<stat name="failures" value="' + IntToStr(FailureCount) + '" />' + CRLF +
'<stat name="errors" value="' + IntToStr(ErrorCount) + '" />' + CRLF +
'<stat name="success-rate" value="' + IntToStr(SuccessRate) + '%" />' + CRLF +
'<stat name="started-at" value="' + DateTimeToStr(FDtStartTime) + '" />' + CRLF +
'<stat name="finished-at" value="' + DateTimeToStr(now) + '" />' + CRLF +
Format('<stat name="runtime" value="%1.3f"/>', [RunTime]) + CRLF +
'</statistics>' + CRLF +
'</test-results>');
if FFileOpen and (TTextRec(FOutputFile).Mode = fmOutput) then
Close(FOutputFile);
FConsoleOutput := 'Run: ' + IntToStr(RunCount) +
' / Failures: ' + IntToStr(FailureCount) +
' / Errors: ' + IntToStr(ErrorCount);
if ErrorCount > 0 then
FConsoleOutput := FConsoleOutput + CRLF + 'Errors:' + CRLF + Trim(FErrorOutput);
if FailureCount > 0 then
FConsoleOutput := FConsoleOutput + CRLF + 'Failures:' + CRLF + Trim(FFailureOutput);
FConsoleOutput := Trim(FConsoleOutput);
Writeln(FConsoleOutput);
end;
procedure TXMLTestListener.Status(ATest: ITest; const Msg: string);
begin
WriteReport(Format('INFO: %s: %s', [ATest.Name, Msg]));
end;
procedure TXMLTestListener.Warning(ATest: ITest; const Msg: string);
begin
WriteReport(Format('WARNING: %s: %s', [ATest.Name, Msg]));
end;
function TXMLTestListener.ShouldRunTest(ATest: ITest): Boolean;
begin
Result := ATest.Enabled;
if not Result then
WriteReport(
Format(
'<test-case name="%s%s" executed="False"/>',
[GetCurrentSuiteName, ATest.GetName]
)
);
end;
procedure TXMLTestListener.EndSuite(ASuite: ITest);
begin
if CompareText(ASuite.Name, ExtractFileName(Application.ExeName)) = 0 then
Exit;
WriteReport('</results>');
WriteReport('</test-suite>');
FSuiteStack.Delete(0);
end;
procedure TXMLTestListener.StartSuite(ASuite: ITest);
var
s: string;
begin
if CompareText(ASuite.Name, ExtractFileName(Application.ExeName)) = 0 then
Exit;
s := GetCurrentSuiteName + ASuite.Name;
WriteReport(
Format(
'<test-suite name="%s" total="%d" notrun="%d">',
[
s,
ASuite.CountTestCases,
ASuite.CountTestCases - ASuite.CountEnabledTestCases
]
)
);
FSuiteStack.Insert(0, ASuite.Name);
WriteReport('<results>');
end;
function TXMLTestListener.GetCurrentSuiteName: string;
var
StackIndex: Integer;
begin
Result := '';
for StackIndex := 0 to FSuiteStack.Count - 1 do
Result := FSuiteStack[StackIndex] + '.' + Result;
end;
function TXMLTestListener.Text2Sgml(const AText: string): string;
begin
Result := StringReplace(AText, '<', '<', [rfReplaceAll]);
Result := StringReplace(Result, '>', '>', [rfReplaceAll]);
end;
{$endregion 'TXMLTestListener'}
end.