1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <QtTest/private/qjunittestlogger_p.h>
41#include <QtTest/private/qtestelement_p.h>
42#include <QtTest/private/qtestjunitstreamer_p.h>
43#include <QtTest/qtestcase.h>
44#include <QtTest/private/qtestresult_p.h>
45#include <QtTest/private/qbenchmark_p.h>
46#include <QtTest/private/qtestlog_p.h>
47
48#ifdef min // windows.h without NOMINMAX is included by the benchmark headers.
49# undef min
50#endif
51#ifdef max
52# undef max
53#endif
54
55#include <QtCore/qlibraryinfo.h>
56
57#include <string.h>
58
59QT_BEGIN_NAMESPACE
60
61QJUnitTestLogger::QJUnitTestLogger(const char *filename)
62 : QAbstractTestLogger(filename)
63{
64}
65
66QJUnitTestLogger::~QJUnitTestLogger()
67{
68 Q_ASSERT(!currentTestSuite);
69 delete logFormatter;
70}
71
72void QJUnitTestLogger::startLogging()
73{
74 QAbstractTestLogger::startLogging();
75
76 logFormatter = new QTestJUnitStreamer(this);
77 delete errorLogElement;
78 errorLogElement = new QTestElement(QTest::LET_SystemError);
79
80 Q_ASSERT(!currentTestSuite);
81 currentTestSuite = new QTestElement(QTest::LET_TestSuite);
82 currentTestSuite->addAttribute(QTest::AI_Name, QTestResult::currentTestObjectName());
83
84 auto localTime = QDateTime::currentDateTime();
85 auto localTimeWithUtcOffset = localTime.toOffsetFromUtc(localTime.offsetFromUtc());
86 currentTestSuite->addAttribute(QTest::AI_Timestamp,
87 localTimeWithUtcOffset.toString(Qt::ISODate).toUtf8().constData());
88
89 QTestElement *property;
90 QTestElement *properties = new QTestElement(QTest::LET_Properties);
91
92 property = new QTestElement(QTest::LET_Property);
93 property->addAttribute(QTest::AI_Name, "QTestVersion");
94 property->addAttribute(QTest::AI_PropertyValue, QTEST_VERSION_STR);
95 properties->addLogElement(property);
96
97 property = new QTestElement(QTest::LET_Property);
98 property->addAttribute(QTest::AI_Name, "QtVersion");
99 property->addAttribute(QTest::AI_PropertyValue, qVersion());
100 properties->addLogElement(property);
101
102 property = new QTestElement(QTest::LET_Property);
103 property->addAttribute(QTest::AI_Name, "QtBuild");
104 property->addAttribute(QTest::AI_PropertyValue, QLibraryInfo::build());
105 properties->addLogElement(property);
106
107 currentTestSuite->addLogElement(properties);
108}
109
110void QJUnitTestLogger::stopLogging()
111{
112 char buf[10];
113
114 qsnprintf(buf, sizeof(buf), "%i", testCounter);
115 currentTestSuite->addAttribute(QTest::AI_Tests, buf);
116
117 qsnprintf(buf, sizeof(buf), "%i", failureCounter);
118 currentTestSuite->addAttribute(QTest::AI_Failures, buf);
119
120 qsnprintf(buf, sizeof(buf), "%i", errorCounter);
121 currentTestSuite->addAttribute(QTest::AI_Errors, buf);
122
123 currentTestSuite->addAttribute(QTest::AI_Time,
124 QByteArray::number(QTestLog::msecsTotalTime() / 1000, 'f').constData());
125
126 currentTestSuite->addLogElement(listOfTestcases);
127
128 // For correct indenting, make sure every testcase knows its parent
129 QTestElement *testcase = listOfTestcases;
130 while (testcase) {
131 testcase->setParent(currentTestSuite);
132 testcase = testcase->nextElement();
133 }
134
135 currentTestSuite->addLogElement(errorLogElement);
136
137 logFormatter->output(currentTestSuite);
138
139 delete currentTestSuite;
140 currentTestSuite = nullptr;
141
142 QAbstractTestLogger::stopLogging();
143}
144
145void QJUnitTestLogger::enterTestFunction(const char *function)
146{
147 currentLogElement = new QTestElement(QTest::LET_TestCase);
148 currentLogElement->addAttribute(QTest::AI_Name, function);
149 currentLogElement->addToList(&listOfTestcases);
150
151 // The element will be deleted when the suite is deleted
152
153 ++testCounter;
154}
155
156void QJUnitTestLogger::leaveTestFunction()
157{
158 currentLogElement->addAttribute(QTest::AI_Time,
159 QByteArray::number(QTestLog::msecsFunctionTime() / 1000, 'f').constData());
160}
161
162void QJUnitTestLogger::addIncident(IncidentTypes type, const char *description,
163 const char *file, int line)
164{
165 const char *typeBuf = nullptr;
166 char buf[100];
167
168 switch (type) {
169 case QAbstractTestLogger::XPass:
170 ++failureCounter;
171 typeBuf = "xpass";
172 break;
173 case QAbstractTestLogger::Pass:
174 typeBuf = "pass";
175 break;
176 case QAbstractTestLogger::XFail:
177 typeBuf = "xfail";
178 break;
179 case QAbstractTestLogger::Fail:
180 ++failureCounter;
181 typeBuf = "fail";
182 break;
183 case QAbstractTestLogger::BlacklistedPass:
184 typeBuf = "bpass";
185 break;
186 case QAbstractTestLogger::BlacklistedFail:
187 ++failureCounter;
188 typeBuf = "bfail";
189 break;
190 case QAbstractTestLogger::BlacklistedXPass:
191 typeBuf = "bxpass";
192 break;
193 case QAbstractTestLogger::BlacklistedXFail:
194 ++failureCounter;
195 typeBuf = "bxfail";
196 break;
197 default:
198 typeBuf = "??????";
199 break;
200 }
201
202 if (type == QAbstractTestLogger::Fail || type == QAbstractTestLogger::XPass) {
203 QTestElement *failureElement = new QTestElement(QTest::LET_Failure);
204 failureElement->addAttribute(QTest::AI_Result, typeBuf);
205 if (file)
206 failureElement->addAttribute(QTest::AI_File, file);
207 else
208 failureElement->addAttribute(QTest::AI_File, "");
209 qsnprintf(buf, sizeof(buf), "%i", line);
210 failureElement->addAttribute(QTest::AI_Line, buf);
211 failureElement->addAttribute(QTest::AI_Description, description);
212 addTag(failureElement);
213 currentLogElement->addLogElement(failureElement);
214 }
215
216 /*
217 Only one result can be shown for the whole testfunction.
218 Check if we currently have a result, and if so, overwrite it
219 iff the new result is worse.
220 */
221 QTestElementAttribute* resultAttr =
222 const_cast<QTestElementAttribute*>(currentLogElement->attribute(QTest::AI_Result));
223 if (resultAttr) {
224 const char* oldResult = resultAttr->value();
225 bool overwrite = false;
226 if (!strcmp(oldResult, "pass")) {
227 overwrite = true;
228 }
229 else if (!strcmp(oldResult, "bpass") || !strcmp(oldResult, "bxfail")) {
230 overwrite = (type == QAbstractTestLogger::XPass || type == QAbstractTestLogger::Fail) || (type == QAbstractTestLogger::XFail)
231 || (type == QAbstractTestLogger::BlacklistedFail) || (type == QAbstractTestLogger::BlacklistedXPass);
232 }
233 else if (!strcmp(oldResult, "bfail") || !strcmp(oldResult, "bxpass")) {
234 overwrite = (type == QAbstractTestLogger::XPass || type == QAbstractTestLogger::Fail) || (type == QAbstractTestLogger::XFail);
235 }
236 else if (!strcmp(oldResult, "xfail")) {
237 overwrite = (type == QAbstractTestLogger::XPass || type == QAbstractTestLogger::Fail);
238 }
239 else if (!strcmp(oldResult, "xpass")) {
240 overwrite = (type == QAbstractTestLogger::Fail);
241 }
242 if (overwrite) {
243 resultAttr->setPair(QTest::AI_Result, typeBuf);
244 }
245 }
246 else {
247 currentLogElement->addAttribute(QTest::AI_Result, typeBuf);
248 }
249
250 if (file)
251 currentLogElement->addAttribute(QTest::AI_File, file);
252 else
253 currentLogElement->addAttribute(QTest::AI_File, "");
254
255 qsnprintf(buf, sizeof(buf), "%i", line);
256 currentLogElement->addAttribute(QTest::AI_Line, buf);
257
258 /*
259 Since XFAIL does not add a failure to the testlog in junitxml, add a message, so we still
260 have some information about the expected failure.
261 */
262 if (type == QAbstractTestLogger::XFail) {
263 QJUnitTestLogger::addMessage(QAbstractTestLogger::Info, QString::fromUtf8(description), file, line);
264 }
265}
266
267void QJUnitTestLogger::addBenchmarkResult(const QBenchmarkResult &result)
268{
269 QTestElement *benchmarkElement = new QTestElement(QTest::LET_Benchmark);
270
271 benchmarkElement->addAttribute(
272 QTest::AI_Metric,
273 QTest::benchmarkMetricName(result.metric));
274 benchmarkElement->addAttribute(QTest::AI_Tag, result.context.tag.toUtf8().data());
275
276 const qreal valuePerIteration = qreal(result.value) / qreal(result.iterations);
277 benchmarkElement->addAttribute(QTest::AI_Value, QByteArray::number(valuePerIteration).constData());
278
279 char buf[100];
280 qsnprintf(buf, sizeof(buf), "%i", result.iterations);
281 benchmarkElement->addAttribute(QTest::AI_Iterations, buf);
282 currentLogElement->addLogElement(benchmarkElement);
283}
284
285void QJUnitTestLogger::addTag(QTestElement* element)
286{
287 const char *tag = QTestResult::currentDataTag();
288 const char *gtag = QTestResult::currentGlobalDataTag();
289 const char *filler = (tag && gtag) ? ":" : "";
290 if ((!tag || !tag[0]) && (!gtag || !gtag[0])) {
291 return;
292 }
293
294 if (!tag) {
295 tag = "";
296 }
297 if (!gtag) {
298 gtag = "";
299 }
300
301 QTestCharBuffer buf;
302 QTest::qt_asprintf(&buf, "%s%s%s", gtag, filler, tag);
303 element->addAttribute(QTest::AI_Tag, buf.constData());
304}
305
306void QJUnitTestLogger::addMessage(MessageTypes type, const QString &message, const char *file, int line)
307{
308 QTestElement *errorElement = new QTestElement(QTest::LET_Error);
309 const char *typeBuf = nullptr;
310
311 switch (type) {
312 case QAbstractTestLogger::Warn:
313 typeBuf = "warn";
314 break;
315 case QAbstractTestLogger::QSystem:
316 typeBuf = "system";
317 break;
318 case QAbstractTestLogger::QDebug:
319 typeBuf = "qdebug";
320 break;
321 case QAbstractTestLogger::QInfo:
322 typeBuf = "qinfo";
323 break;
324 case QAbstractTestLogger::QWarning:
325 typeBuf = "qwarn";
326 break;
327 case QAbstractTestLogger::QFatal:
328 typeBuf = "qfatal";
329 break;
330 case QAbstractTestLogger::Skip:
331 typeBuf = "skip";
332 break;
333 case QAbstractTestLogger::Info:
334 typeBuf = "info";
335 break;
336 default:
337 typeBuf = "??????";
338 break;
339 }
340
341 errorElement->addAttribute(QTest::AI_Type, typeBuf);
342 errorElement->addAttribute(QTest::AI_Description, message.toUtf8().constData());
343 addTag(errorElement);
344
345 if (file)
346 errorElement->addAttribute(QTest::AI_File, file);
347 else
348 errorElement->addAttribute(QTest::AI_File, "");
349
350 char buf[100];
351 qsnprintf(buf, sizeof(buf), "%i", line);
352 errorElement->addAttribute(QTest::AI_Line, buf);
353
354 currentLogElement->addLogElement(errorElement);
355 ++errorCounter;
356
357 // Also add the message to the system error log (i.e. stderr), if one exists
358 if (errorLogElement) {
359 QTestElement *systemErrorElement = new QTestElement(QTest::LET_Error);
360 systemErrorElement->addAttribute(QTest::AI_Description, message.toUtf8().constData());
361 errorLogElement->addLogElement(systemErrorElement);
362 }
363}
364
365QT_END_NAMESPACE
366
367