Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Continuing TestNG test execution post assertion failure

Assertions are used to validate if a condition holds true. In previous article, we discuss on how to create assertion in TestNG.


See: How to work with Assertions in TestNG tests.


Assertion added in previous article were hard assertion, i.e : current test execution stops once an assertion fails and the next test in the test suite is executed while using hard assertions. When we use import org.testng.Assert library, the test execution stops once an error is encountered. This is useful to stop an test execution in case of critical defect in the test, and move to next test in the suite.


Example of creating multiple test in explained in the below TestNG.xml


 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
 <test name="Test1">  
 <classes>  
   <class name="testNG.assertionVal"/>  
 </classes>  
 </test>  
 <test name="Test2">  
 <classes>  
   <class name="testNG.NewTest1"/>  
 </classes>  
 </test>  
 </suite>  

There can be scenario where we do not want to stop test execution due to a minor validation defect.  In that scenario we can use the concept of soft assertion, i.e the test case execution will continue although the test will be marked as failed in the final results but will be executed until the test flow is completed or an hard assertion failure condition occurs.


Below points are important to note from current article:

  • There are two types of assertion Hard assertion and soft assertion.
  • Hard assertion use import org.testng.Assert library
  • Soft assertion use import org.testng.asserts.SoftAssert library
  • Step  softas.assertAll() needs to be added to display the validation failure from soft assertion failure


Below code explains how to use soft assertion in the test.


 package testNG;  
 import org.testng.Assert;  
 import org.testng.annotations.Test;  
 //soft assertion will use the below softAssert class  
 import org.testng.asserts.SoftAssert;  
 public class assertionVal {  
      // Create an soft assertion object softas   
      SoftAssert softas = new SoftAssert();  
      @Test  
      public void assertVal()   
      {  
           //Create an assertion similar to hard assertion as shown below   
           softas.assertEquals("aaaa", "bb");  
           softas.assertEquals("aa","aaccc","asserts exists");  
           // this is an example of hard assertion, test will stop execution   
           //at this step in case error is encountered.  
           Assert.assertEquals("aa", "aa");  
           System.out.println("nitin");  
           //This step is very important as without this step,  
           //no soft assertion failure will be reported in test result.  
           softas.assertAll();  
      }  
 }  

No comments:

Post a Comment