I’m new to unit testing and nUnit (2.48). I’d like to write a test method where the failure case is that it deadlocks. Is this possible? Obviously nUnit doesn’t know by default how long the method should take to execute, so would I have to write code to do the work on a separate thread and then abort it and throw and exception if it took longer than some time I define? Is there a better way to do this?
Thank you
It is possible but it might not be the best thing to do. Unit tests aren’t really suitable for testing concurrency behaviour, unfortunately there aren’t many test-methods that are suitable.
NUnit doesnt do anything with threads. You could write tests that start several threads and then test their interaction. But these would start to look more like integration tests than unit tests.
Another problem is that deadlock behaviour usually depends on the order threads are scheduled in. So it would be hard to write a conclusive test to test a certain deadlock issue because you don’t have any control over the thread scheduling, this is done by the OS. You could end up with tests that sometimes fail on multicore processors but always succeed on single core processors.