Reputation: 41
In my test case, I want to assert that the output of qDebug() includes a certain string. For example, this is what my test looks like. Notice the "Bound to UDP port 34772" output from qdebug. Can I test that the substring 34772 exists in qdebug, from within my test function?
********* 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 *********
Here's my test file. I want to add a QVERIFY()
statement in my test function that checks the output of qDebug for the substring 34772.
#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"
Upvotes: 1
Views: 560
Reputation: 1186
Use QTest::ignoreMessage to assert that a certain message is added in the code under test:
// ...
int port_should_be = (32768 + (getuid() % 4096)*4);
QTest::ignoreMessage(QtDebugMsg, QString::fromLatin1("Bound to UDP port %1").arg(port_should_be).toUtf8().constData());
// ...
Upvotes: 2