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

<p>So, my question is: Is there a good way to unit test a timer based class?</p>

<p>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.<br>
Especially if one wants realistic data (seconds), instead of using the minimal time resolution allowed by the framework (1 ms?).<br>
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.</p>

[{'answer_id': 12046, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 2, 'selected': False, 'text': "<p>I think what I would do in this case is test the code that actually executes when the timer ticks, rather than the entire sequence. What you really need to decide is whether it is worthwhile for you to test the actual behaviour of the application (for example, if what happens after every tick changes drastically from one tick to another), or whether it is sufficient (that is to say, the action is the same every time) to just test your logic.</p>\n\n<p>Since the timer's behaviour is guaranteed never to change, it's either going to work properly (ie, you've configured it right) or not; it seems to be to be wasted effort to include that in your test if you don't actually need to.</p>\n"}, {'answer_id': 12054, 'author': 'David Sykes', 'author_id': 259, 'author_profile': 'https://Stackoverflow.com/users/259', 'pm_score': 5, 'selected': True, 'text': '<p>What I have done is to mock the timer, and also the current system time, that my events could be triggered immediately, but as far as the code under test was concerned time elapsed was seconds.</p>\n'}, {'answer_id': 12743, 'author': 'graham.reeds', 'author_id': 342, 'author_profile': 'https://Stackoverflow.com/users/342', 'pm_score': 4, 'selected': False, 'text': '<p><a href="http://www.lenholgate.com/" rel="noreferrer">Len Holgate</a> has a series of <a href="http://www.lenholgate.com/archives/000306.html" rel="noreferrer">20 articles on testing timer based code</a>.</p>\n'}, {'answer_id': 12785, 'author': 'Mike Deck', 'author_id': 1247, 'author_profile': 'https://Stackoverflow.com/users/1247', 'pm_score': 2, 'selected': False, 'text': "<p>I agree with Danny insofar as it probably makes sense from a unit-testing perspective to simply forget about the timer mechanism and just verify that the action itself works as expected.  I would also say that I disagree in that it's wasted effort to include the configuration of the timer in an automated test suite of some kind.  There are a lot of edge cases when it comes to working with timing applications and it's very easy to create a false sense of security by only testing the things that are easy to test.</p>\n\n<p>I would recommend having a suite of tests that runs the timer as well as the real action.  This suite will probably take a while to run and would likely not be something you would run all the time on your local machine.  But setting these types of things up on a nightly automated build can really help root out bugs before they become too hard to find and fix.</p>\n\n<p>So in short my answer to your question is don't worry about writing a few tests that do take a long time to run.  Unit test what you can and make that test suite run fast and often but make sure to supplement that with integration tests that run less frequently but cover more of the application and its configuration.</p>\n"}]
['https://Stackoverflow.com/questions/12045', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/276/']