qxunittestlogger.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtTest module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include <QtTest/private/qxunittestlogger_p.h> -
43#include <QtTest/private/qtestelement_p.h> -
44#include <QtTest/private/qtestxunitstreamer_p.h> -
45#include <QtTest/qtestcase.h> -
46#include <QtTest/private/qtestresult_p.h> -
47#include <QtTest/private/qbenchmark_p.h> -
48 -
49#include <string.h> -
50 -
51QT_BEGIN_NAMESPACE -
52 -
53QXunitTestLogger::QXunitTestLogger(const char *filename) -
54 : QAbstractTestLogger(filename) -
55 , listOfTestcases(0) -
56 , currentLogElement(0) -
57 , errorLogElement(0) -
58 , logFormatter(0) -
59 , testCounter(0) -
60 , failureCounter(0) -
61 , errorCounter(0) -
62{ -
63}
never executed: }
0
64 -
65QXunitTestLogger::~QXunitTestLogger() -
66{ -
67 delete currentLogElement;
never executed (the execution status of this line is deduced): delete currentLogElement;
-
68 delete logFormatter;
never executed (the execution status of this line is deduced): delete logFormatter;
-
69}
never executed: }
0
70 -
71void QXunitTestLogger::startLogging() -
72{ -
73 QAbstractTestLogger::startLogging();
never executed (the execution status of this line is deduced): QAbstractTestLogger::startLogging();
-
74 -
75 logFormatter = new QTestXunitStreamer(this);
never executed (the execution status of this line is deduced): logFormatter = new QTestXunitStreamer(this);
-
76 delete errorLogElement;
never executed (the execution status of this line is deduced): delete errorLogElement;
-
77 errorLogElement = new QTestElement(QTest::LET_SystemError);
never executed (the execution status of this line is deduced): errorLogElement = new QTestElement(QTest::LET_SystemError);
-
78}
never executed: }
0
79 -
80void QXunitTestLogger::stopLogging() -
81{ -
82 QTestElement *iterator = listOfTestcases;
never executed (the execution status of this line is deduced): QTestElement *iterator = listOfTestcases;
-
83 -
84 char buf[10];
never executed (the execution status of this line is deduced): char buf[10];
-
85 -
86 currentLogElement = new QTestElement(QTest::LET_TestSuite);
never executed (the execution status of this line is deduced): currentLogElement = new QTestElement(QTest::LET_TestSuite);
-
87 currentLogElement->addAttribute(QTest::AI_Name, QTestResult::currentTestObjectName());
never executed (the execution status of this line is deduced): currentLogElement->addAttribute(QTest::AI_Name, QTestResult::currentTestObjectName());
-
88 -
89 qsnprintf(buf, sizeof(buf), "%i", testCounter);
never executed (the execution status of this line is deduced): qsnprintf(buf, sizeof(buf), "%i", testCounter);
-
90 currentLogElement->addAttribute(QTest::AI_Tests, buf);
never executed (the execution status of this line is deduced): currentLogElement->addAttribute(QTest::AI_Tests, buf);
-
91 -
92 qsnprintf(buf, sizeof(buf), "%i", failureCounter);
never executed (the execution status of this line is deduced): qsnprintf(buf, sizeof(buf), "%i", failureCounter);
-
93 currentLogElement->addAttribute(QTest::AI_Failures, buf);
never executed (the execution status of this line is deduced): currentLogElement->addAttribute(QTest::AI_Failures, buf);
-
94 -
95 qsnprintf(buf, sizeof(buf), "%i", errorCounter);
never executed (the execution status of this line is deduced): qsnprintf(buf, sizeof(buf), "%i", errorCounter);
-
96 currentLogElement->addAttribute(QTest::AI_Errors, buf);
never executed (the execution status of this line is deduced): currentLogElement->addAttribute(QTest::AI_Errors, buf);
-
97 -
98 QTestElement *property;
never executed (the execution status of this line is deduced): QTestElement *property;
-
99 QTestElement *properties = new QTestElement(QTest::LET_Properties);
never executed (the execution status of this line is deduced): QTestElement *properties = new QTestElement(QTest::LET_Properties);
-
100 -
101 property = new QTestElement(QTest::LET_Property);
never executed (the execution status of this line is deduced): property = new QTestElement(QTest::LET_Property);
-
102 property->addAttribute(QTest::AI_Name, "QTestVersion");
never executed (the execution status of this line is deduced): property->addAttribute(QTest::AI_Name, "QTestVersion");
-
103 property->addAttribute(QTest::AI_PropertyValue, QTEST_VERSION_STR);
never executed (the execution status of this line is deduced): property->addAttribute(QTest::AI_PropertyValue, "5.0.2");
-
104 properties->addLogElement(property);
never executed (the execution status of this line is deduced): properties->addLogElement(property);
-
105 -
106 property = new QTestElement(QTest::LET_Property);
never executed (the execution status of this line is deduced): property = new QTestElement(QTest::LET_Property);
-
107 property->addAttribute(QTest::AI_Name, "QtVersion");
never executed (the execution status of this line is deduced): property->addAttribute(QTest::AI_Name, "QtVersion");
-
108 property->addAttribute(QTest::AI_PropertyValue, qVersion());
never executed (the execution status of this line is deduced): property->addAttribute(QTest::AI_PropertyValue, qVersion());
-
109 properties->addLogElement(property);
never executed (the execution status of this line is deduced): properties->addLogElement(property);
-
110 -
111 currentLogElement->addLogElement(properties);
never executed (the execution status of this line is deduced): currentLogElement->addLogElement(properties);
-
112 -
113 currentLogElement->addLogElement(iterator);
never executed (the execution status of this line is deduced): currentLogElement->addLogElement(iterator);
-
114 -
115 /* For correct indenting, make sure every testcase knows its parent */ -
116 QTestElement* testcase = iterator;
never executed (the execution status of this line is deduced): QTestElement* testcase = iterator;
-
117 while (testcase) {
never evaluated: testcase
0
118 testcase->setParent(currentLogElement);
never executed (the execution status of this line is deduced): testcase->setParent(currentLogElement);
-
119 testcase = testcase->nextElement();
never executed (the execution status of this line is deduced): testcase = testcase->nextElement();
-
120 }
never executed: }
0
121 -
122 currentLogElement->addLogElement(errorLogElement);
never executed (the execution status of this line is deduced): currentLogElement->addLogElement(errorLogElement);
-
123 -
124 QTestElement *it = currentLogElement;
never executed (the execution status of this line is deduced): QTestElement *it = currentLogElement;
-
125 logFormatter->output(it);
never executed (the execution status of this line is deduced): logFormatter->output(it);
-
126 -
127 QAbstractTestLogger::stopLogging();
never executed (the execution status of this line is deduced): QAbstractTestLogger::stopLogging();
-
128}
never executed: }
0
129 -
130void QXunitTestLogger::enterTestFunction(const char *function) -
131{ -
132 currentLogElement = new QTestElement(QTest::LET_TestCase);
never executed (the execution status of this line is deduced): currentLogElement = new QTestElement(QTest::LET_TestCase);
-
133 currentLogElement->addAttribute(QTest::AI_Name, function);
never executed (the execution status of this line is deduced): currentLogElement->addAttribute(QTest::AI_Name, function);
-
134 currentLogElement->addToList(&listOfTestcases);
never executed (the execution status of this line is deduced): currentLogElement->addToList(&listOfTestcases);
-
135 -
136 ++testCounter;
never executed (the execution status of this line is deduced): ++testCounter;
-
137}
never executed: }
0
138 -
139void QXunitTestLogger::leaveTestFunction() -
140{ -
141} -
142 -
143void QXunitTestLogger::addIncident(IncidentTypes type, const char *description, -
144 const char *file, int line) -
145{ -
146 const char *typeBuf = 0;
never executed (the execution status of this line is deduced): const char *typeBuf = 0;
-
147 char buf[100];
never executed (the execution status of this line is deduced): char buf[100];
-
148 -
149 switch (type) { -
150 case QAbstractTestLogger::XPass: -
151 ++failureCounter;
never executed (the execution status of this line is deduced): ++failureCounter;
-
152 typeBuf = "xpass";
never executed (the execution status of this line is deduced): typeBuf = "xpass";
-
153 break;
never executed: break;
0
154 case QAbstractTestLogger::Pass: -
155 typeBuf = "pass";
never executed (the execution status of this line is deduced): typeBuf = "pass";
-
156 break;
never executed: break;
0
157 case QAbstractTestLogger::XFail: -
158 typeBuf = "xfail";
never executed (the execution status of this line is deduced): typeBuf = "xfail";
-
159 break;
never executed: break;
0
160 case QAbstractTestLogger::Fail: -
161 ++failureCounter;
never executed (the execution status of this line is deduced): ++failureCounter;
-
162 typeBuf = "fail";
never executed (the execution status of this line is deduced): typeBuf = "fail";
-
163 break;
never executed: break;
0
164 default: -
165 typeBuf = "??????";
never executed (the execution status of this line is deduced): typeBuf = "??????";
-
166 break;
never executed: break;
0
167 } -
168 -
169 if (type == QAbstractTestLogger::Fail || type == QAbstractTestLogger::XPass) {
never evaluated: type == QAbstractTestLogger::Fail
never evaluated: type == QAbstractTestLogger::XPass
0
170 QTestElement *failureElement = new QTestElement(QTest::LET_Failure);
never executed (the execution status of this line is deduced): QTestElement *failureElement = new QTestElement(QTest::LET_Failure);
-
171 failureElement->addAttribute(QTest::AI_Result, typeBuf);
never executed (the execution status of this line is deduced): failureElement->addAttribute(QTest::AI_Result, typeBuf);
-
172 if (file)
never evaluated: file
0
173 failureElement->addAttribute(QTest::AI_File, file);
never executed: failureElement->addAttribute(QTest::AI_File, file);
0
174 else -
175 failureElement->addAttribute(QTest::AI_File, "");
never executed: failureElement->addAttribute(QTest::AI_File, "");
0
176 qsnprintf(buf, sizeof(buf), "%i", line);
never executed (the execution status of this line is deduced): qsnprintf(buf, sizeof(buf), "%i", line);
-
177 failureElement->addAttribute(QTest::AI_Line, buf);
never executed (the execution status of this line is deduced): failureElement->addAttribute(QTest::AI_Line, buf);
-
178 failureElement->addAttribute(QTest::AI_Description, description);
never executed (the execution status of this line is deduced): failureElement->addAttribute(QTest::AI_Description, description);
-
179 addTag(failureElement);
never executed (the execution status of this line is deduced): addTag(failureElement);
-
180 currentLogElement->addLogElement(failureElement);
never executed (the execution status of this line is deduced): currentLogElement->addLogElement(failureElement);
-
181 }
never executed: }
0
182 -
183 /* -
184 Only one result can be shown for the whole testfunction. -
185 Check if we currently have a result, and if so, overwrite it -
186 iff the new result is worse. -
187 */ -
188 QTestElementAttribute* resultAttr =
never executed (the execution status of this line is deduced): QTestElementAttribute* resultAttr =
-
189 const_cast<QTestElementAttribute*>(currentLogElement->attribute(QTest::AI_Result));
never executed (the execution status of this line is deduced): const_cast<QTestElementAttribute*>(currentLogElement->attribute(QTest::AI_Result));
-
190 if (resultAttr) {
never evaluated: resultAttr
0
191 const char* oldResult = resultAttr->value();
never executed (the execution status of this line is deduced): const char* oldResult = resultAttr->value();
-
192 bool overwrite = false;
never executed (the execution status of this line is deduced): bool overwrite = false;
-
193 if (!strcmp(oldResult, "pass")) {
never evaluated: !strcmp(oldResult, "pass")
0
194 overwrite = true;
never executed (the execution status of this line is deduced): overwrite = true;
-
195 }
never executed: }
0
196 else if (!strcmp(oldResult, "xfail")) {
never evaluated: !strcmp(oldResult, "xfail")
0
197 overwrite = (type == QAbstractTestLogger::XPass || type == QAbstractTestLogger::Fail);
never evaluated: type == QAbstractTestLogger::XPass
never evaluated: type == QAbstractTestLogger::Fail
0
198 }
never executed: }
0
199 else if (!strcmp(oldResult, "xpass")) {
never evaluated: !strcmp(oldResult, "xpass")
0
200 overwrite = (type == QAbstractTestLogger::Fail);
never executed (the execution status of this line is deduced): overwrite = (type == QAbstractTestLogger::Fail);
-
201 }
never executed: }
0
202 if (overwrite) {
never evaluated: overwrite
0
203 resultAttr->setPair(QTest::AI_Result, typeBuf);
never executed (the execution status of this line is deduced): resultAttr->setPair(QTest::AI_Result, typeBuf);
-
204 }
never executed: }
0
205 }
never executed: }
0
206 else { -
207 currentLogElement->addAttribute(QTest::AI_Result, typeBuf);
never executed (the execution status of this line is deduced): currentLogElement->addAttribute(QTest::AI_Result, typeBuf);
-
208 }
never executed: }
0
209 -
210 if (file)
never evaluated: file
0
211 currentLogElement->addAttribute(QTest::AI_File, file);
never executed: currentLogElement->addAttribute(QTest::AI_File, file);
0
212 else -
213 currentLogElement->addAttribute(QTest::AI_File, "");
never executed: currentLogElement->addAttribute(QTest::AI_File, "");
0
214 -
215 qsnprintf(buf, sizeof(buf), "%i", line);
never executed (the execution status of this line is deduced): qsnprintf(buf, sizeof(buf), "%i", line);
-
216 currentLogElement->addAttribute(QTest::AI_Line, buf);
never executed (the execution status of this line is deduced): currentLogElement->addAttribute(QTest::AI_Line, buf);
-
217 -
218 /* -
219 Since XFAIL does not add a failure to the testlog in xunitxml, add a message, so we still -
220 have some information about the expected failure. -
221 */ -
222 if (type == QAbstractTestLogger::XFail) {
never evaluated: type == QAbstractTestLogger::XFail
0
223 QXunitTestLogger::addMessage(QAbstractTestLogger::Info, description, file, line);
never executed (the execution status of this line is deduced): QXunitTestLogger::addMessage(QAbstractTestLogger::Info, description, file, line);
-
224 }
never executed: }
0
225}
never executed: }
0
226 -
227void QXunitTestLogger::addBenchmarkResult(const QBenchmarkResult &result) -
228{ -
229 QTestElement *benchmarkElement = new QTestElement(QTest::LET_Benchmark);
never executed (the execution status of this line is deduced): QTestElement *benchmarkElement = new QTestElement(QTest::LET_Benchmark);
-
230 -
231 benchmarkElement->addAttribute(
never executed (the execution status of this line is deduced): benchmarkElement->addAttribute(
-
232 QTest::AI_Metric,
never executed (the execution status of this line is deduced): QTest::AI_Metric,
-
233 QTest::benchmarkMetricName(QBenchmarkTestMethodData::current->result.metric));
never executed (the execution status of this line is deduced): QTest::benchmarkMetricName(QBenchmarkTestMethodData::current->result.metric));
-
234 benchmarkElement->addAttribute(QTest::AI_Tag, result.context.tag.toUtf8().data());
never executed (the execution status of this line is deduced): benchmarkElement->addAttribute(QTest::AI_Tag, result.context.tag.toUtf8().data());
-
235 benchmarkElement->addAttribute(QTest::AI_Value, QByteArray::number(result.value).constData());
never executed (the execution status of this line is deduced): benchmarkElement->addAttribute(QTest::AI_Value, QByteArray::number(result.value).constData());
-
236 -
237 char buf[100];
never executed (the execution status of this line is deduced): char buf[100];
-
238 qsnprintf(buf, sizeof(buf), "%i", result.iterations);
never executed (the execution status of this line is deduced): qsnprintf(buf, sizeof(buf), "%i", result.iterations);
-
239 benchmarkElement->addAttribute(QTest::AI_Iterations, buf);
never executed (the execution status of this line is deduced): benchmarkElement->addAttribute(QTest::AI_Iterations, buf);
-
240 currentLogElement->addLogElement(benchmarkElement);
never executed (the execution status of this line is deduced): currentLogElement->addLogElement(benchmarkElement);
-
241}
never executed: }
0
242 -
243void QXunitTestLogger::addTag(QTestElement* element) -
244{ -
245 const char *tag = QTestResult::currentDataTag();
never executed (the execution status of this line is deduced): const char *tag = QTestResult::currentDataTag();
-
246 const char *gtag = QTestResult::currentGlobalDataTag();
never executed (the execution status of this line is deduced): const char *gtag = QTestResult::currentGlobalDataTag();
-
247 const char *filler = (tag && gtag) ? ":" : "";
never evaluated: tag
never evaluated: gtag
0
248 if ((!tag || !tag[0]) && (!gtag || !gtag[0])) {
never evaluated: !tag
never evaluated: !tag[0]
never evaluated: !gtag
never evaluated: !gtag[0]
0
249 return;
never executed: return;
0
250 } -
251 -
252 if (!tag) {
never evaluated: !tag
0
253 tag = "";
never executed (the execution status of this line is deduced): tag = "";
-
254 }
never executed: }
0
255 if (!gtag) {
never evaluated: !gtag
0
256 gtag = "";
never executed (the execution status of this line is deduced): gtag = "";
-
257 }
never executed: }
0
258 -
259 QTestCharBuffer buf;
never executed (the execution status of this line is deduced): QTestCharBuffer buf;
-
260 QTest::qt_asprintf(&buf, "%s%s%s", gtag, filler, tag);
never executed (the execution status of this line is deduced): QTest::qt_asprintf(&buf, "%s%s%s", gtag, filler, tag);
-
261 element->addAttribute(QTest::AI_Tag, buf.constData());
never executed (the execution status of this line is deduced): element->addAttribute(QTest::AI_Tag, buf.constData());
-
262}
never executed: }
0
263 -
264void QXunitTestLogger::addMessage(MessageTypes type, const char *message, const char *file, int line) -
265{ -
266 QTestElement *errorElement = new QTestElement(QTest::LET_Error);
never executed (the execution status of this line is deduced): QTestElement *errorElement = new QTestElement(QTest::LET_Error);
-
267 const char *typeBuf = 0;
never executed (the execution status of this line is deduced): const char *typeBuf = 0;
-
268 -
269 switch (type) { -
270 case QAbstractTestLogger::Warn: -
271 typeBuf = "warn";
never executed (the execution status of this line is deduced): typeBuf = "warn";
-
272 break;
never executed: break;
0
273 case QAbstractTestLogger::QSystem: -
274 typeBuf = "system";
never executed (the execution status of this line is deduced): typeBuf = "system";
-
275 break;
never executed: break;
0
276 case QAbstractTestLogger::QDebug: -
277 typeBuf = "qdebug";
never executed (the execution status of this line is deduced): typeBuf = "qdebug";
-
278 break;
never executed: break;
0
279 case QAbstractTestLogger::QWarning: -
280 typeBuf = "qwarn";
never executed (the execution status of this line is deduced): typeBuf = "qwarn";
-
281 break;
never executed: break;
0
282 case QAbstractTestLogger::QFatal: -
283 typeBuf = "qfatal";
never executed (the execution status of this line is deduced): typeBuf = "qfatal";
-
284 break;
never executed: break;
0
285 case QAbstractTestLogger::Skip: -
286 typeBuf = "skip";
never executed (the execution status of this line is deduced): typeBuf = "skip";
-
287 break;
never executed: break;
0
288 case QAbstractTestLogger::Info: -
289 typeBuf = "info";
never executed (the execution status of this line is deduced): typeBuf = "info";
-
290 break;
never executed: break;
0
291 default: -
292 typeBuf = "??????";
never executed (the execution status of this line is deduced): typeBuf = "??????";
-
293 break;
never executed: break;
0
294 } -
295 -
296 errorElement->addAttribute(QTest::AI_Type, typeBuf);
never executed (the execution status of this line is deduced): errorElement->addAttribute(QTest::AI_Type, typeBuf);
-
297 errorElement->addAttribute(QTest::AI_Description, message);
never executed (the execution status of this line is deduced): errorElement->addAttribute(QTest::AI_Description, message);
-
298 addTag(errorElement);
never executed (the execution status of this line is deduced): addTag(errorElement);
-
299 -
300 if (file)
never evaluated: file
0
301 errorElement->addAttribute(QTest::AI_File, file);
never executed: errorElement->addAttribute(QTest::AI_File, file);
0
302 else -
303 errorElement->addAttribute(QTest::AI_File, "");
never executed: errorElement->addAttribute(QTest::AI_File, "");
0
304 -
305 char buf[100];
never executed (the execution status of this line is deduced): char buf[100];
-
306 qsnprintf(buf, sizeof(buf), "%i", line);
never executed (the execution status of this line is deduced): qsnprintf(buf, sizeof(buf), "%i", line);
-
307 errorElement->addAttribute(QTest::AI_Line, buf);
never executed (the execution status of this line is deduced): errorElement->addAttribute(QTest::AI_Line, buf);
-
308 -
309 currentLogElement->addLogElement(errorElement);
never executed (the execution status of this line is deduced): currentLogElement->addLogElement(errorElement);
-
310 ++errorCounter;
never executed (the execution status of this line is deduced): ++errorCounter;
-
311 -
312 // Also add the message to the system error log (i.e. stderr), if one exists -
313 if (errorLogElement) {
never evaluated: errorLogElement
0
314 QTestElement *systemErrorElement = new QTestElement(QTest::LET_Error);
never executed (the execution status of this line is deduced): QTestElement *systemErrorElement = new QTestElement(QTest::LET_Error);
-
315 systemErrorElement->addAttribute(QTest::AI_Description, message);
never executed (the execution status of this line is deduced): systemErrorElement->addAttribute(QTest::AI_Description, message);
-
316 errorLogElement->addLogElement(systemErrorElement);
never executed (the execution status of this line is deduced): errorLogElement->addLogElement(systemErrorElement);
-
317 }
never executed: }
0
318}
never executed: }
0
319 -
320QT_END_NAMESPACE -
321 -
322 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial