TestNG Assertions


What is Assertion?
Asserts helps us to verify the conditions of the test and decide whether test has failed or passed. A test is considered successful ONLY if it is completed without throwing any exception.

Types of Assertions:

1) Hard Assertion
2) Soft Assertion



Hard Assertion
It is the default assert mechanism built into TestNG’s package. We use it when a test has to stop immediately after the assertion fails.

Soft Assertion
It is a custom assert mechanism supported by TestNG’s package. We use it when a test has to continue execution even after an assertion fails in the sequence.

Hard Assertions
1) Assert.assertTrue()
2) Assert.assertFalse()
3) Assert.assertEquals()

Test1:
import org.testng.Assert;
import org.testng.annotations.Test;
public class Test1 {
      @Test
      void demoTest() {
            Assert.assertTrue(true); // passed
            Assert.assertEquals("welcome", "welcome"); // true - passed
            Assert.assertEquals("selenium", "selenium");// true - passed
            System.out.println("Successfully passed!");
      }
}

Above Test which includes multiple assert calls, all of which get passed and so the test case.

Test2
import org.testng.Assert;
import org.testng.annotations.Test;

public class Test2 {
      @Test
      void demoTest() {
            Assert.assertTrue(true); // passed
            Assert.assertEquals("welcome", "Welcome"); // false - failed
            Assert.assertEquals("selenium", "selenium");
            System.out.println("Successfully passed!");
      }
}
In above scenario, the second assert call fails which leads to the end of the test case.

Testcase:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestCase1 {

           @Test     
           public void loginTest()
           {
     System.setProperty("webdriver.chrome.driver","C://Drivers/chromedriver_win32/chromedriver.exe");
           WebDriver driver=new ChromeDriver(); // launch the browser
          
           driver.get("http://newtours.demoaut.com/"); //open URL
          
           WebElement usernametxt=driver.findElement(By.name("userName"));
           WebElement passwordtxt=driver.findElement(By.name("password"));
          
               
           //This is to check whether the textbox is displayed or not
           //Test will only continue, if the below statement is true
          
           Assert.assertTrue(usernametxt.isDisplayed());
            usernametxt.sendKeys("mercury");

            Assert.assertTrue(passwordtxt.isDisplayed());
            passwordtxt.sendKeys("mercury");
            
            
            /*Assert.assertFalse(usernametxt.isDisplayed());
            usernametxt.sendKeys("mercury");

            Assert.assertFalse(passwordtxt.isDisplayed());
            passwordtxt.sendKeys("mercury");*/
            
                     
           driver.findElement(By.name("login")).click();
          
           //validation
           String ExpTitle="Find a Flight: Mercury Tours:";
           Assert.assertEquals(ExpTitle, driver.getTitle()); // compares both the titles
           driver.close();
}
}

Soft Assertion

Test3:
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class Test3 {
       SoftAssert softAssert = new SoftAssert();
       @Test
      void demoTest() {
            softAssert.assertTrue(true); // passed
            softAssert.assertEquals("welcome", "Welcome"); // false - failed
            softAssert.assertEquals("selenium", "selenium");// true - passed
            System.out.println("Successfully passed!");
            softAssert.assertAll();
      }
}

You can cross-check from the output , that the message appeared there even after one of the assert calls failed.

Test4:   An Issue In Using The Soft Assertion.

In this example, you can see that there are multiple test cases. They are using the same Soft assertion object. We added it to highlight the issue which occurs when one test failure makes other tests to fail. It happens due to the use of same assert object which evaluates all occurrences of assert methods despite being in different cases.
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class Test4 {

      SoftAssert softAssert = new SoftAssert();
      
     @Test
     void demoTest1() {
           softAssert.assertEquals("welcome", "Wel"); // false - failed
           softAssert.assertAll();
     }
    
     @Test
     void demoTest2() {
           softAssert.assertEquals("welcome", "welcome"); // true - passed
           softAssert.assertAll();
     }
}

Test5: Right Way To Use The Soft Assertion.

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class Test5 {
      SoftAssert softAssert1 = new SoftAssert();
      SoftAssert softAssert2 = new SoftAssert();
      @Test
     void demoTest1() {
           softAssert1.assertEquals("welcome", "Wel"); // false - failed
           softAssert1.assertAll();
     }
     @Test
     void demoTest2() {
           softAssert2.assertEquals("welcome", "welcome"); // true - passed
           softAssert2.assertAll();
     }
}

Followers