Usage of Assert.Inconclusive

Johannes Rudolph picture Johannes Rudolph · Aug 2, 2009 · Viewed 19.6k times · Source

I'm wondering how someone should use Assert.Inconclusive().

I'm using it if my unit test would be about to fail for a reason other than what the test is for.

For example, I have a method on a class that calculates the sum of an array of ints. On the same class, there is also a method to calculate the average of the element. It is implemented by calling sum and dividing it by the length of the array.

Writing a Unit test for Sum() is simple. However, when I write a test for Average(), and Sum() fails, then Average() is likely to fail also.

The failure of Average is not explicit about the reason it failed; it failed for a reason other than what it should test for. That's why I would check if Sum() returns the correct result, otherwise I Assert.Inconclusive().

Is this to be considered good practice? What is Assert.Inconclusive intended for? Or should I rather solve the previous example by means of an Isolation Framework?

Answer

Igor Brejc picture Igor Brejc · Aug 2, 2009

Inconclusive test is a test for which you cannot determine the result. For example, what if you had a test that uses some kind of an external resource (Internet connection, for example). If the connection is currently not available, this doesn't really mean the test is a failure. On the other hand, you shouldn't just mark it as successful without actually running it through. So you mark it as inconclusive and this can be seen in the test report.

NOTE: In general, you shouldn't use such external resources in your tests, since this can make the tests fragile.

For tests that haven't been finished yet, I use MbUnit's Explicit attribute.