TestNG tutorial for beginners

Here I will learn and teach you some basics of TestNG.

What is TestNG

TestNG is a testing framework which is used for a different range of testing. e.g. Unit Testing, Integration Testing etc.

How to Write Tests

  1. Create test file where you will write business logic which needs to be tested.
  2. Use TestNG annotation to identify different method execution levels, Groups, and Suites.
  3. Create build.xml or testng.xml file.
  4. In above file provide details of test class and group or suites.
  5. RUN TestNG.

 

TestNG eclipse example

Here i will teach you how to install TestNG eclipse plugin and use the same to create test and run them.

Note*: Copied from TestNG Site

For the Eclipse plug-in, we suggest using the update site:

Select Help / Software updates / Find and Install.
Search for new features to install.
New remote site.
For Eclipse 3.4 and above, enter http://beust.com/eclipse
For Eclipse 3.3 and below, enter http://beust.com/eclipse1
Make sure the check box next to URL is checked and click Next.
Eclipse will then guide you through the process.

 

Once plugin installed next step would be to create a simple test file and run it.

Create Dynamic Web Project(TestNGWebProject) in Eclipse. To know how to create it please visit here. Once project created, create TestNG class using a template.

 

Next screen as shown above several options will be available. Here we can provide the package name, class name and different annotation that can be used. Also we can optionally provide XML suite file(TestNG.xml).

Note*: You need to add testng JAR from here.
FirstTest.java : Sysout has been added to identify the sequence of different methods.

package com.jbt;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;

public class FirstTest {
@Test(dataProvider = "dp")
public void f(Integer n, String s) {
System.out.println("Inside Data Provider");
}

@BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}

@AfterMethod
public void afterMethod() {
System.out.println("After Method");
}

@DataProvider
public Object[][] dp() {
return new Object[][] { new Object[] { 1, "a" },
new Object[] { 2, "b" }, };
}

@BeforeClass
public void beforeClass() {
System.out.println("Before Class");
}

@AfterClass
public void afterClass() {
System.out.println("After Class");
}

@BeforeTest
public void beforeTest() {
System.out.println("Before Test");
}

@AfterTest
public void afterTest() {
System.out.println("After Test");
}

@BeforeSuite
public void beforeSuite() {
System.out.println("Before Suite");
}

@AfterSuite
public void afterSuite() {
System.out.println("After Suite");
}

}

TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<classes>
<class name="com.jbt.FirstTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

How to Run Test

Till now we have created Test class and Suite XML file. Next step would be to create a run configuration and run our test. To create run configuration go to Run >> Run Configuration. Here you will find TestNG option in the left navigation. Right-click same option and select New as shown below.

When new button is clicked you will get another window which will ask you different options like Class/ Method / Groups/ Package/ Suite. As we have already created suites for this newly created class(FirstTest.java) select the same(TestNG.xml) using Browse button.

 TestNG4

Click  Apply and Run.

The output will be like below.

 

[TestNG] Running:
  C:\Gautam\workspace\TestNG\TestNGWebProject\src\TestNG.xml

Before Suite
Before Test
Before Class
Before Method
Inside Data Provider
After Method
Before Method
Inside Data Provider
After Method
After Class
After Test
After Suite

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Here you can identify the sequence of test method execution.

This is it for this article. Will write more article once i learn more about TestNGG. 🙂

4 Comments TestNG tutorial for beginners

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.