Handling multiple assert in Selenium with Assert.multiple of NUnit 3.6

In this post we will discuss how to handle multiple assert in Selenium with Assert.multiple of NUnit 3.6, this is one of the most requested feature of NUnit.

NUnit 3.6

Nunit 3.6 released by Jan 9th 2017 has number of notable improvement and features something like this

  • .NET Standard 1.6 is now supported
  • Adds support for Multiple Assert blocks
  • Added the –params option to NUnitLite
  • Theories now support Nullable enums
  • Improved assert error messages to help differentiate differences in values
  • Added warnings with Warn.If(), Warn.Unless() and Assert.Warn()
  • Enabled Path, File and Directory Asserts/Contraints for .NET Core testing
  • Added NonTestAssemblyAttribute for use by third-party developers to indicate that their assemblies reference the NUnit framework, but do not contain tests

Where is Assert.Multiple helpful ?

Consider a scenario something like this

Lets say we have to test multiple properties of an UI element in Selenium

In Nunit test framework, if the first assert fails (not meeting the condition), then the second assertion will not even takes place and the whole test fails (which we all know)

But using new Assert.Multiple method, the code can be written something like this

Benefits of Assert.Multiple

  • The multiple assert block may contain any arbitrary code, not just asserts.
  • Multiple assert blocks may be nested. Failure is not reported until the outermost block exits.
  • If the code in the block calls a method, that method may also contain multiple assert blocks.
  • The test will be terminated immediately if any exception is thrown that is not handled. An unexpected exception is often an indication that the test itself is in error, so it must be terminated. If the exception occurs after one or more assertion failures have been recorded, those failures will be reported along with the terminating exception itself.
  • Assert.Fail is handled just as any other assert failure. The message and stack trace are recorded but the test continues to execute until the end of the block.
  • An error is reported if any of the following are used inside a multiple assert block:
    • Assert.Pass
    • Assert.Ignore
    • Assert.Inconclusive
    • Assume.That
  • Use of Warnings (Assert.Warn, Warn.If, Warn.Unless) is permitted inside a multiple assert block. Warnings are reported normally along with any failures that occur inside the block.

Here is the complete video of the above discussion

Here is the complete source code of the above video

Thanks for reading the post and watching the video!!!

Please leave your comments and let me know if there is anything I should update in this post.

Thanks,
Karthik KK

Leave a Reply

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