Wednesday, April 4, 2018

Soft Assert or Multiple Assert in C# using Nunit

Soft Assert or Multiple asserts is required in few cases where a user wants his test to continue if an assert fails. Usually, a tester has to test multiple validation points in a single test case then he needs this feature.

Using nunit, it is possible. see the code below.

Step 1: Install nunit using NuGet Manager

Step 2: Create a class "TestMultipleAssert" and add following code in it.

using NUnit.Framework;

namespace WordPressAutomation.DifferentTests
{
  
    [TestFixture]
    public class TestMultipleAssert
    {
      

         [Test]
        public void ComplexNumberTest()
        {
         
            Assert.Multiple(() =>
            {
                Assert.AreEqual(5, 5, "First Assert");
                Assert.AreEqual("Testing 2", "Testing 2", "Second Assert");
                Assert.AreNotEqual("123", "ABC", "Asserts are not equal");
             });
        }
    }
}

Step 3: Run this code and everything will pass.

Step 4: Change the first assert expected value

 Assert.AreEqual(6, 5, "First Assert");

Step 5: Now execute it. Test case will fail in this case

No comments:

Post a Comment