在我的测试用例中,我想声明qDebug()的输出包含某个字符串。例如,这就是我的测试。请注意qdebug的“绑定到UDP端口34772”输出。我可以在我的测试函数中测试qdebug中是否存在子串34772吗?
********* Start testing of tst_NetSocket *********
Config: Using QTest library 4.8.6, Qt 4.8.6
PASS : tst_NetSocket::initTestCase()
QDEBUG : tst_NetSocket::testBindToPortDefaultToMinAvailable() Bound to UDP port 34772
FAIL! : tst_NetSocket::testBindToPortDefaultToMinAvailable() 'socket->getCurrentPort() == port_should_be' returned FALSE. ()
Loc: [test_net_socket.cpp(59)]
PASS : tst_NetSocket::cleanupTestCase()
Totals: 5 passed, 1 failed, 0 skipped
********* Finished testing of tst_NetSocket *********
这是我的测试文件。我想在我的测试函数中添加一个QVERIFY()
语句,用于检查子串34772的qDebug输出。
#include <QObject>
#include <QtTest/QtTest>
#include <unistd.h>
#include "net_socket.hpp"
class tst_NetSocket: public QObject
{
Q_OBJECT
private slots:
// ....
void testBindToPortDefaultToMinAvailable();
};
// ... other tests removed for example ...
void tst_NetSocket::testBindToPortDefaultToMinAvailable()
{
NetSocket * socket = new NetSocket();
int port_should_be = (32768 + (getuid() % 4096)*4);
if (socket->bindToPort()) {
QVERIFY(socket->getCurrentPort() == port_should_be);
}
}
QTEST_MAIN(tst_NetSocket)
#include "test_net_socket.moc"
答案 0 :(得分:2)
使用QTest::ignoreMessage断言在测试代码中添加了某条消息:
// ...
int port_should_be = (32768 + (getuid() % 4096)*4);
QTest::ignoreMessage(QtDebugMsg, QString::fromLatin1("Bound to UDP port %1").arg(port_should_be).toUtf8().constData());
// ...