Are there any techniques to fast-forward time for testing purposes in a Qt application?

  • Using Qt for developing a system that in various places involves time based state transitions. I'm trying to put in place comprehensive unit testing coverage as I go, and obviously trying to provide thorough automated testing of state transitions that occur over multiple second or even minute periods is severely suboptimal.

    In the past, when dealing with the same challenges for "bare metal" C and RTOS based embedded systems, I've successfully implemented hook functions that have allowed the fast-forwarding/acceleration of passing time for testing purposes, and am wondering if anyone out there has come across any techniques for achieving the same in Qt (developer APIs, shim libraries etc)

    The state transitions in question may trigger from a combination of QTimer timeouts, examination of QDateTime/QElapsedTimer/QDeadlineTimer timestamps and QThread sleeps, so a simple fake of QTimer::timeout signals is not enough.

    Not really needing the usual suggestions regarding dependency injection of mockable timers, interfaces that allow configurable timeouts, or hanging custom test facilitating facade/decorators over the entirety of the Qt library time related classes. I'm well aware of how I could do this, I'm hoping that I don't have to...

    Duped at


  • Moderators

    @Richard-Lang I know you don't want to hear it but the answer for testing stuff like this is mocking. That really is the whole point of mocking. Otherwise what I do in these situations is change my timeouts and such based on a debug setting. That way I can speed things up to quickly test while developing but without those -D flags to the build, they run at normal time.

    That being said a quick google turned this up which may be something you're looking for:

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.