Site Loader
Auckland, New Zealand
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 https://gist.github.com/executeautomation/c52f8094846d95fbb3ebbb47b7062243 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

Post Author: Karthik kk

4 Replies to “Handling multiple assert in Selenium with Assert.multiple of NUnit 3.6”

  1. I’ve written 3 asserts in multiple assert block when 3 are failed in the results is showing 3 failures.

  2. In your cases you can better inline multiple assert 🙂
    Assert.That(“SomeText”, Is.EqualTo(“SomeText”).And.Not.Null);

  3. Hi Karthik,

    Thanks much for the tutorials and videos. It was very helpful.
    I have used Assert.Multiple in Nunit. But the team switching to xUnit for better performance. Could you please help me similar methods in xUnit.

  4. Hi,

    What if I want to check two things like an OR , so the test must pass if either the 1st condition or the 2nd one is met?:

    E.g.
    Assert.That(_driver.FindElement(By.ClassName(“xyz”)).Text, Is.EqualTo(“Welcome new user”));
    Assert.That(_driver.FindElement(By.ClassName(“xyz”)).Text, Is.EqualTo(“Hello Ashok”));

Leave a Reply to Ravindra Karanki Cancel reply

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