[Development] date/time adjust for (auto) testing

André Somers andre at familiesomers.nl
Wed Jun 3 12:26:48 CEST 2015


Bo Thorsen schreef op 3-6-2015 om 11:44:
> Den 03-06-2015 kl. 08:38 skrev André Somers:
>> We have applications that use the current date and time at places spread
>> around the code. For normal operations, that works very nicely. However,
>> we find that for (auto) testing, it would be very convenient if we could
>> trick the application into believing it is some other date/time, so that
>> we can test if certain behaviours work the way we would like to
>> automatically. Currently, these tests take a lot of time because we
>> actually need to manually adjust the system date and time, do some
>> stuff, then adjust again, etc.
>>
>> It would be really confortable if there was some control to set a
>> date/time offset (so the time keeps running) or a fixed date/time to be
>> returned from currentDate(), currentTime() or currentDateTime()
>> respectively. I guess access to such a thing does not belong in the main
>> Qt classes, but is really a testing tool, so perhaps it could find
>> refuge in QtTest somewhere. Would a contribution adding such a thing
>> stand any chance of being accepted, or would this be considered out of
>> scope or even unwanted?
>>
>> An alternative might be to hook the windows kernel API, but that may be
>> much tricker to get right and may have unforseen consequences for the
>> code injected by Squish doing the actual testing.
> To me it sounds like you need to create your own static current...()
> functions so you can control the output for unit tests instead.
That might be an option, and I did considder that. We may end up doing 
that, but it would involve changing quite a number of places and worse, 
is a bit hard to make sure is used everywhere consistently in the future.
>
> Or even better: Modify the tests so you can adapt the compares to
> something that handles the current time and date. Date is simple enough
> (yes, it can fail if you run the test at exactly 23.59.59.99999). For
> the time you just allow for a couple of seconds difference.
That's much harder to do, as I already explained earlier.
>
> I *really* don't like the idea of creating a system to add offsets to
> the QDateTime::currentDateTime (or friends). Then we can't depend on the
> output being correct, which means any unit tests you write using it are
> useless.
I expected the proposel to be contriversial, that's why I posted it just 
as an email in the first place. I think it would be useful especially 
_for_ unit tests. I'd also put the controls in the QtTest namespace, so 
that it is very obvious that this functionality is there to facilitate 
tests, not for other use. Note that there are many other methods there 
to trick Qt into acting as if events happened on the outside of the 
application, like simulating clicks and keyboard actions. Here, one 
would simulate the change of system time.
>
> If you really want to modify the output of current...() then you might
> consider preloading a library over Qt that does it for you.
>
I am considering that option as well, but that would require me to hook 
into the windows kernel calls. Like I said, that approach is much 
trickier and have a bigger chance of having side effects on for instance 
Squish.

André




More information about the Development mailing list