XUnit 2.4.2-pre: Multiple asserts in one test

Multiple asserts in test-cases in XUnit is considered bad practice. The usual reason for this, is that if one assert fails, you will never see the result of the rest. And probably true, having a single assertion, improves defect localization and simplifies the tests. But sometimes… sometimes you wish you could have multiple conditions; and that it would be following best practice.

In a calculator test, we want to test the Add method and we want to include Commutativity, for example that 1+2 = 2+1 = 3

NUnit solves this through Assert.Multiple(()=>{ /* multiple assert statements */ });u and all failed asserts will be recorded and reported. In the upcoming release, XUnit added a Multiple statement.

Without Assert.Multiple, our tests need to be simple and with only a single condition, we could end up writing something like this:

We could replace the two by a Theory, with two [InlineData] of the same values one in reverse order, but the intent somewhat disappears.

XUnit 2.4.2-pre (finally) introduces the Multiple assertion. The documentation states:

Added Assert.Multiple(params Action[] checks) which allows the developer to run several assertions as individual lambdas, and collect up all the failures into a single failure. If there is only a single failure, it will show as normal; if there are multiple failures, a new message shows that there were multiple failures, which includes all the failure messages and associated stack traces.


This means that we can now rewrite the above with Multiple:

I think this is a nice addition to the assertion library of XUnit. I think, however, perhaps it would be worth considering adding messages. This way, we could identify constituents of a Multiple — but perhaps… this would make it too attractive. After all, we need to make tests simple and readable.

There are other ways to improve readability of your XUnit-based test!

Leave a Comment