How to test Qt SCXML state machines

你说的曾经没有我的故事 提交于 2019-12-22 09:11:43

问题


I'm trying to verify the behavior of a state machine using Qt test framework. I simply don't get how I am supposed to tests Qt SCXML implementation. Sure there is QSignalSpy, but that is only for signals/slops which do not require the event loop to run. What I essentially want to do is:

myStateMachine.submitEvent("MyEvent");
// Run event loop
// Check result

I tried to QCoreApplication::processEvents() this sometimes worked, but sometimes also got stuck when calling processEvents(). I guess I might triggered an infinite loop. Also googling did not help, but there must be a way to do this properly.


回答1:


In a QtTest based test you can use QTest::qWait() to run the event loop for a given time.

You can also use QSignalSpy to wait for a signal to happen within a certain time, see QSignalSpy::wait()

If there are more than one signal that can trigger test continuation, you can also use a nested event loop, e.g. something like this

QEventLoop loop;
connect(sender1, SIGNAL(signal1()), &loop, SLOT(quit()));
connect(sender2, SIGNAL(signal2()), &loop, SLOT(quit()));
loop.exec();

Potentially combined with a timer to end the loop in case none of those signals arrive




回答2:


We had the same issue and we took advantage of QScxmlStateMachine::reachedStableState signal.

// We declared a QTestEventLoop as m_eventLoop
connect(stateMachine, &QScxmlStateMachine::reachedStableState, 
        &m_eventLoop, &QTestEventLoop::exitLoop);

stateMachine->submitEvent("MyEvent");
m_eventLoop.enterLoopMSecs(3000);
// check the state, results etc


来源:https://stackoverflow.com/questions/40677999/how-to-test-qt-scxml-state-machines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!