Qt 事件循环和单元测试? [英] Qt event loop and unit testing?

查看:21
本文介绍了Qt 事件循环和单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们开始在 Qt 中进行单元测试,希望听到对涉及单元测试信号和槽的场景的评论.

I'we started experimenting with unit testing in Qt and would like to hear comments on a scenario that involves unit testing signals and slots.

这是一个例子:

我要测试的代码是(m_socket 是一个指向QTcpSocket的指针):

The code i would like to test is (m_socket is a pointer to QTcpSocket):

void CommunicationProtocol::connectToCamera()
{
    m_socket->connectToHost(m_cameraIp,m_port);
}

由于这是一个异步调用,我无法测试返回值.但是,我想测试套接字在成功连接时发出的响应信号(void connected ())是否确实发出了.

Since that is an asynchronous call i can't test a returned value. I would however like to test if the response signal that the socket emits on a successful connection (void connected ()) is in fact emitted.

我已经写了下面的测试:

I've written the test below:

void CommunicationProtocolTest::testConnectToCammera()
{
    QSignalSpy spy(communicationProtocol->m_socket, SIGNAL(connected()));
    communicationProtocol->connectToCamera();
    QTest::qWait(250);
    QCOMPARE(spy.count(), 1);
}

我的动机是,如果在 250 毫秒内没有响应,那就是有问题.

My motivation was, if the response doesn't happen in 250ms, something is wrong.

但是,信号从未被捕获,我无法确定它是否已发出.但是我注意到我没有在测试项目的任何地方启动事件循环.在开发项目中,事件循环在 main 中以 QCoreApplication::exec() 启动.

However, the signal is never caught, and I can't say for sure if it's even emitted. But I've noticed that I'm not starting the event loop anywhere in the test project. In the development project, the event loop is started in main with QCoreApplication::exec().

综上所述,当对依赖于信号和槽的类进行单元测试时,

To sum it up, when unit testing a class that depends on signals and slots, where should the

QCoreApplication a(argc, argv);
return a.exec();

在测试环境中运行?

推荐答案

我意识到这是一个旧线程,但是当我点击它和其他人一样,没有答案,彼得和其他评论的答案仍然没有抓住重点使用 QSignalSpy.

I realize this is an old thread but as I hit it and as others will, there is no answer and the answer by peter and other comments still miss the point of using QSignalSpy.

回答你关于哪里需要 QCoreApplication exec 函数"的原始问题,基本上答案是,不需要.QTest 和 QSignalSpy 已经内置了.

To answer you original question about "where the QCoreApplication exec function is needed", basically the answer is, it isn't. QTest and QSignalSpy already has that built in.

您在测试用例中真正需要做的是运行"现有的事件循环.

What you really need to do in your test case is "run" the existing event loop.

假设您使用的是 Qt 5:http://doc.qt.io/qt-5/qsignalspy.html#wait

Assuming you are using Qt 5: http://doc.qt.io/qt-5/qsignalspy.html#wait

所以要修改您的示例以使用等待功能:

So to modify your example to use the wait function:

void CommunicationProtocolTest::testConnectToCammera()
{
    QSignalSpy spy(communicationProtocol->m_socket, SIGNAL(connected()));
    communicationProtocol->connectToCamera();

    // wait returns true if 1 or more signals was emitted
    QCOMPARE(spy.wait(250), true);

    // You can be pedantic here and double check if you want
    QCOMPARE(spy.count(), 1);
}

这应该会为您提供所需的行为,而无需创建另一个事件循环.

That should give you the desired behaviour without having to create another event loop.

这篇关于Qt 事件循环和单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆