Aeturnum
Published in

Aeturnum

Test Automation and Reporting using TestNG

Hi everyone! in this article, we are going to discuss on Test Automation using TestNG. Additionally, we will be discussing on how to set up Test NG and Test Reporting using TestNG

Well… Here come the questions. What is TestNG?

Let’s find out.

What is TestNG

TestNG is a testing framework in which NG stands for “Next Generation”. It has the ability to generate testing reports in a readable format and can easily come to know how many test cases are passed, failed, and skipped

Test Result structure in Test NG

Using TestNG we can set priorities for test case executions to determine which test case should be executed first.

TestNG annotations are very easy to understand

Ex: @BeforeTest, @AfterTest

In TestNG, we do not use a static main method which will simplify the code.

Setup TestNG in Eclipse IDE

There are three ways to download and install TestNG in Eclipse IDE.

  1. Download and install directly from Eclipse marketplace
  2. Download and install using the “Install new software” feature
  3. Download and install using offline jar files (Only If you are using a portable eclipse version

Here I am going to use the 2nd method which is Download and install TestNG using the “Install new software” feature in Eclipse

  1. Firstly, you need to open the Eclipse IDE and navigate to Help section from the top menu and select “Install new software”

2. It will open the below screen and, then search for TestNG in “Work with” section

3. You will get a few suggestions and select “TestNG — http://dl.bintray.com/testng-team/testng-eclipse-release/

4. Following screen will appear and select “TestNG” checkbox and then click on “Next” without changing any other checkbox selections

5. The below screen will provide an overview of the items to be installed. Simply click on Next

6. You need to accept “the terms of the license agreement” by selecting the radio button option and then click on “Finish” to finish the installation process.

Setting up a Sample Java project with TestNG

  1. Navigate to Eclipse IDE
  2. Create a new project using File → New → Project → Java Project → Next

3. Provide a project name and click on ‘Finish’

4. Now we need to add below resource files to our project

a) Web Driver (for this example I am using the Chrome Driver)

b) Selenium Jar Files

c) Selenium standalone server

Before adding those resource files we need to create a folder structure inside the project as below,

Folder Structure

In order to add folders, right-click on the Project click on New → Folder →

Provide a name for the folder and click on ‘Finish’

After you created the folder structure it should look like below

5. We need to add resources to the above-created folders

Adding resources to the resources folders

a) Download the ChromeDriver (make sure that you are downloading the ChromeDriver version which is support for your installed chrome browser)

b) Download the Selenium Standalone server from https://www.selenium.dev/downloads/ (make sure to download the latest stable version)

c) Download Selenium jar files from https://www.selenium.dev/downloads/

(make sure to download the stable version)

d)Copy the downloaded files and paste those to the relevant folder in the pre-created folder structure.

e) Then we need to add the selenium Jar files and the selenium standalone server to the build path. To do that,

Select all the selenium jar files and the selenium standalone server,

Right Click → Build Path → Add to Build Path

After adding those to the build path the folder structure should look like below

6. Creating a TestNG class

a) Select the relevant package, Right Click → New → Other

Then select TestNG Class

b) Provide a Class name and click “Finish”

Create a Property file to store properties like base URL, driver path, user names, and passwords. The main advantage of having a property file is, we don’t want to hard-coded the above-mentioned fields.

To set up the property file,

1st create a file under the “Resources” folder, name it as “config.properties”

Then create a Java class under the “src” package to read the properties from the “config.properties” file.

Well... That's complete the TestNG setup, Let's create a sample TestNG Test Class.

1st we need to define the Web Driver

public WebDriver driver;

After that, I am going to define a method to access the Base URL. I used @beforeTest annotation before the method, and from that, I am expecting this method to run first before run any priority test cases

Then we are ready to define the test cases based on the priority.

I am going to write a sample test case to verify the page title of the base URL.

We can change the priority of the test case by changing the number. Furthermore, If we need to skip the test case, we can simply define it in the test case as “enabled = false”. Then that test case will not run.

After the execution of all the test cases, we can define a method to close the browser. To define that we can use @AfterTest annotation before the method

Here is the full code segment for the above example

To run the Test we can simply right click on the class → Run As → TestNG Test

How to run multiple Test Classes using TestNG

Suppose our solution consists of several packages and several Test Classes. When a situation like this, how can we run all tests together?

Multiple TestNG test cases

The solution is to create a TestNG.xml file inside the root of the project.

Creating TestNG.xml file

Right-click on the project New → File

Provide the file name as TestNG.xml and Click on ‘finish’

We can define the format as below

Then we can simply run this TestNG.xml file and it will execute the tests according to the defined order in the XML file.

TestNG Reporting

TestNG is capable of generating reports in HTML format.

After running a TestNG project, a new folder called “test-output” is automatically created in the project structure.

If this “test-output” folder is not created automatically,

Rightclick on the Project → Refresh

Under the autogenerated “test-output” folder there is an “index.html” file

Rightclick on “index.html” file → Open with → Web Browser

Then it will open in Eclipse inbuilt web browser and it contains the Test Result summary

Moreover, you can generate a Test Report which can be more readable and emailable, using “emailable-report.html” file

As we did in the above example, all you need to do is, Rightclick on the “emailable-report.html” file → Open with → Web Browser.

That concludes our article as well. I hope you got a picture of what we are trying to achieve. Your comments and suggestions are welcome. You may also share your experience with TestNG.

--

--

--

Aeturnum is a software services organization based in Boston, USA

Recommended from Medium

Raspberry Pi Cam Pan-Tilt Controlled Over Local Internet

How to get rid of “Unnamed:0” column in pandas DataFrame?

Be a part of Call for Code Daily

Learn Microsoft 365 in 5 minutes

Parts Inventory Management

Introducing Kingdom Karnage Art on Jumpnet

The Analysis of Mybb 18.20 From Stored XSS to RCE

Not that Adam Lambert Learns to Code Part 3

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dinusha Amugoda

Dinusha Amugoda

Team Lead — Software Quality Assurance

More from Medium

Visual Regression Testing with webdriver IO

DevOps Working Model for Continuous Testing (CT) With Katalon

Allure Reporting in Selenium using TestNG and Maven

Playwright Automation Introduction