Search code examples
qtunit-testingtestingtime

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 https://forum.qt.io/topic/88268/are-there-any-techniques-to-fast-forward-time-for-testing-purposes-in-a-qt-application

thx


Solution

  • 5 years later, I've built my own

    https://github.com/RichardLangFromNZ/QtFakeTime