TestNG-Definition, Examples and its Usage

Automation by Artifical Intelligence
Test Automation

TestNG is a testing framework inspired from JUnit and NUnit, its existing features has make it more powerful. TestNg provide seamless integration with Jira, Jenkins, Cucumber, Maven etc

It has given rich libraries for better test development to Software test developers .

Some of the common features(Referred testng.org) of TestNg are-

1-Annotations-Different annotation which control or structure test properly.
2-Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, 3-one thread per test class, etc…).
4-Test that your code is multi thread safe.
5-Flexible test configuration- Parameterisation, annotation etc
6-Support for data-driven testing (with @DataProvider). When data driven tests are required, with the help of data provider, same can be achieved.
7-Support for parameters- Same test can be executed with different input data, commonly sending data from xml file.
8-Powerful execution model (no more TestSuite)- can be integrated with CI tools etc
9-Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, CI tools too etc…).
10-Default JDK functions for runtime and logging (no dependencies).
11-Dependent methods for application server testing.

TestNG is designed to cover all categories of tests: unit, functional, end-to-end,regression and integration, etc

How to install it, in Eclipse-

1- Go to Eclipse MarketPlace, Search TestNG

2- Select the TestNG option, Agree the Terms and Conditions and install it.

Once after TestNg is installed, create a class(without selecting Main checkbox) in your java project.

Find below Widely used annotations in TestNG-

@BeforeSuite- Executes method before start of test suite
@BeforeTest-Executes before test methods of particular class
@Test-Method will be executed as test method
@Test(Priority=n)-Test Method executes in defined priority
@Test(DependsOnMethod={“Method”})-Executed only when dependent method is passed.
@BeforeGroups: The list of groups that this configuration method will run before. This method is runs shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method runs shortly after the last test method that belongs to any of these groups is invoked.

package Selenium_Example;
import junit.framework.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestNG_Example
{
@BeforeSuite
public void BeforeSuite()
{
System.out.println("Before Suite-I am in Setup");
}

@BeforeTest
public void Setup()
{
System.out.println("Beforetest-I am in Setup");
}

@Test(priority=1)
public void Test1()
{
System.out.println("I am in Test1");
}

@Test(priority=2)
public void Test2()
{
System.out.println("I am in Test2");
Assert.fail();

}

@Test(priority=3, dependsOnMethods={"Test2"})
public void Test3()
{
System.out.println("I am in Test1");
}

@AfterTest()
public void PostTestValidation()
{
System.out.println("AfterTest");
Assert.fail();

}

}

Output-

Before Suite-I am in Setup
Beforetest-I am in Setup
I am in Test1
I am in Test2
AfterTest

Test3 is not executed because if you see annotation in Test3 method is (DependsonMethod) which is test2 and since I have deliberately failed that method, Test3 is not executed.

Also, Once after execution, test-output folder is generated, which is basically default reporting of TestNG, It has various reports like index.html file which has summary of execution, emailable-report.html and testng-failed.xml(which contains test cases which are failed).

Also Output statements can explain Sequence of method execution-

i-first beforeSuite annotation is executed, then
ii-Beforetest
iii-@Test-Prioritywise,

Major advantages of TestNG over JUnit:

1-Annotations are easier to understand and implement
2-Test cases can be clubbed more easily using group annotation
3-Parallel testing is possible

Conclusion-

TestNG is a testing framework that is capable of making Selenium tests user friendly. The main advantages of TestNG over JUnit are the following.

1- Annotations are easier to use and understand.
2- Test cases can be clubbed more easily.
3-TestNG allows us to create parallel tests.
4- TestNG reports generative more interactive output after every test execution which is easy to understand as well as strategically designed to present various aspect of test result like-
i-Runtimes of each method.
ii-The chronological order by which methods were executed
5-TestNG is capable of generating HTML-based reports as well as report customization is also possible.

Its Prashant Chaudhary, I have 10+ years of experience in software test automation, performance & process automation with various commercial and open source tools. This blog shares my experience on the topic of Automation. The content published on my blog is solely mine, it does not express the views of any of my employers (current or previous).
No Comments

Leave a Reply

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

Selenium Basic
1
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Test Automation
1
What is ITestListner interface and its Use in Selenium

There are several interfaces available in TestNG that allow you to modify TestNG’s behavior or to give test scripts additional strength. These interfaces are broadly called “TestNG Listeners”. Here are a few listeners which are commonly used: 1- IAnnotationTransformer 2- IAnnotationTransformer2 3- IHookable 4- IInvokedMethodListener 5- IMethodInterceptor 6- IReporter 7- …

Test Automation
How to Capture Screenshot in Selenium Webdriver-3 different ways

Abstract– Capturing screenshot is an essential feature of a test automation framework, They help in defect analyses once after test execution is over. In this blog post we will see how to capture screenshot, there are different ways to capture screenshots. Which includes- 1- Capture full Page 2- Capturng full …

Shares