What Bug Will It Find?

Standard

“Is this a good test?”

Usually the question refers to a test that somebody has already written. But is the test effective?

Should we have written it in the first place?

That’s right, we don’t need to test everything. There are a lot of examples (e.g., testing getters/setters or data mapping) of those. Looking at them, we smell they are not that valuable, and in fact are more of a liability. For example, they enforce rewriting on interface change.

Is this test valuable? For that, we should ask a different question:

“If it fails, what bug will it find?”

Or in other words, what is the risk of not having that test in place?

For my first example, I’ll call the immortal Calculator. Mr. Calculator has a complex Add function that takes two numbers and adds them.

 

Now, if our test for the Add function fail, that is one broken main functionality. Of course, we want to test it.

My next witness will be the SafeDiv function. Of course, we won’t just let the calculator divide by zero. Instead, we’ll throw an exception:

Apart from the test that checks the division, we can write a test for the error handling function:

Is the exception throwing important? Maybe. We expect an exception to be thrown anyway.

Is the message we’re asserting on important? Sometimes it is, for user experience reasons. And the experience we’re supplying here is not that dramatic (depending on how sensitive you are).

In other words: If this test fails in the future, it will be because the message was incorrect. That would be the bug it finds.

And to tell the truth, this is usually not a very important bug to fix. Therefore, the test we just wrote is not that valuable, and may become a liability in the future. The risk of not having it is very low.

When we write tests, we shouldn’t assert everything. We shouldn’t verify all possible calls.

We should think what bug we expect the test to help us uncover. If that’s not an important bug, don’t write the test.

Bonus tip: You can tell if that’s a valuable test just by writing the test name. It will tell you in advance, if you’re wasting your time or not.

Leave a Reply

Your email address will not be published. Required fields are marked *