Should I Use Or Split Multiple Asserts?

Gil Zilberfeld explains why we would want to split unit tests with multiple assertions into single unit tests with single asserts.

I get this unit testing question often. It makes efficient sense to stuff all kinds of assertions after a single setup and invocation. While there’s no one answer, for me, it usually comes down to: Try to separate rather than join them. While it’s possible to have some asserts for the same operation, in most situations, we’re better off separating them to multiple unit tests.

The main idea behind this is that if the first assertion fails, we won’t know what happened with consequent ones (depending on default implementation of unit testing frameworks). When that happens, we lose information that is important for solving the problem.

The following example here is of a transaction that transfers money from one account to another. To check it completely, we’d like to test that both account balances changed.

I think we can all agree that the transaction we check is a single operation. It makes sense that we assert both balance amounts here, with the same setup and invocation. One unit test to rule them all.

When You First Fail

What happens when the first assert fails? It throws an exception, the unit test ends, and the second assertion will not be checked. An error will only be reported on the first one.

The other assert could have given us information about the problem. We lost that information.

Now consider the other option. If there were two separate unit tests:

If we ignore the duplication for a minute, now we have the results of both tests and conditions. In the unit test run report we’ll see the result of both.

Why is that important?

Because we want to fix the problem as quickly as possible. And more information early (as in the unit test report) helps you triangulate the problematic code.

Is it worth splitting the tests? As always – it depends.

Assuming we should write the test, it unit tests for a higher risk code. If that’s the case, when it breaks, we want to fix it as quickly as possible.

We’ll want more data sooner.  I’d go for the split.


4 thoughts on “Should I Use Or Split Multiple Asserts?

  1. splintor

    But what about the duplication?

    Also note that this is not relevant when testing JavaScript code with Jasmine. In Jasmine, when an expectation fails, it issues an error, but the test continues to run the remaining expectations. This turns out to be quite handy, and the C# community should consider having such an option for its tests. as well.

Leave a Reply

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