Friday 22 June 2018

TestNG Assertions and Difference Between Hard Assert and Soft Assert

Hi everyone, In this post we are going to cover very important feature of TestNG which is TestNG Assertions. Why this is important? because no test case is considered complete unless we have assert actual values with expected values.Assertions helps us to verify weather the test case is passed or failed.
There are three types of Assertions in TestNG Hard Assert, Soft Assert . Let us learn all these in detail.

Video Tutorial


What is Assertion and How it Works ?


Consider below example where we are launching google.com and asserting that title should be equals to Google. this is the simplest example of assertion and this is how test case with assertion looks like.

@Test
 public void testCaseVerifyHomePage() {
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();
  driver.navigate().to("http://google.com");
  Assert.assertEquals("Gooooogle", driver.getTitle());
 }

Now if you change your code to assert the title with Expected value as "Doodle" then it will throw Exception that Expected - Doodle but Found - Google.

@Test
 public void testCaseVerifyHomePage() {
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();
  driver.navigate().to("http://google.com");
  Assert.assertEquals("Doodle", driver.getTitle());
 }
In this way assertions helps us to make sure test case is passed or failed and we can catch the bugs with these types of assertions.
Apart from assertEquals there are various types of Assertions as per given below.

Commonly Used Assertions -


assertEqual(String actual,String expected) :- It takes two string arguments and checks whether both are equal, if not it will fail the test.

assertEqual(String actual,String expected, String message) :- It takes three string arguments and checks whether both are equal, if not it will fail the test and throws the message which we provide.

assertEquals(boolean actual,boolean expected) :- It takes two boolean arguments and checks whether both are equal, if not it will fail the test.

assertEquals(java.util.Collection actual, java.util.Collection expected, java.lang.String message) :- Takes two collection objects and verifies both collections contain the same elements and with the same order. if not it will fail the test with the given message.

assertTrue(condition) :- It takes one boolean arguments and checks that a condition is true, If it isn't, an AssertionError is thrown.

assertTrue(condition, message) :- It takes one boolean argument and String message. It Asserts that a condition is true. If it isn't, an AssertionError, with the given message, is thrown.

assertFalse(condition) :- It takes one boolean arguments and checks that a condition is false, If it isn't, an AssertionError is thrown.

assertFalse(condition, message) :- It takes one boolean argument and String message. It Asserts that a condition is false. If it isn't, an AssertionError, with the given message, is thrown.

Hard Assert -  


Hard Assert is a type of TestNG assetion which throws Exception Immediately if the assert statement fails and move on to the next testcase and if there is any code in the current test case after assert statement it will not execute that statement. The google example which we saw earlier is also an example of hard assert. you can try adding code  after assert statement it will not get executed.
In the below example after first assert statement execution the test case execution stops and further statements in the test case are not executed.


@Test
 public void test1(){
   
   Assert.assertTrue(5<1);
   System.out.println(Assertion Failed);
   Assert.assertFalse(1<5);
   System.out.println(Assertion Failed);
   Assert.assertEquals(Passed, Failed);
   System.out.println(Assertion Failed);
 }

Soft Assert - 

To deal with the disadvantage of Hard Assertions, customized error handler provided by TestNG is called Soft Assertion. Soft Assertions are the type of assertions that do not throw an exception when an assertion fails and would continue with the next step after assert statement. This is usually used when our test requires multiple assertions to be executed and the user want all of the assertions/codes to be executed before failing/skipping the tests.


@Test
 public void test1(){
   SoftAssert sa= new SoftAssert();
   sa.assertTrue(5<1);
   System.out.println(Assertion Failed);
   sa.assertFalse(1<5);
   System.out.println(Assertion Failed);
   sa.assertEquals(Passed, “Failed);
   System.out.println(Assertion Failed); 
                        sa.assertAll()
 }


1 comment: