Search code examples
c#.netunit-testingtimer

Unit testing a timer based application?


I am currently writing a simple, timer-based mini app in C# that performs an action n times every k seconds.
I am trying to adopt a test-driven development style, so my goal is to unit-test all parts of the app.

So, my question is: Is there a good way to unit test a timer-based class?

The problem, as I see it, is that there is a big risk that the tests will take uncomfortably long to execute since they must wait so and so long for the desired actions to happen.
Especially if one wants realistic data (seconds), instead of using the minimal time resolution allowed by the framework (1 ms?).
I am using a mock object for the action, to register the number of times the action was called, and so that the action takes practically no time.


Solution

  • What I have done is to mock the timer, and also the current system time, so that my events could be triggered immediately, but as far as the code under test was concerned time elapsed was seconds.