我尝试在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: %sn", msg);
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %sn", msg);
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %sn", msg);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %sn", 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!nn";
}
//! [1]
//! [2]
QTEST_MAIN(TestQString)
#include "testqstring.moc"
//! [2]
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: %sn", msg);
// break;
//case QtCriticalMsg:
// fprintf(stderr, "Critical: %sn", msg);
// break;
//case QtFatalMsg:
// fprintf(stderr, "Fatal: %sn", 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示例相同