I noticed in my code that a class that created a new thread
that was never properly cleaned up. The unit test of that piece of code succeeded, but on the console there was a warning
saying that a System.AppDomainUnloadedException
had occurred. This can be reproduced with the following code snippet
[TestMethod]
public void StartNewThread_ThreadNotStopped_StillSucceeds()
{
var thread = new Thread(DoNothingForEver);
thread.Start();
}
private void DoNothingForEver()
{
while (true){/* do nothing */}
}
When developing such unit tests, the developer is responsible for ensuring that such exceptions don't occur, but at work I've seen this happen too often. Even worse, I've witnessed that when code with successful unit tests was refactored, the unit tests would still succeed but an unnoticed exception
was thrown. When digging into this issue, it became clear that the refactored code was not working properly, and as such, the unit test had little added value.
Is there any way that I can instrument my test projects such that MsTest
will fail in such situations?