TestNG Tutorials

Include and Exclude Test Methods in TestNG

TestNg provides an option to include or exclude Groups, Test Methods, Classes and Packages using include and exclude tags by defining in testng.xml.

First we will create an examples to use include and exclude tags for Test Methods in a class.

We will create a Class with three Test Methods. In that we will include two test methods and try to exclude one test method.

Below is the example class file with three test methods

Multiple Tests with TestNG.XML

TestNG provides an option to execute multiple tests in a single configuration file (testng.xml). It allows to divide tests into different parts and group them in a single tests. We can group all the tests related to database into one group, Regression tests in one group. And all the test cases related to Unit test cases into one group and so on..

TestNG makes it easy by following the below Syntax to add multiple tests in testng.xml.

Class level annotations in TestNG

TestNG has great feature to define annotations on a class instead of a each test method.

If say suppose there are 10 test methods, where adding @Test on a class level is simpler than adding @Test for each method.

When we make class level @Test annotation, all the public methods of this class will become test methods even if they are not annotated.

We can still define @Test annotation on of the method if we want to add any attributes to particular test method.

Logging with TestNG using Listeners

In TestNG we can generate logs with the help of Listeners. Listeners implement the interface "org.testng.ITestListener". There are methods which gives you a real time information.

You can either extend 'TestListenerAdapter' or implement Interface 'ITestListener' which is a listener for test running.

What are Listeners in TestNG : -

Configuring ReportNG with TestNG for HTML Reports

ReportNG is a simple plug-in for the TestNG unit-testing framework to generate HTML reports as a replacement for the default TestNG HTML reports. You can also customize html report with the help of TestNG listeners.

To use ReportNG reports we need to follow the below three steps:

Step 1: Add the below Jars Files to your project.


TestNG Test Case Priority

In TestNG "Priority" is used to schedule the test cases. When there are multiple test cases, we want to execute test cases in order. Like First we need to execute a test case "Registration" before login.

In order to achive, we use need to add annotation as @Test(priority=??). The default value will be zero for priority.

If you don't mention the priority, it will take all the test cases as "priority=0" and execute.

Timeout Test in TestNG

When a test method is taking longer time than the specified time (in milliseconds) to execute, then that test method will be terminated and marked as failed, this feature is available in both JUnit 4 and TestNG.

While running test methods there can be cases where certain test methods get struck or may take longer time than to complete the execution than the expected. We need to handle these type of cases by specifying Timeout and proceed to execute further test cases / methods

TestNG Example:

Skip Exception Test in TestNG

Using TestNG, we have multiple ways to Skip a test based on our requirement. We can Skip complete test without executing it or we can Skip a test when a specific condition is not satisfied.

In TestNG, @Test(enabled=false) annotation is used to skip a test case if it is not ready to test. We don't need to import any additional statements.

As in JUnit, TestNG will not show you the other test method as Skipped or Ignored. It will not consider that case method at all when the annotation is mentioned as “@Test(enabled=false)”

Exception Test in TestNG

In TestNG we use expectedException with @Test annotation and we need to specify the type of exceptions that are expected to be thrown when executing the test methods.

A below example which throws an exception called “ArithmeticException” when dividing two numbers with denominator value as 0.

	public void dividedByZeroExample1(){
		int e = 1/0;

If we remove the “(expectedException = ArithmeticException.class)”, it will return exception called “java.lang.ArithmeticException: / by zero”

Groups in TestNG

TestNG allows us to perform sophisticated groupings of test methods.

Using TestNG we can execute only set of groups while excluding another set. This gives us the maximum flexibility in divide tests and doesn't require us to recompile anything if you want to run two different sets of tests back to back.

Groups are specified in testng.xml file and can be used either under the or tag. Groups specified in the tag apply to all the tags underneath.