How to Write TestNG Test Case with Example
In this tutorial, we will learn how to write test cases in TestNG with the help of examples.
Before continue, hope that you have successfully installed TestNG plugin into your Eclipse. TestNG plugin helps to execute TestNG tests or suites using Eclipse.
If not install so far, I recommend that you just go to the previous tutorial and learn the basics of TestNG and install TestNG plugin in your Eclipse step by step.
Now let’s move to the main topic and we learn how to write our first TestNG test case in simple steps. Before creating our first TestNG test, first, we have to create Java project into Eclipse to add TestNG test classes.
Java Project
A Java project is a place that contains Java source codes and related files to compile our program. It can be used to maintain proper management of source code and related files.
Let’s create a Java project into Eclipse. If you already know to create Java project in Eclipse, you can skip this section.
How to Create Java Project in Eclipse?
To create a Java project in eclipse, perform the following simple steps.
Step 1: Open Eclipse and go to File > New > Other. A dialog window with multiple actions will be displayed.
Step 2: Select the Java project in Eclipse, as shown in the following screenshot and click on Next.
Step 3: A dialog window will be opened to fill the project name. Enter “FirstTestNGProject” as the project name and then press the Finish button to create a project.
This will create a new Java project in Eclipse.
[adinserter block=”5″]
Step 4: Now go to Project > Properties. Select Java Build Path on the left-hand side of the Properties dialog window as shown in following below screenshot. This will show the build path for the newly created project.
Step 5: Click on Libraries Tab and then click on the Add Library option.
Step 6: Now, select TestNG on Add Library dialog window as shown in the below screenshot and click on the Next button.
Step 7: Click on Finish on the next dialog window. This will set the default TestNG library for this project. You will notice that TestNG is included on the library list.
Step 8: Now add the JAR files that contain the Selenium API. We downloaded these files from https://docs.seleniumhq.org/download/ when we were installing Selenium and Eclipse in the previous chapters.
Click on Add External JARS… and then navigate to that place where you have placed Selenium JAR files. After adding the external JARs, your screen will look like this:
Step 9: Click on the Finish button and verify that your FirstTestNGProject is visible on the Eclipse Package Explorer window.
We have successfully created a new Java project in Eclipse and added Selenium API and TestNG library to the build path of the project.
Now we can move forward and can create our first TestNG test class for this newly created Java project.
Creating First TestNG Test Case with Example
Perform the following important steps to create the first TestNG test case.
Step 1: Right-click on the “src” package folder and select New > Other. It will open a new wizard window in eclipse.
[adinserter block=”2″]
Step 2: Select TestNG class option from the wizard window and click on Next as shown in the below screenshot.
Step 3: Now, click on the browse button on the next window and select Java project where you need to add your class. You can see this in the below image. Click on the Ok button.
Step 4: Enter package name and test class name indicated below on the appropriate input box as shown in the below screenshot and click on Finish button.
As you can see in the above screenshot, the window is also giving you an option to select different annotations while creating a new TestNG class.
If you select them, the plugin will declare dummy methods with these annotations while creating the class. Eclipse will automatically generate a template for TestNG class, as shown below.
First Test Case Coding Example
Let’s write the first TestNG test case in which we will get the title of Google home page as output. Write the following code to your newly created test case as shown below step by step.
Program code 1:
package firstTestNGProject; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class FirstTest { // Create WebDriver reference. public WebDriver driver; @Test public void homePage() { // Create an object of FirefoxDriver class to open the Firefox browser. driver = new FirefoxDriver(); System.out.println("Launching Firefox browser"); // Declare URL of google home page with data type string. String URL = "https://www.google.com"; // Creating string literal. // Call get() method and pass URL as a parameter. // This method is used to open the URL of web applications in the browser. // It will wait until the whole page gets loaded. driver.get(URL); // Call getTitle() method to get the title of home page. // Since this method will return the title of homepage as a string. // That's why we will store it using a variable getTitle of type String. String getTitle = driver.getTitle(); System.out.println("Title of home page: " +getTitle); // Call close() method to close browser. driver.close(); } }
Key points:
Now notice the following key points in the above TestNG test case example program.
1. TestNG does not require a main() method.
2. Methods need not be static.
3. A class named FirstTest has a test method, denoted by the TestNG annotation @Test mentioned before the homePage() function.
4. When the test method will be executed, the following actions will be performed by code.
- Firefox browser will be launch.
- The homepage of Google will open.
- We will get the output as the title of the homepage.
- At last, the browser will be closed.
5. Since we used annotations in the above test case, we needed to import the package org.testng.annotations.*.
Running TestNG Test Case
In this section, we will learn how to run a newly created test case through Eclipse.
To run the test, simply select test class “FirstTest.java” in the package of src and then right-click on it, selecting Run as from the menu, and choosing TestNG test.
Eclipse will provide two results for your test execution, one in the Console window and the other on the TestNG Results window.
Output on Console Window:
Output: Launching Firefox browser Title of home page: Google PASSED: homePage =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 ===============================================
TestNG Result Window:
Steps to Generate TestNG HTML Report
TestNG has the ability to generate HTML reports in a readable format. Follow all the steps to generate an HTML test report.
Step 1: In the previous section, we have successfully run our test case. Now we will generate reports in HTML format.
For this, go to Project Explorer window on the left side and right-click on the project name (FirstTestNGProject) in the Project Explorer window and then click on the “Refresh” option.
Step 2: Notice that a “test-output” folder has been created in the Project folder. Expand it and look for an index.html file as shown in the screenshot.
This HTML report file is the result of the most recent test run.
Step 3: Double-click on that index.html file to open it within the built-in web browser of Eclipse. You can also refresh this page any time by simply pressing F5 just like in an ordinary web browser, when you want to rerun your test.
This tutorial has covered all the important steps related to how to write TestNG test case with the help of example and screenshots. Hope that you will have successfully created a test case using TestNG and generated test reports. In the next, we will understand different annotations in TestNG.
Thanks for reading!!!