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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With