1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtTest module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
53 QXunitTestLogger::QXunitTestLogger(const char *filename)
54 : QAbstractTestLogger(filename)
56 , currentLogElement(0)
65 QXunitTestLogger::~QXunitTestLogger()
67 delete currentLogElement;
71 void QXunitTestLogger::startLogging()
73 QAbstractTestLogger::startLogging();
75 logFormatter = new QTestXunitStreamer(this);
76 delete errorLogElement;
77 errorLogElement = new QTestElement(QTest::LET_SystemError);
80 void QXunitTestLogger::stopLogging()
82 QTestElement *iterator = listOfTestcases;
86 currentLogElement = new QTestElement(QTest::LET_TestSuite);
87 currentLogElement->addAttribute(QTest::AI_Name, QTestResult::currentTestObjectName());
89 qsnprintf(buf, sizeof(buf), "%i", testCounter);
90 currentLogElement->addAttribute(QTest::AI_Tests, buf);
92 qsnprintf(buf, sizeof(buf), "%i", failureCounter);
93 currentLogElement->addAttribute(QTest::AI_Failures, buf);
95 qsnprintf(buf, sizeof(buf), "%i", errorCounter);
96 currentLogElement->addAttribute(QTest::AI_Errors, buf);
98 QTestElement *property;
99 QTestElement *properties = new QTestElement(QTest::LET_Properties);
101 property = new QTestElement(QTest::LET_Property);
102 property->addAttribute(QTest::AI_Name, "QTestVersion");
103 property->addAttribute(QTest::AI_PropertyValue, QTEST_VERSION_STR);
104 properties->addLogElement(property);
106 property = new QTestElement(QTest::LET_Property);
107 property->addAttribute(QTest::AI_Name, "QtVersion");
108 property->addAttribute(QTest::AI_PropertyValue, qVersion());
109 properties->addLogElement(property);
111 currentLogElement->addLogElement(properties);
113 currentLogElement->addLogElement(iterator);
115 /* For correct indenting, make sure every testcase knows its parent */
116 QTestElement* testcase = iterator;
118 testcase->setParent(currentLogElement);
119 testcase = testcase->nextElement();
122 currentLogElement->addLogElement(errorLogElement);
124 QTestElement *it = currentLogElement;
125 logFormatter->output(it);
127 QAbstractTestLogger::stopLogging();
130 void QXunitTestLogger::enterTestFunction(const char *function)
132 currentLogElement = new QTestElement(QTest::LET_TestCase);
133 currentLogElement->addAttribute(QTest::AI_Name, function);
134 currentLogElement->addToList(&listOfTestcases);
139 void QXunitTestLogger::leaveTestFunction()
143 void QXunitTestLogger::addIncident(IncidentTypes type, const char *description,
144 const char *file, int line)
146 const char *typeBuf = 0;
150 case QAbstractTestLogger::XPass:
154 case QAbstractTestLogger::Pass:
157 case QAbstractTestLogger::XFail:
160 case QAbstractTestLogger::Fail:
169 if (type == QAbstractTestLogger::Fail || type == QAbstractTestLogger::XPass) {
170 QTestElement *failureElement = new QTestElement(QTest::LET_Failure);
171 failureElement->addAttribute(QTest::AI_Result, typeBuf);
173 failureElement->addAttribute(QTest::AI_File, file);
175 failureElement->addAttribute(QTest::AI_File, "");
176 qsnprintf(buf, sizeof(buf), "%i", line);
177 failureElement->addAttribute(QTest::AI_Line, buf);
178 failureElement->addAttribute(QTest::AI_Description, description);
179 addTag(failureElement);
180 currentLogElement->addLogElement(failureElement);
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.
188 QTestElementAttribute* resultAttr =
189 const_cast<QTestElementAttribute*>(currentLogElement->attribute(QTest::AI_Result));
191 const char* oldResult = resultAttr->value();
192 bool overwrite = false;
193 if (!strcmp(oldResult, "pass")) {
196 else if (!strcmp(oldResult, "xfail")) {
197 overwrite = (type == QAbstractTestLogger::XPass || type == QAbstractTestLogger::Fail);
199 else if (!strcmp(oldResult, "xpass")) {
200 overwrite = (type == QAbstractTestLogger::Fail);
203 resultAttr->setPair(QTest::AI_Result, typeBuf);
207 currentLogElement->addAttribute(QTest::AI_Result, typeBuf);
211 currentLogElement->addAttribute(QTest::AI_File, file);
213 currentLogElement->addAttribute(QTest::AI_File, "");
215 qsnprintf(buf, sizeof(buf), "%i", line);
216 currentLogElement->addAttribute(QTest::AI_Line, buf);
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.
222 if (type == QAbstractTestLogger::XFail) {
223 QXunitTestLogger::addMessage(QAbstractTestLogger::Info, description, file, line);
227 void QXunitTestLogger::addBenchmarkResult(const QBenchmarkResult &result)
229 QTestElement *benchmarkElement = new QTestElement(QTest::LET_Benchmark);
231 benchmarkElement->addAttribute(
233 QTest::benchmarkMetricName(QBenchmarkTestMethodData::current->result.metric));
234 benchmarkElement->addAttribute(QTest::AI_Tag, result.context.tag.toUtf8().data());
235 benchmarkElement->addAttribute(QTest::AI_Value, QByteArray::number(result.value).constData());
238 qsnprintf(buf, sizeof(buf), "%i", result.iterations);
239 benchmarkElement->addAttribute(QTest::AI_Iterations, buf);
240 currentLogElement->addLogElement(benchmarkElement);
243 void QXunitTestLogger::addTag(QTestElement* element)
245 const char *tag = QTestResult::currentDataTag();
246 const char *gtag = QTestResult::currentGlobalDataTag();
247 const char *filler = (tag && gtag) ? ":" : "";
248 if ((!tag || !tag[0]) && (!gtag || !gtag[0])) {
260 QTest::qt_asprintf(&buf, "%s%s%s", gtag, filler, tag);
261 element->addAttribute(QTest::AI_Tag, buf.constData());
264 void QXunitTestLogger::addMessage(MessageTypes type, const char *message, const char *file, int line)
266 QTestElement *errorElement = new QTestElement(QTest::LET_Error);
267 const char *typeBuf = 0;
270 case QAbstractTestLogger::Warn:
273 case QAbstractTestLogger::QSystem:
276 case QAbstractTestLogger::QDebug:
279 case QAbstractTestLogger::QWarning:
282 case QAbstractTestLogger::QFatal:
285 case QAbstractTestLogger::Skip:
288 case QAbstractTestLogger::Info:
296 errorElement->addAttribute(QTest::AI_Type, typeBuf);
297 errorElement->addAttribute(QTest::AI_Description, message);
298 addTag(errorElement);
301 errorElement->addAttribute(QTest::AI_File, file);
303 errorElement->addAttribute(QTest::AI_File, "");
306 qsnprintf(buf, sizeof(buf), "%i", line);
307 errorElement->addAttribute(QTest::AI_Line, buf);
309 currentLogElement->addLogElement(errorElement);
312 // Also add the message to the system error log (i.e. stderr), if one exists
313 if (errorLogElement) {
314 QTestElement *systemErrorElement = new QTestElement(QTest::LET_Error);
315 systemErrorElement->addAttribute(QTest::AI_Description, message);
316 errorLogElement->addLogElement(systemErrorElement);