[Interest] QTest + QSignalSpy doesn't seem to work with deferred signals

Bob Hood bhood2 at comcast.net
Wed Jan 18 18:16:30 CET 2017

I'm trying to develop a unit test for an deferred signal; i.e. one that won't 
be emitted until the event loop has been allowed to run.  The case looks like:

   void MyTest::user_info_deferred()
       QVERIFY(qnam_class_ptr != nullptr);

       QSignalSpy spy(qnam_class_ptr, &QNAMClass::signal_userInfoReceived);

       // Retrieve account user information (deferred)
       QCOMPARE(qname_class_ptr->userInfo(), true);

       QCOMPARE(spy.count(), 1);   // make sure the signal was emitted exactly 
one time
QNAMClassUser info = qvariant_cast<QNAMClassUser>(spy.at(0).at(0));

I have registered the QNAMClassUser data type, both in the header:


and in the MyTest::initTestCase() method:


Everything compiles, and the call to userInfo() that initiates the QNAM 
request returns true.  However, not surprisingly, the signal has not yet been 
emitted by the time I hit the next statement that tests the result.  (This is 
taken directly from the QSignalSpy example in the documentation, btw).

Is QTest capable of testing deferred results--i.e., evaluating tests results 
after the even loop has run again--or can only synchronous actions be tested?  
I know people have used qWait() and such in the past, or I supposed I could 
manually invoke the event loop within the unit test until signals are emitted, 
but I was wondering if QTest has been designed to handle that in a canonical 
fashion I'm not immediately seeing.

More information about the Interest mailing list