[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the QtTest module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
| 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at qt-info@nokia.com.
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include "qtestlogger_p.h"
|
---|
| 43 | #include "qtestelement.h"
|
---|
| 44 | #include "qtestxunitstreamer.h"
|
---|
| 45 | #include "qtestxmlstreamer.h"
|
---|
| 46 | #include "qtestlightxmlstreamer.h"
|
---|
| 47 | #include "qtestfilelogger.h"
|
---|
| 48 |
|
---|
| 49 | #include "QtTest/qtestcase.h"
|
---|
| 50 | #include "QtTest/private/qtestresult_p.h"
|
---|
| 51 | #include "QtTest/private/qbenchmark_p.h"
|
---|
| 52 |
|
---|
| 53 | #include <string.h>
|
---|
| 54 |
|
---|
| 55 | QT_BEGIN_NAMESPACE
|
---|
| 56 |
|
---|
| 57 | QTestLogger::QTestLogger(int fm)
|
---|
| 58 | :listOfTestcases(0), currentLogElement(0), errorLogElement(0),
|
---|
| 59 | logFormatter(0), format( (TestLoggerFormat)fm ), filelogger(new QTestFileLogger),
|
---|
| 60 | testCounter(0), passCounter(0),
|
---|
| 61 | failureCounter(0), errorCounter(0),
|
---|
| 62 | warningCounter(0), skipCounter(0),
|
---|
| 63 | systemCounter(0), qdebugCounter(0),
|
---|
| 64 | qwarnCounter(0), qfatalCounter(0),
|
---|
| 65 | infoCounter(0)
|
---|
| 66 | {
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | QTestLogger::~QTestLogger()
|
---|
| 70 | {
|
---|
| 71 | if(format == TLF_XunitXml)
|
---|
| 72 | delete currentLogElement;
|
---|
| 73 | else
|
---|
| 74 | delete listOfTestcases;
|
---|
| 75 |
|
---|
| 76 | delete logFormatter;
|
---|
| 77 | delete filelogger;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | void QTestLogger::startLogging()
|
---|
| 81 | {
|
---|
| 82 | switch(format){
|
---|
| 83 | case TLF_LightXml:{
|
---|
| 84 | logFormatter = new QTestLightXmlStreamer;
|
---|
| 85 | filelogger->init();
|
---|
| 86 | break;
|
---|
| 87 | }case TLF_XML:{
|
---|
| 88 | logFormatter = new QTestXmlStreamer;
|
---|
| 89 | filelogger->init();
|
---|
| 90 | break;
|
---|
| 91 | }case TLF_XunitXml:{
|
---|
| 92 | logFormatter = new QTestXunitStreamer;
|
---|
| 93 | delete errorLogElement;
|
---|
| 94 | errorLogElement = new QTestElement(QTest::LET_SystemError);
|
---|
| 95 | filelogger->init();
|
---|
| 96 | break;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | logFormatter->setLogger(this);
|
---|
| 101 | logFormatter->startStreaming();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | void QTestLogger::stopLogging()
|
---|
| 105 | {
|
---|
| 106 | QTestElement *iterator = listOfTestcases;
|
---|
| 107 |
|
---|
| 108 | if(format == TLF_XunitXml ){
|
---|
| 109 | char buf[10];
|
---|
| 110 |
|
---|
| 111 | currentLogElement = new QTestElement(QTest::LET_TestSuite);
|
---|
| 112 | currentLogElement->addAttribute(QTest::AI_Name, QTestResult::currentTestObjectName());
|
---|
| 113 |
|
---|
| 114 | QTest::qt_snprintf(buf, sizeof(buf), "%i", testCounter);
|
---|
| 115 | currentLogElement->addAttribute(QTest::AI_Tests, buf);
|
---|
| 116 |
|
---|
| 117 | QTest::qt_snprintf(buf, sizeof(buf), "%i", failureCounter);
|
---|
| 118 | currentLogElement->addAttribute(QTest::AI_Failures, buf);
|
---|
| 119 |
|
---|
| 120 | QTest::qt_snprintf(buf, sizeof(buf), "%i", errorCounter);
|
---|
| 121 | currentLogElement->addAttribute(QTest::AI_Errors, buf);
|
---|
| 122 |
|
---|
| 123 | QTestElement *property;
|
---|
| 124 | QTestElement *properties = new QTestElement(QTest::LET_Properties);
|
---|
| 125 |
|
---|
| 126 | property = new QTestElement(QTest::LET_Property);
|
---|
| 127 | property->addAttribute(QTest::AI_Name, "QTestVersion");
|
---|
| 128 | property->addAttribute(QTest::AI_PropertyValue, QTEST_VERSION_STR);
|
---|
| 129 | properties->addLogElement(property);
|
---|
| 130 |
|
---|
| 131 | property = new QTestElement(QTest::LET_Property);
|
---|
| 132 | property->addAttribute(QTest::AI_Name, "QtVersion");
|
---|
| 133 | property->addAttribute(QTest::AI_PropertyValue, qVersion());
|
---|
| 134 | properties->addLogElement(property);
|
---|
| 135 |
|
---|
| 136 | currentLogElement->addLogElement(properties);
|
---|
| 137 |
|
---|
| 138 | currentLogElement->addLogElement(iterator);
|
---|
| 139 |
|
---|
| 140 | /* For correct indenting, make sure every testcase knows its parent */
|
---|
| 141 | QTestElement* testcase = iterator;
|
---|
| 142 | while (testcase) {
|
---|
| 143 | testcase->setParent(currentLogElement);
|
---|
| 144 | testcase = testcase->nextElement();
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | currentLogElement->addLogElement(errorLogElement);
|
---|
| 148 |
|
---|
| 149 | QTestElement *it = currentLogElement;
|
---|
| 150 | logFormatter->output(it);
|
---|
| 151 | }else{
|
---|
| 152 | logFormatter->output(iterator);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | logFormatter->stopStreaming();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | void QTestLogger::enterTestFunction(const char *function)
|
---|
| 159 | {
|
---|
| 160 | char buf[1024];
|
---|
| 161 | QTest::qt_snprintf(buf, sizeof(buf), "Entered test-function: %s\n", function);
|
---|
| 162 | filelogger->flush(buf);
|
---|
| 163 |
|
---|
| 164 | currentLogElement = new QTestElement(QTest::LET_TestCase);
|
---|
| 165 | currentLogElement->addAttribute(QTest::AI_Name, function);
|
---|
| 166 | currentLogElement->addToList(&listOfTestcases);
|
---|
| 167 |
|
---|
| 168 | ++testCounter;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | void QTestLogger::leaveTestFunction()
|
---|
| 172 | {
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | void QTestLogger::addIncident(IncidentTypes type, const char *description,
|
---|
| 176 | const char *file, int line)
|
---|
| 177 | {
|
---|
| 178 | const char *typeBuf = 0;
|
---|
| 179 | char buf[100];
|
---|
| 180 |
|
---|
| 181 | switch (type) {
|
---|
| 182 | case QAbstractTestLogger::XPass:
|
---|
| 183 | ++failureCounter;
|
---|
| 184 | typeBuf = "xpass";
|
---|
| 185 | break;
|
---|
| 186 | case QAbstractTestLogger::Pass:
|
---|
| 187 | ++passCounter;
|
---|
| 188 | typeBuf = "pass";
|
---|
| 189 | break;
|
---|
| 190 | case QAbstractTestLogger::XFail:
|
---|
| 191 | ++passCounter;
|
---|
| 192 | typeBuf = "xfail";
|
---|
| 193 | break;
|
---|
| 194 | case QAbstractTestLogger::Fail:
|
---|
| 195 | ++failureCounter;
|
---|
| 196 | typeBuf = "fail";
|
---|
| 197 | break;
|
---|
| 198 | default:
|
---|
| 199 | typeBuf = "??????";
|
---|
| 200 | break;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | if (type == QAbstractTestLogger::Fail || type == QAbstractTestLogger::XPass
|
---|
| 204 | || ((format != TLF_XunitXml) && (type == QAbstractTestLogger::XFail))) {
|
---|
| 205 | QTestElement *failureElement = new QTestElement(QTest::LET_Failure);
|
---|
| 206 | failureElement->addAttribute(QTest::AI_Result, typeBuf);
|
---|
| 207 | if(file)
|
---|
| 208 | failureElement->addAttribute(QTest::AI_File, file);
|
---|
| 209 | else
|
---|
| 210 | failureElement->addAttribute(QTest::AI_File, "");
|
---|
| 211 | QTest::qt_snprintf(buf, sizeof(buf), "%i", line);
|
---|
| 212 | failureElement->addAttribute(QTest::AI_Line, buf);
|
---|
| 213 | failureElement->addAttribute(QTest::AI_Description, description);
|
---|
[769] | 214 | addTag(failureElement);
|
---|
[556] | 215 | currentLogElement->addLogElement(failureElement);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /*
|
---|
| 219 | Only one result can be shown for the whole testfunction.
|
---|
| 220 | Check if we currently have a result, and if so, overwrite it
|
---|
| 221 | iff the new result is worse.
|
---|
| 222 | */
|
---|
| 223 | QTestElementAttribute* resultAttr =
|
---|
| 224 | const_cast<QTestElementAttribute*>(currentLogElement->attribute(QTest::AI_Result));
|
---|
| 225 | if (resultAttr) {
|
---|
| 226 | const char* oldResult = resultAttr->value();
|
---|
| 227 | bool overwrite = false;
|
---|
| 228 | if (!strcmp(oldResult, "pass")) {
|
---|
| 229 | overwrite = true;
|
---|
| 230 | }
|
---|
| 231 | else if (!strcmp(oldResult, "xfail")) {
|
---|
| 232 | overwrite = (type == QAbstractTestLogger::XPass || type == QAbstractTestLogger::Fail);
|
---|
| 233 | }
|
---|
| 234 | else if (!strcmp(oldResult, "xpass")) {
|
---|
| 235 | overwrite = (type == QAbstractTestLogger::Fail);
|
---|
| 236 | }
|
---|
| 237 | if (overwrite) {
|
---|
| 238 | resultAttr->setPair(QTest::AI_Result, typeBuf);
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | else {
|
---|
| 242 | currentLogElement->addAttribute(QTest::AI_Result, typeBuf);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | if(file)
|
---|
| 246 | currentLogElement->addAttribute(QTest::AI_File, file);
|
---|
| 247 | else
|
---|
| 248 | currentLogElement->addAttribute(QTest::AI_File, "");
|
---|
| 249 |
|
---|
| 250 | QTest::qt_snprintf(buf, sizeof(buf), "%i", line);
|
---|
| 251 | currentLogElement->addAttribute(QTest::AI_Line, buf);
|
---|
| 252 |
|
---|
| 253 | /*
|
---|
| 254 | Since XFAIL does not add a failure to the testlog in xunitxml, add a message, so we still
|
---|
| 255 | have some information about the expected failure.
|
---|
| 256 | */
|
---|
| 257 | if (format == TLF_XunitXml && type == QAbstractTestLogger::XFail) {
|
---|
| 258 | QTestLogger::addMessage(QAbstractTestLogger::Info, description, file, line);
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | void QTestLogger::addBenchmarkResult(const QBenchmarkResult &result)
|
---|
| 263 | {
|
---|
| 264 | QTestElement *benchmarkElement = new QTestElement(QTest::LET_Benchmark);
|
---|
| 265 | // printf("element %i", benchmarkElement->elementType());
|
---|
| 266 |
|
---|
[846] | 267 | benchmarkElement->addAttribute(
|
---|
| 268 | QTest::AI_Metric,
|
---|
| 269 | QTest::benchmarkMetricName(QBenchmarkTestMethodData::current->result.metric));
|
---|
[556] | 270 | benchmarkElement->addAttribute(QTest::AI_Tag, result.context.tag.toAscii().data());
|
---|
| 271 | benchmarkElement->addAttribute(QTest::AI_Value, QByteArray::number(result.value).constData());
|
---|
| 272 |
|
---|
| 273 | char buf[100];
|
---|
| 274 | QTest::qt_snprintf(buf, sizeof(buf), "%i", result.iterations);
|
---|
| 275 | benchmarkElement->addAttribute(QTest::AI_Iterations, buf);
|
---|
| 276 | currentLogElement->addLogElement(benchmarkElement);
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[769] | 279 | void QTestLogger::addTag(QTestElement* element)
|
---|
| 280 | {
|
---|
| 281 | const char *tag = QTestResult::currentDataTag();
|
---|
| 282 | const char *gtag = QTestResult::currentGlobalDataTag();
|
---|
| 283 | const char *filler = (tag && gtag) ? ":" : "";
|
---|
| 284 | if ((!tag || !tag[0]) && (!gtag || !gtag[0])) {
|
---|
| 285 | return;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | if (!tag) {
|
---|
| 289 | tag = "";
|
---|
| 290 | }
|
---|
| 291 | if (!gtag) {
|
---|
| 292 | gtag = "";
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | QTestCharBuffer buf;
|
---|
| 296 | QTest::qt_asprintf(&buf, "%s%s%s", gtag, filler, tag);
|
---|
| 297 | element->addAttribute(QTest::AI_Tag, buf.constData());
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[556] | 300 | void QTestLogger::addMessage(MessageTypes type, const char *message, const char *file, int line)
|
---|
| 301 | {
|
---|
| 302 | QTestElement *errorElement = new QTestElement(QTest::LET_Error);
|
---|
| 303 | const char *typeBuf = 0;
|
---|
| 304 |
|
---|
| 305 | switch (type) {
|
---|
| 306 | case QAbstractTestLogger::Warn:
|
---|
| 307 | ++warningCounter;
|
---|
| 308 | typeBuf = "warn";
|
---|
| 309 | break;
|
---|
| 310 | case QAbstractTestLogger::QSystem:
|
---|
| 311 | ++systemCounter;
|
---|
| 312 | typeBuf = "system";
|
---|
| 313 | break;
|
---|
| 314 | case QAbstractTestLogger::QDebug:
|
---|
| 315 | ++qdebugCounter;
|
---|
| 316 | typeBuf = "qdebug";
|
---|
| 317 | break;
|
---|
| 318 | case QAbstractTestLogger::QWarning:
|
---|
| 319 | ++qwarnCounter;
|
---|
[769] | 320 | typeBuf = "qwarn";
|
---|
[556] | 321 | break;
|
---|
| 322 | case QAbstractTestLogger::QFatal:
|
---|
| 323 | ++qfatalCounter;
|
---|
| 324 | typeBuf = "qfatal";
|
---|
| 325 | break;
|
---|
| 326 | case QAbstractTestLogger::Skip:
|
---|
| 327 | ++skipCounter;
|
---|
| 328 | typeBuf = "skip";
|
---|
| 329 | break;
|
---|
| 330 | case QAbstractTestLogger::Info:
|
---|
| 331 | ++infoCounter;
|
---|
| 332 | typeBuf = "info";
|
---|
| 333 | break;
|
---|
| 334 | default:
|
---|
| 335 | typeBuf = "??????";
|
---|
| 336 | break;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | errorElement->addAttribute(QTest::AI_Type, typeBuf);
|
---|
| 340 | errorElement->addAttribute(QTest::AI_Description, message);
|
---|
[769] | 341 | addTag(errorElement);
|
---|
[556] | 342 |
|
---|
| 343 | if(file)
|
---|
| 344 | errorElement->addAttribute(QTest::AI_File, file);
|
---|
| 345 | else
|
---|
| 346 | errorElement->addAttribute(QTest::AI_File, "");
|
---|
| 347 |
|
---|
| 348 | char buf[100];
|
---|
| 349 | QTest::qt_snprintf(buf, sizeof(buf), "%i", line);
|
---|
| 350 | errorElement->addAttribute(QTest::AI_Line, buf);
|
---|
| 351 |
|
---|
| 352 | currentLogElement->addLogElement(errorElement);
|
---|
| 353 | ++errorCounter;
|
---|
| 354 |
|
---|
| 355 | // Also add the message to the system error log (i.e. stderr), if one exists
|
---|
| 356 | if (errorLogElement) {
|
---|
| 357 | QTestElement *systemErrorElement = new QTestElement(QTest::LET_Error);
|
---|
| 358 | systemErrorElement->addAttribute(QTest::AI_Description, message);
|
---|
| 359 | errorLogElement->addLogElement(systemErrorElement);
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | void QTestLogger::setLogFormat(TestLoggerFormat fm)
|
---|
| 364 | {
|
---|
| 365 | format = fm;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | QTestLogger::TestLoggerFormat QTestLogger::logFormat()
|
---|
| 369 | {
|
---|
| 370 | return format;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | int QTestLogger::passCount() const
|
---|
| 374 | {
|
---|
| 375 | return passCounter;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | int QTestLogger::failureCount() const
|
---|
| 379 | {
|
---|
| 380 | return failureCounter;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | int QTestLogger::errorCount() const
|
---|
| 384 | {
|
---|
| 385 | return errorCounter;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | int QTestLogger::warningCount() const
|
---|
| 389 | {
|
---|
| 390 | return warningCounter;
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | int QTestLogger::skipCount() const
|
---|
| 394 | {
|
---|
| 395 | return skipCounter;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | int QTestLogger::systemCount() const
|
---|
| 399 | {
|
---|
| 400 | return systemCounter;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | int QTestLogger::qdebugCount() const
|
---|
| 404 | {
|
---|
| 405 | return qdebugCounter;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | int QTestLogger::qwarnCount() const
|
---|
| 409 | {
|
---|
| 410 | return qwarnCounter;
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | int QTestLogger::qfatalCount() const
|
---|
| 414 | {
|
---|
| 415 | return qfatalCounter;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | int QTestLogger::infoCount() const
|
---|
| 419 | {
|
---|
| 420 | return infoCounter;
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | QT_END_NAMESPACE
|
---|
| 424 |
|
---|