My team makes extensive use of NUnit unit tests in our C# project. Recently we have started using the Task Parallel Library (TPL) in .NET 4, which has introduced a wrinkle for us.
In the TPL, if a Task is faulted (that is, an exception was thrown while executing the task), that task's Exception property must be retrieved at least once. If it is not, when the Task object is finalized by the garbage collector an exception will be thrown which terminates the process.
It is possible to detect and prevent this by registering a handler for the TaskScheduler.UnobservedTaskException. We have done that for a few of our test cases to reproduce unobserved task exception bugs, but I would rather have some way to modify the way NUnit runs the tests so that for each test, an UnobservedTaskException handler is registered, then after that test a garbage collection is forced to flush out any tasks with unobserved exceptions. I would then like this to cause the test to fail.
How are other teams solving this problem? How are you detecting test cases that pass (that is, complete without any exceptions) but leave one or more Task objects with unobserved exceptions?