如何在jenkins控制台中看到qDebug和Unit测试宏(QCOMPARE等)的结果

时间:2014-07-02 08:08:16

标签: qt unit-testing jenkins console qdebug

我尝试将单元测试结果打印在jenkins控制台中。但我不能这样做。我在想jenkins构建控制台无法打印qDebug。我试过它无法打印qdebug。但是std :: cout正在发挥作用。我该怎么办?

谢谢。我试过你说的。但它无法奏效。它给出了错误。错误:C3861:' qInstallMsgHandler':未找到标识符

//! [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]

1 个答案:

答案 0 :(得分:1)

QT4版本: 尝试像这样实现自己的Messagehandler:

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();
}

每个DebugMsg都被重定向到std :: cout。

QT5 here

的相同示例