2014-07-10

Writing async tests

Ever since async/await was introduced in .net 4.5 I've seen people happily using those keywords in their (unit) tests. A usage I believe to be a mistake.

When .net 4.5 was first in public beta most test frameworks had a bug making it impossible to write async test methods. I wish they had never fixed that because I don't think those help you write good tests.

The main reason is that the use of await in your test code first of all makes it impossible to had a short timeout on your task completion. Whenever you test something involving tasks a simple mistake typically leads to the task never completing, especially when setting up a fake in the wrong way. Relying on the default timeout typically means a very long feedback cycle to see the tests fail.

Another problem is that when you use the await keyword you do not know if it was the method that threw an exception (like ArgumentException) or if the task failed after it was started. The difference is important if you want to follow the TAP guidelines.

That is why I never mark my test methods as async. Instead I use helpers to make sure my tests complete or fail within a reasonable amount of time. I've collected some of the helpers here in my toolbox.

No comments:

Post a Comment