Tuesday Tip Day – Multiple Asserts with Nunit

Be a better automation tester

Ok so it’s not technically Tuesday, but I’m only a day late and besides, Wednesday Tip Day doesn’t quite work.

So todays tip is all about how to deal with tests that might have multiple asserts. You might be thinking that it really isn’t something that requires a tip, as it’s just a case of writing a few extra lines of code, but there’s a problem with multiple asserts if not done properly.

Let’s say you have a test that opens a new dialogue box and wanted to check that the content is correct, the controls are displayed and that any links are displayed. You might typically write your asserts as so:

Assert.AreEqual(expectedContent, actualContent, “Expected content did not match the actual content”);
Assert.IsTrue(dialogueBox.ConfirmButton.Enabled, “Confirm button is not enabled as expected”);
Assert.IsTrue(dialogueBox.PdfLink.Displayed, “PDF Link not displayed as expected”);

Forgive the very poor use of asserts above…I promise it’s just for the example. Poor asserts aside, let me explain the problem. While this test on paper covers all the acceptance criteria for your test case, there’s a good chance that not all your asserts are being run in the event of a failure. For example, if the first assert fails because the content is incorrect, then the second or third assert aren’t even run. You could argue that if the test fails at any point, then it doesn’t matter which assert it failed at, just that it did actually fail. While that may be true to an extent, and would be perfectly acceptable if you were only ever running tests locally, when your tests are run using a CI system like Jenkins, Bamboo, Azure etc, this becomes a big issue.

The reports that are generated from your chosen platform, or even the custom reports that you’ve created yourself will only contain a message about the first point of failure, which means that you’re not getting the full picture from your test runs and may potentially miss a potential blocker getting through while you’re sat there fixing one of the many problems in your product.

So how do we fix this issue? Nunit contains a useful, yet mostly unknown feature called Assert.Multiple. That will allow multiple asserts to be run, even if one above them fails. Here’s an example on how to use this feature:

Assert.Multiple(() =>
{
    Assert.AreEqual(expectedContent, actualContent, “Expected content did not match the actual content”);
    Assert.IsTrue(dialogueBox.ConfirmButton.Enabled, “Confirm button is not enabled as expected”);
    Assert.IsTrue(dialogueBox.PdfLink.Displayed, “PDF Link not displayed as expected”);
});

With the above code, even if the content assert fails, we will still run the other two meaning we get the complete result from our test. This means your reports will now contain more useful information and give a more accurate representation of the state of your tests.

There is the argument that a test should never contain more than one assert…but let’s not get in to that one here.

Hopefully this has helped you and has taught you how to improve some of your tests. If there’s any areas you’d like to see covered with regards to the tips I give, then please do get in contact and I’ll make sure to cover them all.