TestNG Tutorials

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.

reportng-1.1.4.jar
velocity-dep-1.4.jar
guice-3.0.jar

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.

@Test(expectedExceptions=ArithmeticException.class)
	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.

TestNG XML example to execute Multiple Classes

In testng.xml file we can specify multiple name (s) which needs to be executed.

In a project there may be many classes, but we want to execute only the selected classes.

We can pass class names of multiple packages also. If say suppose, we want to execute two classes in one package and other class from some other package.

The below is the example testng.xml which will execute the class names that are specified.

Example: