Assertions and Reporting in TestNG


In the previous post, we have learnt how to skip test case and create dependency between test cases. In this post, we will learn about assertions and Reporting in TestNG.

All test cases must have some verification points or check points. Test case pass or fail result will be dependent on these verification points. In selenium scripts, we use assertions for validating these verification points. It can be inserted any where in the scripts depending on test requirements.

In Selenium , we verify very frequently that element is present on the webpage or not. Because if element is not present then test cases should be failed else perform action on the element. This verification can also be done by assertions. Assertions are very powerful and commonly used statements to make the scripts more responsive.

Let’s understand how to use assertions.

1) Assert.assertTrue() & Assert.assertFalse() : AssertTrue will make the test case passed if it finds the expected condition. As an example, we open Gmail application where we are verifying that username is displayed or not. If it displayed, assertTrue will verify the presence of element and continues execution. But if it does not find the element , it will fail the test case and log the corresponding error message.

In the below example, We are intentionally providing the wrong xpath of Username. So, assertion will fail when it does not find the element.

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class AsserTrueExample {
	
	private static WebDriver driver;
	@Test
	public void Test1(){
		driver = new FirefoxDriver();
		 
	      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
	 
	      driver.get("http://www.gmail.com");
	 
	      // username element in Gmail
	      WebElement GmailUsername = driver.findElement(By.xpath("//*[@id='Emai"));  //wrong Xpath
	      Assert.assertTrue(GmailUsername.isDisplayed());
              System.out.println("It will not reach this statement");
	}

}

Output: Expected result is failed. So, Assertion will take care of that.

AssetTrue in TestNG

 

Note: Assert.assertFalse() works opposite of Assert.assertTrue(). It means that if you want your test to continue only if when some certain element is not present on the page. You will use Assert false, so it will fail the test in case of the element present on the page.


2. Assert.assertEquals(): It will verify if expected condition matches with actual condition. If matches, then keep continuing execution. If fails, will stop the execution and remaining statements will not be executed.

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

public class AssertEqualsExample {
	
	@Test
	public void Test1(){
		
		String expected = "Shekhar";
		String actual = "Sharma";
		
		Assert.assertEquals(actual, expected);
		
		System.out.println("This statement will not be executed as assert is failing in previous statement.");
	}

}

Asserequals in TestNG


Reporting in TestNG:

TestNG provides a default reporting as a HTML file. Let’s run the below code and check the reporting.

package com.testsuite.example;

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


public class Car {
	

	
	@Test(dependsOnMethods={"startCar"})
	public void driveCar(){
		System.out.println("Car driving");
	}
	
	@Test
	public void startCar(){
		System.out.println("Car started");
	}
	
	@Test(dependsOnMethods={"driveCar"})
	public void stopCar(){
		Assert.assertEquals("A", "B");
		System.out.println("Car stopped");
		
	}
}

Output: 

Reporting testNG

How to check the testNG reporting?

Right click on the project and click Refresh.

Refresh the project

 

After refreshing project, you will observe a new folder named as ‘test-output’.

Test-output

 

Expand the folder ‘test-output’. You will see one file named as ‘index.html’. Right click on the file and copy its path and open this file in a browser.

Index html reporting

Once you will open it in browser, you will see the HTML reporting with all the failed and passed result.

 Testng reporting


 

This is box title
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...