Search code examples
qtunit-testingjenkinsconsoleqdebug

How can see qDebug and Unit test Macro (QCOMPARE etc) result in jenkins console


I try to do that unit test results are printed in jenkins console. But I can not do. I am thinking that jenkins build console can not print qDebug. And I tried it can not print qdebug. But std::cout is working. How can I proceed?

Thank you. I tried that you said. But It can not work. It gives error. error: C3861: 'qInstallMsgHandler': identifier not found

//! [0]

#include <QtTest/QtTest>
#include <qapplication.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <QDebug>
void myMessageOutput(QtMsgType type, const char *msg)
{
 switch (type) {
 case QtDebugMsg:
     fprintf(stderr, "Debug: %s\n", msg);
     break;
 case QtWarningMsg:
     fprintf(stderr, "Warning: %s\n", msg);
     break;
 case QtCriticalMsg:
     fprintf(stderr, "Critical: %s\n", msg);
     break;
 case QtFatalMsg:
     fprintf(stderr, "Fatal: %s\n", msg);
     abort();
 }
 }
 class TestQString: public QObject
 {
Q_OBJECT
public:
TestQString(){
    qInstallMsgHandler(myMessageOutput);
}
private slots:
void toUpper();

};
//! [0]

//! [1]
void TestQString::toUpper()
{
QString str = "Hello";
QCOMPARE(str.toUpper(), QString("HELLO"));
qDebug() << "hello";
std::cout << "Hello, world!\n\n";
}
//! [1]

//! [2]
QTEST_MAIN(TestQString)
#include "testqstring.moc"
//! [2]

Solution

  • QT4 Version: Try to implement your own Messagehandler like this:

    void MessageOutput(QtMsgType type, const char* msg)
    {
        //in this function, you can write the message to any stream!
        switch (type) {
          case QtDebugMsg:
            std::cout << msg << ::std::endl;
            break;
        //case QtWarningMsg:
        //  fprintf(stderr, "Warning: %s\n", msg);
        //  break;
        //case QtCriticalMsg:
        //  fprintf(stderr, "Critical: %s\n", msg);
        //  break;
        //case QtFatalMsg:
        //  fprintf(stderr, "Fatal: %s\n", msg);
        //  abort();
        }
    }
    
    int main(int argc, char* argv[])
    {
    qInstallMsgHandler(MessageOutput);
    
    QApplication App(argc, argv);
    
    Testapp Updater;
    Updater.doSomething();
    
    return App.exec();
    }
    

    Every DebugMsg is redirected to std::cout.

    Same example for QT5 here