TestNG Tutorial: TestNG is an automation testing framework inspired by JUnit and NUnit, adding new functionalities that make TestNG annotations more powerful and easier to use. An open-source framework, where NG stands for Next Generation, TestNG is similar to JUnit but designed with better features, especially for integrated class testing, making it more powerful. It provides the developer to write more powerful and flexible tests with the help of easy annotations, sequencing, grouping, and parametrizing.
Using TestNG, you can easily generate proper reports, to easily come to know how many test cases are passed, test cases failed and skipped. You can separately execute the failed test cases.
Require TestNG In Selenium?
Selenium tests do not generate test results in a proper format. Using TestNG we can generate the test results more properly.
- TestNG will generate the reports in a proper format which includes the number of test cases run, the number of test cases passed, the number of test cases failed, and the number of test cases skipped.
- The same case can be executed multiple numbers of times without using the loop just by using the invocation count keyword.
- We can group the multiple test cases more easily by grouping them into the testng.xml file. So you can prioritize which test case should be executed first.
- We can execute multiple test cases on multiple browsers. i.e, cross-browser testing.
- The TestNG framework can be integrated easily with tools like Jenkins, Maven, etc.
- Annotations that are used in the testing are very easy to understand ex: @BeforeTest, @AfterTest, @BeforeMethod, @AfterMethod, @BeforeSuite, @AfterSuite.
- TestNg will generate the reports in a readable format.
- TestNG can simplify the testing code? There is no need for the main method in our tests.
The Usual structure will look like below and somewhat difficult to read
public class myclass {
public static String baseUrl = “https://www.facebook.com/“;
public static WebDriver driver = new ChromeDriver();
public static void main(String[] args) {
driver.get(baseUrl);
verifyHomepageTitle();
driver.quit();
}
public static void verifyHomepageTitle() {
String expectedTitle = “Facebook - Log In or Sign Up”;
String actualTitle = driver.getTitle();
try {
Assert.assertEquals(actualTitle, expectedTitle);
System.out.println(“Test Passed”);
} catch (Throwable e) {
System.out.println(“Test Failed”);
}
}
}
The TestNG structure will look like below and easier to understand.
public class myclass {
public static String baseUrl = “https://www.facebook.com/ “;
public WebDriver driver;
@BeforeTest
public void setBaseURL() {
driver = new ChromeDriver();
driver.get(baseURL);
}
public void verifyHomepageTitle() {
String expectedTitle = “Facebook - Log In or Sign Up”;
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
@AfterTest
public void endsession() {
driver.quit();
}
}
- Uncaught exceptions are handled automatically by TestNG without terminating the test. These exceptions will as failed in the report.
TestNG advantages over JUnit
- TestNG Annotations are very easy to understand over JUnit
- Method name constraint is not present in TestNG like JUnit, you can specify any method names in TestNG.
- There is no constraint in TestNG like Junit, you have to declare @BeforeClass and @AfterClass which is present in JUnit.
- Grouping of Test cases in easy in TestNG which is not possible in JUnit.
- TestNG allows us to define the dependent test cases and each test case is independent of other test cases.
- Parallel execution is possible in TestNG.
- TestNG allows us to group test cases more easily.
First Test Case using annotations
Before creating a test case, first, we need to create a new project in Eclipse.
Setting up of new TestNG Project
Step 1: Go to File -> Click New -> Click Java Project
Step 2: Enter Project Name as “TestNGProject” and click Next button
Step 3: Click on Libraries tab to add the TestNG Libraries by clicking Add Library
Step 4: Choose “TestNG” on Add Library dialog box and click on Next.
Step 5: Click “Finish” button
You will notice in Libraries list that TestNG is included
Step 6: Now we will add JAR files that contain Selenium API. These files are found in Java client driver downloaded from https://www.selenium.dev/downloads/. To add JAR files click on Add External JARs.
This will navigate to Selenium JAR files and add both jars files under the libs tab and click open.
Your screen looks like below after adding the external JARs
Step 7: Click the Finish button and you can see the created project is visible on the Eclipse Package Explorer window.
Creating a New TestNG Test File
Let’s create a New TestNG File
Step 1: Right-click on src package > Select New > Click on Other
Step 2: Select the TestNG class under the TestNG folder and click on Next.
Step 3: Enter the values indicated below and click on the Finish button. Notice that we named our Java file name as “TestNGProject”.
The eclipse will automatically create the template for our TestNG file as shown below.
Example of our First Test Case
Let us create our first Test Case that will check if the Facebook homepage is correct.
package testngpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class firsttestngfile {
public String baseUrl = " https://www.facebook.com/";
String driverPath = "C:\\geckodriver.exe";
public WebDriver driver ;
@Test
public void verifyHomepageTitle() {
System.out.println("launching chrome browser");
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new ChromeDriver();
driver.get(baseUrl);
String expectedTitle = " Facebook - Log In or Sign Up";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
driver.close();
}
}
From the above code, we notice that
- TestNG does not require any main() method.
- Methods need not be static
- We used the @Test annotation. @Test annotation will tell that the method under it is a test case. In this case, we have set the verifyHomepageTitle() method as our test case, so we placed an ‘@Test’ annotation above it.
- We need to import the package org.testng.annotations.*. Since we use annotations in TestNG.
- We used the Assert class in the code. This Assert class will conduct verification operations in TestNG. To use it, we imported the org.testng.Assert package.
Running the Test
To run the test, Right-click on the program > Run As > TestNG Test. The eclipse will give two outputs – one in the TestNG Results window and the other is in the Console window.
Generating the HTML Reports
Using TestNG we can generate the reports in HTML format.
Step 1: Right-click on our created project name (TestNGProject) in the Project Explorer window and click on the ‘Refresh’ option.
Step 2: You will notice that a “test-output” folder was created. Expand the test-output folder and we can see an index.html file. This HTML file will provide the report of the results of the most recent test run.
Step 3: Double-click on index.html file to open it within Eclipse built-in browser.
Annotations used in TestNG
Here we go through more advanced annotations and their usages.
Multiple Test Cases
In a single TestNG file we can use multiple @Test annotations. By default, @Test methods are executed in alphabetical order. In the below code the methods b_test, c_test, a_test are not arranged in alphabetical order, they will execute as such.
Public class TestNGFile {
@Test
public void b_test() {
Assert.fail();
}
@Test
public void c_test() {
Assert.assertTrue(true);
}
@Test
public void a_test() {
throw new SkipException(“ Skipping a_test...”);
}
}
Parameters
We use the parameter “priority” if you want the methods to be executed in a different order. Parameters are the keywords that will modify the annotations function.
- Parameters will require you to assign a value to them. Just we need to place a “=” next to them followed by the value.
- Parameters are enclosed with parentheses which are placed after the annotation like the code shown below.
TestNG will execute the @Test annotation with low priority value to the high priority value. There is no need that your priority values to be consecutive.
Multiple Parameters
Other than the priority parameter, @Test has another parameter called “alwaysrun” which can set either to true or false. We need to separate with a comma if you want to use two or more parameters in a single annotation which is shown below
@Test(priority = 0, alwaysRun = true)
@BeforeTest and @AfterTest Methods
@BeforeTest: This method will execute prior to the first test case in the TestNG file.@AfterTest: This method will execute after all the test cases in the TestNG file are executed.
package testngpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class firsttestngfile {
public String baseUrl = "http://facebook.com/";
String driverPath = "D:\\Drivers\\geckodriver.exe";
public WebDriver driver ;
@BeforeTest
public void launchBrowser() {
System.out.println("launching Firefox browser");
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver();
driver.get(baseUrl);
}
@Test
public void verifyHomepageTitle() {
String expectedTitle = " Facebook - Log In or Sign Up";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
@AfterTest
public void terminateBrowser(){
driver.close();
}
}
When you run the above code it will execute the methods in the sequence
- 1st – launchBrowser()
- 2nd – verifyHomepageTitle()
- 3rd – terminateBrowser()
TestNG Annotations
@BeforeSuite: This annotation method will run before all tests in this suite run.
@AfterSuite: This annotation method will be run after all tests in this suite run.
@BeforeTest: It runs before any test method inside the test class
@AfterTest: This annotation method will run after any test method inside the test class.
@BeforeGroups: The groups of the list that this configuration method will run before.
@AfterGroups: The groups of lists that this configuration method will run after.
@BeforeClass: This method runs only one time before the first test method in the current class is invoked
@AfterClass: This method runs only once after all the methods in the test in the current class are executed.
@BeforeMethod: It runs before each test method in a program
@AfterMethod: It runs after each test method in a program
@Test: This annotation method is a part of a test case
Conclusion
- TestNG is a testing framework that is used for making Selenium tests to understand easily and for generating reports
- The advantages of TestNG over JUnit are.
- TestNG Annotations are very easy to understand over JUnit
- Method name constraint is not present in TestNG like JUnit, you can specify any method names in TestNG.
- There is no constraint in TestNG like Junit, you have to declare @BeforeClass and @AfterClass which is present in JUnit.
- Grouping of Test cases in easy in TestNG which is not possible in JUnit.
- TestNG allows us to define the dependent test cases and each test case is independent of other test cases.
- Parallel execution is possible in TestNG.
- TestNG allows us to group test cases more easily.
- TestNG will generate HTML-based reports.
- Annotations can use the parameters just like the usual Java methods.
6 Responses