Let’s say I just wrote the following class and I am starting to write some unit tests for it:
http://www.ogre3d.org/docs/api/html/classOgre_1_1Timer.html
How would I make sure the timer is working properly by just the unit tests? I can test to make sure that getMilliseconds() is always greater than the start time for example, but that does not tell me that getMilliseconds() is returning the correct delta-time.
Any suggestions?
Ideally, you should separate out the OS call and make that it’s own class that uses virtual functions so you can stub out the OS call with your own data. That would let you test that everything except for the data you get back from the OS is correct.
In my opinion, this is good idea in general. I have come to think of the OS as the Singleton you can’t remove. It’s best to localize the way you use that Singleton so that most of your code doesn’t access it.
That being said, there are some techniques for testing the OS.
One technique for testing is to try to get the OS to do approximately the same thing in two different ways and make sure those two different ways more or less match up. In this case, I would suggest using a
sleepfunction of some sort. On Unix, for example, you could usenanosleepto sleep for a fraction of a second and then test to make sure that your timer class measured that fraction of a second as being more or less the same (within some margin of error).