Using CATCH with Qt

331 views
Skip to first unread message

Jens W.

unread,
Jul 13, 2013, 8:08:42 AM7/13/13
to catch...@googlegroups.com
Hi,

I started using CATCH yesterday, and one of my problems is, that I need to test code running with Qt.
Some part of this code is only running and passing correctly if the eventloop is also running.

My current solution is running catch through a timer:

#define CATCH_CONFIG_RUNNER
#include <catch.hpp>



TEST_CASE( "LibProcessSink", "[ProcessSink]" ) {
    QVariant id = 1;
    ProcessSink test_path_invalid(" ",id);
    REQUIRE(test_path_invalid.start()==false);
    ProcessSink path_valid("C:\\windows\\system32\\cmd.exe /c dir",id);
    REQUIRE(path_valid.start()==true);
    path_valid.waitForFinished();
    REQUIRE(path_valid.getResult().isEmpty() == false);
}

void startCatch()
{
// this works, but is CATCH set up correctly that way? (no commandline args is clear)
    Catch::Session session;
    session.run();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    /*Catch::Session().run(argc, argv);// well, that one isn't going to run during eventloop execution, test results not valid*/
    QTimer timer;
    timer.setSingleShot(true);
    // I probably could also use a lambda here, to hand over the commandline parameters
    QObject::connect(&timer,&QTimer::timeout,startCatch);
    timer.start(1000);//*/
    return a.exec();
}

This works for now, but its just a first shot at the problem, is there a better way?

While I found a solution for this, one of my concerns currently is how to test asynchrone code (f.e. Qt SIGNAL/SLOT, boost::asio).
Especially if I need to wait for certain events/signals, what would be the best way to this with catch?

kind regards,

Jens Weller

Christian Askeland

unread,
Sep 20, 2013, 5:06:10 AM9/20/13
to catch...@googlegroups.com
We use catch in our Qt project. For testing the asynchronous code, we use QEventLoop instead of QApplication::exec().  Listening to signals can be done by connecting them to the QEventLoop quit() slot.
Reply all
Reply to author
Forward
0 new messages