在测试用例中读取qdebug
Read qdebug within a test case?
在我的测试用例中,我想断言qDebug()的输出包括某个字符串。例如,这就是我的测试结果。请注意qdebug的"绑定到UDP端口34772"输出。我可以从我的测试函数中测试子字符串34772是否存在于qdebug中吗?
********* 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"
使用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());
// ...
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++将文本文件中的数据读取到结构数组中
- 正在将csv文件读取为双精度矢量
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 为什么在读取文件大小时文件IO速度会发生变化
- 正在读取二进制文件(is_open)
- 如何在c++中从文本文件中逐行读取整数
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 独立读取-修改-写入顺序
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 在测试用例中读取qdebug