First Test Case with TestNG

Print View Mobile View

We saw how to install TestNG in Eclipse IDE and how to create a new project and set up a package. Now lets start writing our first test case using TestNG.

We’ll first write a simple script that prints “Welcome to SumTips!” in the console to make sure TestNG is working properly. To create a new class, right-click on Package > Go to TestNG > Create TestNG Class. This will open a new dialog similar to the one seen below:
New TestNG class wizard

Source folder and the package name will be populated based on the selections you made earlier. Enter “FirstTestCaseDemo” for “Class name.” Don’t select anything from “Annotations” for now. Click “Finish.” You should now have a new class in your Eclipse IDE with an empty f():
Empty TestNG Testcase
Let’s change the code a bit to print what we intended:

File: FirstTestCaseDemo.java

package TestNGDemo;

import org.testng.annotations.Test;

public class FirstTestCaseDemo {
	@Test
	public void Test() {
		System.out.println("Welcome to SumTips");
	}
}

To run this test case, run the file as you normally do using Ctrl + F11 or navigate to Run > Run As > TestNG Test. This will give us an output that the test was successfully executed:
First Test Case with TestNG
Let’s write another script. This time we are going to write a test case to launch Google Chrome browser, open SumTips.com homepage, verify if correct page title is shown, print a console message, and finally close the browser.

Start creating a new class named “FirstTestCase” like we did above; and this time we are going to use two Annotations: @BeforeMethod and @AfterMethod. We have a separate post for all TestNG Annotations here. Click on the check boxes corresponding to each method and hit “Finish.”

The newly created TestNG class should be listed under the package and you’ll have a class file with three empty methods. Now, let’s start coding.

File: FirstTestCase.java

package TestNGDemo;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;

public class FirstTestCase {
	public WebDriver driver;

	@Test
	public void f() {
		// Open SumTips homepage
		driver.get("http://sumtips.com");
		// Get Actual page title
		String actualTitle = driver.getTitle();
		// Print Actual page title
		System.out.println(actualTitle);
		// Expected Page title
		String expectedTitle = "SumTips";
		// Check if Actual and Expected Page title match using Assert
		Assert.assertEquals(actualTitle, expectedTitle);
	}
	@BeforeMethod
	public void beforeMethod() {
		// Set path of Chrome Driver
		System.setProperty("webdriver.chrome.driver", "C:\\Files\\chromedriver.exe");
		// Create a new instance of the Chrome driver
		driver = new ChromeDriver();
	}

	@AfterMethod
	public void afterMethod() {
		//Close driver
		driver.quit();
	}

}

After executing this script, you should be able to view the result as in the below image:
TestNG Results window
In the Console window you can see a text-based report of the test case, and if you switch over to TestNG Results window, you should be able to see a graphical result with the number of Passed, Failed and Skipped test cases. For a more in-depth information, you can always go to the “All Tests” tab.

TestNG also prepares HTML reports each time a test case/suite is expected. You can find these reports in the “test-output” folder in the Project directory.

The execution flow of the script goes like this:

  1. @BeforeMethod : Create a new instance and launch Google Chrome browser.
  2. @Test : Opens the base URL, gets the title of the page, verifies whether the title matched with the expected title, and throws out the result. Asserts is used to verify the conditions of the test and decide whether test has passed or failed.
  3. @AfterMethod : Close Google Chrome

Note:

  • Main() is not need with TestNG
  • Methods don’t have to be static
  • You can have multiple test cases with multiple @Test methods in the same class
  • org.testng.annotations.* packages is imported whenever annotations are used
  • org.testng.Assert package is imported for Assert class, which we have used for verification.