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 systemOutputElement;
78 systemOutputElement = new QTestElement(QTest::LET_SystemOutput);
79 delete systemErrorElement;
80 systemErrorElement = new QTestElement(QTest::LET_SystemError);
81
82 Q_ASSERT(!currentTestSuite);
83 currentTestSuite = new QTestElement(QTest::LET_TestSuite);
84 currentTestSuite->addAttribute(QTest::AI_Name, QTestResult::currentTestObjectName());
85
86 auto localTime = QDateTime::currentDateTime();
87 auto localTimeWithUtcOffset = localTime.toOffsetFromUtc(localTime.offsetFromUtc());
88 currentTestSuite->addAttribute(QTest::AI_Timestamp,
89 localTimeWithUtcOffset.toString(Qt::ISODate).toUtf8().constData());
90
91 QTestElement *property;
92 QTestElement *properties = new QTestElement(QTest::LET_Properties);
93
94 property = new QTestElement(QTest::LET_Property);
95 property->addAttribute(QTest::AI_Name, "QTestVersion");
96 property->addAttribute(QTest::AI_PropertyValue, QTEST_VERSION_STR);
97 properties->addLogElement(property);
98
99 property = new QTestElement(QTest::LET_Property);
100 property->addAttribute(QTest::AI_Name, "QtVersion");
101 property->addAttribute(QTest::AI_PropertyValue, qVersion());
102 properties->addLogElement(property);
103
104 property = new QTestElement(QTest::LET_Property);
105 property->addAttribute(QTest::AI_Name, "QtBuild");
106 property->addAttribute(QTest::AI_PropertyValue, QLibraryInfo::build());
107 properties->addLogElement(property);
108
109 currentTestSuite->addLogElement(properties);
110}
111
112void QJUnitTestLogger::stopLogging()
113{
114 char buf[10];
115
116 qsnprintf(buf, sizeof(buf), "%i", testCounter);
117 currentTestSuite->addAttribute(QTest::AI_Tests, buf);
118
119 qsnprintf(buf, sizeof(buf), "%i", failureCounter);
120 currentTestSuite->addAttribute(QTest::AI_Failures, buf);
121
122 qsnprintf(buf, sizeof(buf), "%i", errorCounter);
123 currentTestSuite->addAttribute(QTest::AI_Errors, buf);
124
125 currentTestSuite->addAttribute(QTest::AI_Time,
126 QByteArray::number(QTestLog::msecsTotalTime() / 1000, 'f').constData());
127
128 currentTestSuite->addLogElement(listOfTestcases);
129
130 // For correct indenting, make sure every testcase knows its parent
131 QTestElement *testcase = listOfTestcases;
132 while (testcase) {
133 testcase->setParent(currentTestSuite);
134 testcase = testcase->nextElement();
135 }
136
137 if (systemOutputElement->childElements())
138 currentTestSuite->addLogElement(systemOutputElement);
139 currentTestSuite->addLogElement(systemErrorElement);
140
141 logFormatter->output(currentTestSuite);
142
143 delete currentTestSuite;
144 currentTestSuite = nullptr;
145
146 QAbstractTestLogger::stopLogging();
147}
148
149void QJUnitTestLogger::enterTestFunction(const char *function)
150{
151 currentLogElement = new QTestElement(QTest::LET_TestCase);
152 currentLogElement->addAttribute(QTest::AI_Name, function);
153 currentLogElement->addToList(&listOfTestcases);
154
155 // The element will be deleted when the suite is deleted
156
157 ++testCounter;
158}
159
160void QJUnitTestLogger::leaveTestFunction()
161{
162 currentLogElement->addAttribute(QTest::AI_Time,
163 QByteArray::number(QTestLog::msecsFunctionTime() / 1000, 'f').constData());
164}
165
166void QJUnitTestLogger::addIncident(IncidentTypes type, const char *description,
167 const char *file, int line)
168{
169 const char *typeBuf = nullptr;
170 char buf[100];
171
172 switch (type) {
173 case QAbstractTestLogger::XPass:
174 ++failureCounter;
175 typeBuf = "xpass";
176 break;
177 case QAbstractTestLogger::Pass:
178 typeBuf = "pass";
179 break;
180 case QAbstractTestLogger::XFail:
181 typeBuf = "xfail";
182 break;
183 case QAbstractTestLogger::Fail:
184 ++failureCounter;
185 typeBuf = "fail";
186 break;
187 case QAbstractTestLogger::BlacklistedPass:
188 typeBuf = "bpass";
189 break;
190 case QAbstractTestLogger::BlacklistedFail:
191 ++failureCounter;
192 typeBuf = "bfail";
193 break;
194 case QAbstractTestLogger::BlacklistedXPass:
195 typeBuf = "bxpass";
196 break;
197 case QAbstractTestLogger::BlacklistedXFail:
198 ++failureCounter;
199 typeBuf = "bxfail";
200 break;
201 default:
202 typeBuf = "??????";
203 break;
204 }
205
206 if (type == QAbstractTestLogger::Fail || type == QAbstractTestLogger::XPass) {
207 QTestElement *failureElement = new QTestElement(QTest::LET_Failure);
208 failureElement->addAttribute(QTest::AI_Result, typeBuf);
209 if (file)
210 failureElement->addAttribute(QTest::AI_File, file);
211 else
212 failureElement->addAttribute(QTest::AI_File, "");
213 qsnprintf(buf, sizeof(buf), "%i", line);
214 failureElement->addAttribute(QTest::AI_Line, buf);
215 failureElement->addAttribute(QTest::AI_Description, description);
216 addTag(failureElement);
217 currentLogElement->addLogElement(failureElement);
218 }
219
220 /*
221 Only one result can be shown for the whole testfunction.
222 Check if we currently have a result, and if so, overwrite it
223 iff the new result is worse.
224 */
225 QTestElementAttribute* resultAttr =
226 const_cast<QTestElementAttribute*>(currentLogElement->attribute(QTest::AI_Result));
227 if (resultAttr) {
228 const char* oldResult = resultAttr->value();
229 bool overwrite = false;
230 if (!strcmp(oldResult, "pass")) {
231 overwrite = true;
232 }
233 else if (!strcmp(oldResult, "bpass") || !strcmp(oldResult, "bxfail")) {
234 overwrite = (type == QAbstractTestLogger::XPass || type == QAbstractTestLogger::Fail) || (type == QAbstractTestLogger::XFail)
235 || (type == QAbstractTestLogger::BlacklistedFail) || (type == QAbstractTestLogger::BlacklistedXPass);
236 }
237 else if (!strcmp(oldResult, "bfail") || !strcmp(oldResult, "bxpass")) {
238 overwrite = (type == QAbstractTestLogger::XPass || type == QAbstractTestLogger::Fail) || (type == QAbstractTestLogger::XFail);
239 }
240 else if (!strcmp(oldResult, "xfail")) {
241 overwrite = (type == QAbstractTestLogger::XPass || type == QAbstractTestLogger::Fail);
242 }
243 else if (!strcmp(oldResult, "xpass")) {
244 overwrite = (type == QAbstractTestLogger::Fail);
245 }
246 if (overwrite) {
247 resultAttr->setPair(QTest::AI_Result, typeBuf);
248 }
249 }
250 else {
251 currentLogElement->addAttribute(QTest::AI_Result, typeBuf);
252 }
253
254 if (file)
255 currentLogElement->addAttribute(QTest::AI_File, file);
256 else
257 currentLogElement->addAttribute(QTest::AI_File, "");
258
259 qsnprintf(buf, sizeof(buf), "%i", line);
260 currentLogElement->addAttribute(QTest::AI_Line, buf);
261
262 /*
263 Since XFAIL does not add a failure to the testlog in junitxml, add a message, so we still
264 have some information about the expected failure.
265 */
266 if (type == QAbstractTestLogger::XFail) {
267 QJUnitTestLogger::addMessage(QAbstractTestLogger::Info, QString::fromUtf8(description), file, line);
268 }
269}
270
271void QJUnitTestLogger::addBenchmarkResult(const QBenchmarkResult &result)
272{
273 QTestElement *benchmarkElement = new QTestElement(QTest::LET_Benchmark);
274
275 benchmarkElement->addAttribute(
276 QTest::AI_Metric,
277 QTest::benchmarkMetricName(result.metric));
278 benchmarkElement->addAttribute(QTest::AI_Tag, result.context.tag.toUtf8().data());
279
280 const qreal valuePerIteration = qreal(result.value) / qreal(result.iterations);
281 benchmarkElement->addAttribute(QTest::AI_Value, QByteArray::number(valuePerIteration).constData());
282
283 char buf[100];
284 qsnprintf(buf, sizeof(buf), "%i", result.iterations);
285 benchmarkElement->addAttribute(QTest::AI_Iterations, buf);
286 currentLogElement->addLogElement(benchmarkElement);
287}
288
289void QJUnitTestLogger::addTag(QTestElement* element)
290{
291 const char *tag = QTestResult::currentDataTag();
292 const char *gtag = QTestResult::currentGlobalDataTag();
293 const char *filler = (tag && gtag) ? ":" : "";
294 if ((!tag || !tag[0]) && (!gtag || !gtag[0])) {
295 return;
296 }
297
298 if (!tag) {
299 tag = "";
300 }
301 if (!gtag) {
302 gtag = "";
303 }
304
305 QTestCharBuffer buf;
306 QTest::qt_asprintf(&buf, "%s%s%s", gtag, filler, tag);
307 element->addAttribute(QTest::AI_Tag, buf.constData());
308}
309
310void QJUnitTestLogger::addMessage(MessageTypes type, const QString &message, const char *file, int line)
311{
312 auto messageElement = new QTestElement(QTest::LET_Message);
313 auto systemLogElement = systemOutputElement;
314 const char *typeBuf = nullptr;
315
316 switch (type) {
317 case QAbstractTestLogger::Warn:
318 systemLogElement = systemErrorElement;
319 typeBuf = "warn";
320 break;
321 case QAbstractTestLogger::QSystem:
322 typeBuf = "system";
323 break;
324 case QAbstractTestLogger::QDebug:
325 typeBuf = "qdebug";
326 break;
327 case QAbstractTestLogger::QInfo:
328 typeBuf = "qinfo";
329 break;
330 case QAbstractTestLogger::QWarning:
331 systemLogElement = systemErrorElement;
332 typeBuf = "qwarn";
333 break;
334 case QAbstractTestLogger::QFatal:
335 systemLogElement = systemErrorElement;
336 typeBuf = "qfatal";
337 break;
338 case QAbstractTestLogger::Skip:
339 typeBuf = "skip";
340 break;
341 case QAbstractTestLogger::Info:
342 typeBuf = "info";
343 break;
344 default:
345 typeBuf = "??????";
346 break;
347 }
348
349 messageElement->addAttribute(QTest::AI_Type, typeBuf);
350 messageElement->addAttribute(QTest::AI_Description, message.toUtf8().constData());
351 addTag(messageElement);
352
353 if (file)
354 messageElement->addAttribute(QTest::AI_File, file);
355 else
356 messageElement->addAttribute(QTest::AI_File, "");
357
358 char buf[100];
359 qsnprintf(buf, sizeof(buf), "%i", line);
360 messageElement->addAttribute(QTest::AI_Line, buf);
361
362 currentLogElement->addLogElement(messageElement);
363 ++errorCounter;
364
365 // Also add the message to the system log (stdout/stderr), if one exists
366 if (systemLogElement) {
367 auto messageElement = new QTestElement(QTest::LET_Message);
368 messageElement->addAttribute(QTest::AI_Description, message.toUtf8().constData());
369 systemLogElement->addLogElement(messageElement);
370 }
371}
372
373QT_END_NAMESPACE
374
375