Qt 4.8
Public Types | Public Functions | Properties | List of all members
QTestLogger Class Reference

#include <qtestlogger_p.h>

Inheritance diagram for QTestLogger:
QAbstractTestLogger

Public Types

enum  TestLoggerFormat { TLF_XML = 0, TLF_LightXml = 1, TLF_XunitXml = 2 }
 
- Public Types inherited from QAbstractTestLogger
enum  IncidentTypes { Pass, XFail, Fail, XPass }
 
enum  MessageTypes {
  Warn, QWarning, QDebug, QSystem,
  QFatal, Skip, Info
}
 

Public Functions

void addBenchmarkResult (const QBenchmarkResult &result)
 
void addIncident (IncidentTypes type, const char *description, const char *file=0, int line=0)
 
void addMessage (MessageTypes type, const char *message, const char *file=0, int line=0)
 
void addTag (QTestElement *element)
 
void enterTestFunction (const char *function)
 
int errorCount () const
 
int failureCount () const
 
bool hasRandomSeed () const
 
int infoCount () const
 
void leaveTestFunction ()
 
TestLoggerFormat logFormat ()
 
int passCount () const
 
int qdebugCount () const
 
int qfatalCount () const
 
 QTestLogger (int fm=0)
 
int qwarnCount () const
 
unsigned int randomSeed () const
 
void registerRandomSeed (unsigned int seed)
 
void setLogFormat (TestLoggerFormat fm)
 
int skipCount () const
 
void startLogging ()
 
void stopLogging ()
 
int systemCount () const
 
int warningCount () const
 
 ~QTestLogger ()
 
- Public Functions inherited from QAbstractTestLogger
 QAbstractTestLogger ()
 
virtual ~QAbstractTestLogger ()
 

Properties

QTestElementcurrentLogElement
 
int errorCounter
 
QTestElementerrorLogElement
 
int failureCounter
 
QTestFileLoggerfilelogger
 
TestLoggerFormat format
 
bool hasRandomSeed_
 
int infoCounter
 
QTestElementlistOfTestcases
 
QTestBasicStreamerlogFormatter
 
int passCounter
 
int qdebugCounter
 
int qfatalCounter
 
int qwarnCounter
 
unsigned int randomSeed_
 
int skipCounter
 
int systemCounter
 
int testCounter
 
int warningCounter
 

Additional Inherited Members

- Static Public Functions inherited from QAbstractTestLogger
static bool isTtyOutput ()
 
static void outputString (const char *msg)
 

Detailed Description

Definition at line 64 of file qtestlogger_p.h.

Enumerations

◆ TestLoggerFormat

Enumerator
TLF_XML 
TLF_LightXml 
TLF_XunitXml 

Definition at line 70 of file qtestlogger_p.h.

Constructors and Destructors

◆ QTestLogger()

QTestLogger::QTestLogger ( int  fm = 0)

Definition at line 57 of file qtestlogger.cpp.

60  testCounter(0), passCounter(0),
65  infoCounter(0), randomSeed_(0),
66  hasRandomSeed_(false)
67 {
68 }
bool hasRandomSeed_
QTestFileLogger * filelogger
unsigned int randomSeed_
QTestBasicStreamer * logFormatter
QTestElement * errorLogElement
TestLoggerFormat format
QTestElement * currentLogElement
QTestElement * listOfTestcases

◆ ~QTestLogger()

QTestLogger::~QTestLogger ( )

Definition at line 70 of file qtestlogger.cpp.

71 {
72  if(format == TLF_XunitXml)
73  delete currentLogElement;
74  else
75  delete listOfTestcases;
76 
77  delete logFormatter;
78  delete filelogger;
79 }
QTestFileLogger * filelogger
QTestBasicStreamer * logFormatter
QTestElement * currentLogElement
QTestElement * listOfTestcases

Functions

◆ addBenchmarkResult()

void QTestLogger::addBenchmarkResult ( const QBenchmarkResult result)
virtual

Implements QAbstractTestLogger.

Definition at line 271 of file qtestlogger.cpp.

272 {
273  QTestElement *benchmarkElement = new QTestElement(QTest::LET_Benchmark);
274 // printf("element %i", benchmarkElement->elementType());
275 
276  benchmarkElement->addAttribute(
279  benchmarkElement->addAttribute(QTest::AI_Tag, result.context.tag.toAscii().data());
280  benchmarkElement->addAttribute(QTest::AI_Value, QByteArray::number(result.value).constData());
281 
282  char buf[100];
283  QTest::qt_snprintf(buf, sizeof(buf), "%i", result.iterations);
284  benchmarkElement->addAttribute(QTest::AI_Iterations, buf);
285  currentLogElement->addLogElement(benchmarkElement);
286 }
QTest::QBenchmarkMetric metric
Definition: qbenchmark_p.h:100
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
bool addLogElement(QTestElement *element)
void addAttribute(const QTest::AttributeIndex index, const char *value)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int Q_TESTLIB_EXPORT qt_snprintf(char *str, int size, const char *format,...)
Definition: qtestcase.cpp:961
const char * benchmarkMetricName(QBenchmarkMetric metric)
QByteArray toAscii() const Q_REQUIRED_RESULT
Returns an 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4014
static QBenchmarkTestMethodData * current
Definition: qbenchmark_p.h:167
QBenchmarkContext context
Definition: qbenchmark_p.h:97
static QByteArray number(int, int base=10)
Returns a byte array containing the string equivalent of the number n to base base (10 by default)...
QTestElement * currentLogElement

◆ addIncident()

void QTestLogger::addIncident ( IncidentTypes  type,
const char *  description,
const char *  file = 0,
int  line = 0 
)
virtual

Implements QAbstractTestLogger.

Definition at line 184 of file qtestlogger.cpp.

186 {
187  const char *typeBuf = 0;
188  char buf[100];
189 
190  switch (type) {
192  ++failureCounter;
193  typeBuf = "xpass";
194  break;
196  ++passCounter;
197  typeBuf = "pass";
198  break;
200  ++passCounter;
201  typeBuf = "xfail";
202  break;
204  ++failureCounter;
205  typeBuf = "fail";
206  break;
207  default:
208  typeBuf = "??????";
209  break;
210  }
211 
214  QTestElement *failureElement = new QTestElement(QTest::LET_Failure);
215  failureElement->addAttribute(QTest::AI_Result, typeBuf);
216  if(file)
217  failureElement->addAttribute(QTest::AI_File, file);
218  else
219  failureElement->addAttribute(QTest::AI_File, "");
220  QTest::qt_snprintf(buf, sizeof(buf), "%i", line);
221  failureElement->addAttribute(QTest::AI_Line, buf);
222  failureElement->addAttribute(QTest::AI_Description, description);
223  addTag(failureElement);
224  currentLogElement->addLogElement(failureElement);
225  }
226 
227  /*
228  Only one result can be shown for the whole testfunction.
229  Check if we currently have a result, and if so, overwrite it
230  iff the new result is worse.
231  */
232  QTestElementAttribute* resultAttr =
234  if (resultAttr) {
235  const char* oldResult = resultAttr->value();
236  bool overwrite = false;
237  if (!strcmp(oldResult, "pass")) {
238  overwrite = true;
239  }
240  else if (!strcmp(oldResult, "xfail")) {
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 {
252  }
253 
254  if(file)
256  else
258 
259  QTest::qt_snprintf(buf, sizeof(buf), "%i", line);
261 
262  /*
263  Since XFAIL does not add a failure to the testlog in xunitxml, add a message, so we still
264  have some information about the expected failure.
265  */
267  QTestLogger::addMessage(QAbstractTestLogger::Info, description, file, line);
268  }
269 }
int type
Definition: qmetatype.cpp:239
bool setPair(QTest::AttributeIndex attributeIndex, const char *value)
void addTag(QTestElement *element)
const char * value() const
bool addLogElement(QTestElement *element)
void addAttribute(const QTest::AttributeIndex index, const char *value)
int Q_TESTLIB_EXPORT qt_snprintf(char *str, int size, const char *format,...)
Definition: qtestcase.cpp:961
const QTestElementAttribute * attribute(QTest::AttributeIndex index) const
void addMessage(MessageTypes type, const char *message, const char *file=0, int line=0)
QTestElement * currentLogElement

◆ addMessage()

void QTestLogger::addMessage ( MessageTypes  type,
const char *  message,
const char *  file = 0,
int  line = 0 
)
virtual

Implements QAbstractTestLogger.

Definition at line 309 of file qtestlogger.cpp.

Referenced by addIncident().

310 {
311  QTestElement *errorElement = new QTestElement(QTest::LET_Error);
312  const char *typeBuf = 0;
313 
314  switch (type) {
316  ++warningCounter;
317  typeBuf = "warn";
318  break;
320  ++systemCounter;
321  typeBuf = "system";
322  break;
324  ++qdebugCounter;
325  typeBuf = "qdebug";
326  break;
328  ++qwarnCounter;
329  typeBuf = "qwarn";
330  break;
332  ++qfatalCounter;
333  typeBuf = "qfatal";
334  break;
336  ++skipCounter;
337  typeBuf = "skip";
338  break;
340  ++infoCounter;
341  typeBuf = "info";
342  break;
343  default:
344  typeBuf = "??????";
345  break;
346  }
347 
348  errorElement->addAttribute(QTest::AI_Type, typeBuf);
349  errorElement->addAttribute(QTest::AI_Description, message);
350  addTag(errorElement);
351 
352  if(file)
353  errorElement->addAttribute(QTest::AI_File, file);
354  else
355  errorElement->addAttribute(QTest::AI_File, "");
356 
357  char buf[100];
358  QTest::qt_snprintf(buf, sizeof(buf), "%i", line);
359  errorElement->addAttribute(QTest::AI_Line, buf);
360 
361  currentLogElement->addLogElement(errorElement);
362  ++errorCounter;
363 
364  // Also add the message to the system error log (i.e. stderr), if one exists
365  if (errorLogElement) {
366  QTestElement *systemErrorElement = new QTestElement(QTest::LET_Error);
367  systemErrorElement->addAttribute(QTest::AI_Description, message);
368  errorLogElement->addLogElement(systemErrorElement);
369  }
370 }
int type
Definition: qmetatype.cpp:239
void addTag(QTestElement *element)
bool addLogElement(QTestElement *element)
void addAttribute(const QTest::AttributeIndex index, const char *value)
int Q_TESTLIB_EXPORT qt_snprintf(char *str, int size, const char *format,...)
Definition: qtestcase.cpp:961
QTestElement * errorLogElement
QTestElement * currentLogElement

◆ addTag()

void QTestLogger::addTag ( QTestElement element)

Definition at line 288 of file qtestlogger.cpp.

Referenced by addIncident(), and addMessage().

289 {
290  const char *tag = QTestResult::currentDataTag();
291  const char *gtag = QTestResult::currentGlobalDataTag();
292  const char *filler = (tag && gtag) ? ":" : "";
293  if ((!tag || !tag[0]) && (!gtag || !gtag[0])) {
294  return;
295  }
296 
297  if (!tag) {
298  tag = "";
299  }
300  if (!gtag) {
301  gtag = "";
302  }
303 
304  QTestCharBuffer buf;
305  QTest::qt_asprintf(&buf, "%s%s%s", gtag, filler, tag);
306  element->addAttribute(QTest::AI_Tag, buf.constData());
307 }
int qt_asprintf(QTestCharBuffer *str, const char *format,...)
void addAttribute(const QTest::AttributeIndex index, const char *value)
const char * constData() const
static const char * currentGlobalDataTag()
static const char * currentDataTag()

◆ enterTestFunction()

void QTestLogger::enterTestFunction ( const char *  function)
virtual

Implements QAbstractTestLogger.

Definition at line 167 of file qtestlogger.cpp.

168 {
169  char buf[1024];
170  QTest::qt_snprintf(buf, sizeof(buf), "Entered test-function: %s\n", function);
171  filelogger->flush(buf);
172 
176 
177  ++testCounter;
178 }
QTestFileLogger * filelogger
void addAttribute(const QTest::AttributeIndex index, const char *value)
void flush(const char *msg)
int Q_TESTLIB_EXPORT qt_snprintf(char *str, int size, const char *format,...)
Definition: qtestcase.cpp:961
void addToList(T **list)
Definition: qtestcorelist.h:93
QTestElement * currentLogElement
QTestElement * listOfTestcases

◆ errorCount()

int QTestLogger::errorCount ( ) const

Definition at line 392 of file qtestlogger.cpp.

393 {
394  return errorCounter;
395 }

◆ failureCount()

int QTestLogger::failureCount ( ) const

Definition at line 387 of file qtestlogger.cpp.

388 {
389  return failureCounter;
390 }

◆ hasRandomSeed()

bool QTestLogger::hasRandomSeed ( ) const

Definition at line 443 of file qtestlogger.cpp.

Referenced by QTestXmlStreamer::output(), and stopLogging().

444 {
445  return hasRandomSeed_;
446 }
bool hasRandomSeed_

◆ infoCount()

int QTestLogger::infoCount ( ) const

Definition at line 427 of file qtestlogger.cpp.

428 {
429  return infoCounter;
430 }

◆ leaveTestFunction()

void QTestLogger::leaveTestFunction ( )
virtual

Implements QAbstractTestLogger.

Definition at line 180 of file qtestlogger.cpp.

181 {
182 }

◆ logFormat()

QTestLogger::TestLoggerFormat QTestLogger::logFormat ( )

Definition at line 377 of file qtestlogger.cpp.

378 {
379  return format;
380 }
TestLoggerFormat format

◆ passCount()

int QTestLogger::passCount ( ) const

Definition at line 382 of file qtestlogger.cpp.

383 {
384  return passCounter;
385 }

◆ qdebugCount()

int QTestLogger::qdebugCount ( ) const

Definition at line 412 of file qtestlogger.cpp.

413 {
414  return qdebugCounter;
415 }

◆ qfatalCount()

int QTestLogger::qfatalCount ( ) const

Definition at line 422 of file qtestlogger.cpp.

423 {
424  return qfatalCounter;
425 }

◆ qwarnCount()

int QTestLogger::qwarnCount ( ) const

Definition at line 417 of file qtestlogger.cpp.

418 {
419  return qwarnCounter;
420 }

◆ randomSeed()

unsigned int QTestLogger::randomSeed ( ) const

Definition at line 438 of file qtestlogger.cpp.

Referenced by stopLogging().

439 {
440  return randomSeed_;
441 }
unsigned int randomSeed_

◆ registerRandomSeed()

void QTestLogger::registerRandomSeed ( unsigned int  seed)
virtual

Implements QAbstractTestLogger.

Definition at line 432 of file qtestlogger.cpp.

433 {
434  randomSeed_ = seed;
435  hasRandomSeed_ = true;
436 }
bool hasRandomSeed_
unsigned int randomSeed_
static unsigned int seed
Definition: qtestcase.cpp:943

◆ setLogFormat()

void QTestLogger::setLogFormat ( TestLoggerFormat  fm)

Definition at line 372 of file qtestlogger.cpp.

373 {
374  format = fm;
375 }

◆ skipCount()

int QTestLogger::skipCount ( ) const

Definition at line 402 of file qtestlogger.cpp.

403 {
404  return skipCounter;
405 }

◆ startLogging()

void QTestLogger::startLogging ( )
virtual

Reimplemented from QAbstractTestLogger.

Definition at line 81 of file qtestlogger.cpp.

82 {
83  switch(format){
84  case TLF_LightXml:{
86  filelogger->init();
87  break;
88  }case TLF_XML:{
90  filelogger->init();
91  break;
92  }case TLF_XunitXml:{
94  delete errorLogElement;
96  filelogger->init();
97  break;
98  }
99  }
100 
101  logFormatter->setLogger(this);
103 }
QTestFileLogger * filelogger
void setLogger(const QTestLogger *tstLogger)
QTestBasicStreamer * logFormatter
QTestElement * errorLogElement

◆ stopLogging()

void QTestLogger::stopLogging ( )
virtual

Reimplemented from QAbstractTestLogger.

Definition at line 105 of file qtestlogger.cpp.

106 {
107  QTestElement *iterator = listOfTestcases;
108 
109  if(format == TLF_XunitXml ){
110  char buf[10];
111 
114 
115  QTest::qt_snprintf(buf, sizeof(buf), "%i", testCounter);
117 
118  QTest::qt_snprintf(buf, sizeof(buf), "%i", failureCounter);
120 
121  QTest::qt_snprintf(buf, sizeof(buf), "%i", errorCounter);
123 
126 
127  property = new QTestElement(QTest::LET_Property);
128  property->addAttribute(QTest::AI_Name, "QTestVersion");
129  property->addAttribute(QTest::AI_PropertyValue, QTEST_VERSION_STR);
130  properties->addLogElement(property);
131 
132  property = new QTestElement(QTest::LET_Property);
133  property->addAttribute(QTest::AI_Name, "QtVersion");
134  property->addAttribute(QTest::AI_PropertyValue, qVersion());
135  properties->addLogElement(property);
136 
137  if (hasRandomSeed()) {
138  property = new QTestElement(QTest::LET_Property);
139  property->addAttribute(QTest::AI_Name, "RandomSeed");
140  QTest::qt_snprintf(buf, sizeof(buf), "%i", randomSeed());
141  property->addAttribute(QTest::AI_PropertyValue, buf);
142  properties->addLogElement(property);
143  }
144 
145  currentLogElement->addLogElement(properties);
146 
147  currentLogElement->addLogElement(iterator);
148 
149  /* For correct indenting, make sure every testcase knows its parent */
150  QTestElement* testcase = iterator;
151  while (testcase) {
152  testcase->setParent(currentLogElement);
153  testcase = testcase->nextElement();
154  }
155 
157 
159  logFormatter->output(it);
160  }else{
161  logFormatter->output(iterator);
162  }
163 
165 }
unsigned int randomSeed() const
bool hasRandomSeed() const
#define it(className, varName)
bool addLogElement(QTestElement *element)
virtual void output(QTestElement *element) const
void addAttribute(const QTest::AttributeIndex index, const char *value)
Q_CORE_EXPORT const char * qVersion()
static const char * currentTestObjectName()
QTestBasicStreamer * logFormatter
#define QTEST_VERSION_STR
Definition: qtest_global.h:78
int Q_TESTLIB_EXPORT qt_snprintf(char *str, int size, const char *format,...)
Definition: qtestcase.cpp:961
QTestElement * errorLogElement
const char * property
Definition: qwizard.cpp:138
static const QCssKnownValue properties[NumProperties - 1]
Definition: qcssparser.cpp:67
void setParent(const QTestElement *p)
QTestElement * currentLogElement
QTestElement * listOfTestcases

◆ systemCount()

int QTestLogger::systemCount ( ) const

Definition at line 407 of file qtestlogger.cpp.

408 {
409  return systemCounter;
410 }

◆ warningCount()

int QTestLogger::warningCount ( ) const

Definition at line 397 of file qtestlogger.cpp.

398 {
399  return warningCounter;
400 }

Properties

◆ currentLogElement

QTestElement* QTestLogger::currentLogElement
private

◆ errorCounter

int QTestLogger::errorCounter
private

Definition at line 119 of file qtestlogger_p.h.

Referenced by addMessage(), errorCount(), and stopLogging().

◆ errorLogElement

QTestElement* QTestLogger::errorLogElement
private

Definition at line 111 of file qtestlogger_p.h.

Referenced by addMessage(), startLogging(), and stopLogging().

◆ failureCounter

int QTestLogger::failureCounter
private

Definition at line 118 of file qtestlogger_p.h.

Referenced by addIncident(), failureCount(), and stopLogging().

◆ filelogger

QTestFileLogger* QTestLogger::filelogger
private

Definition at line 114 of file qtestlogger_p.h.

Referenced by enterTestFunction(), startLogging(), and ~QTestLogger().

◆ format

TestLoggerFormat QTestLogger::format
private

Definition at line 113 of file qtestlogger_p.h.

Referenced by logFormat().

◆ hasRandomSeed_

bool QTestLogger::hasRandomSeed_
private

Definition at line 128 of file qtestlogger_p.h.

Referenced by hasRandomSeed(), and registerRandomSeed().

◆ infoCounter

int QTestLogger::infoCounter
private

Definition at line 126 of file qtestlogger_p.h.

Referenced by addMessage(), and infoCount().

◆ listOfTestcases

QTestElement* QTestLogger::listOfTestcases
private

Definition at line 109 of file qtestlogger_p.h.

Referenced by enterTestFunction(), stopLogging(), and ~QTestLogger().

◆ logFormatter

QTestBasicStreamer* QTestLogger::logFormatter
private

Definition at line 112 of file qtestlogger_p.h.

Referenced by startLogging(), stopLogging(), and ~QTestLogger().

◆ passCounter

int QTestLogger::passCounter
private

Definition at line 117 of file qtestlogger_p.h.

Referenced by addIncident(), and passCount().

◆ qdebugCounter

int QTestLogger::qdebugCounter
private

Definition at line 123 of file qtestlogger_p.h.

Referenced by addMessage(), and qdebugCount().

◆ qfatalCounter

int QTestLogger::qfatalCounter
private

Definition at line 125 of file qtestlogger_p.h.

Referenced by addMessage(), and qfatalCount().

◆ qwarnCounter

int QTestLogger::qwarnCounter
private

Definition at line 124 of file qtestlogger_p.h.

Referenced by addMessage(), and qwarnCount().

◆ randomSeed_

unsigned int QTestLogger::randomSeed_
private

Definition at line 127 of file qtestlogger_p.h.

Referenced by randomSeed(), and registerRandomSeed().

◆ skipCounter

int QTestLogger::skipCounter
private

Definition at line 121 of file qtestlogger_p.h.

Referenced by addMessage(), and skipCount().

◆ systemCounter

int QTestLogger::systemCounter
private

Definition at line 122 of file qtestlogger_p.h.

Referenced by addMessage(), and systemCount().

◆ testCounter

int QTestLogger::testCounter
private

Definition at line 116 of file qtestlogger_p.h.

Referenced by enterTestFunction(), and stopLogging().

◆ warningCounter

int QTestLogger::warningCounter
private

Definition at line 120 of file qtestlogger_p.h.

Referenced by addMessage(), and warningCount().


The documentation for this class was generated from the following files: