Working with TestNG Annotations (Part1)

14 / Jul / 2017 by Atul Sharma 0 comments

TestNG is a Java Framework to set up the execution flow of the code and for reporting purpose.

 Annotations used in TestNG:

Annotation Description
@BeforeSuite BeforeSuite annotation method runs only once before all tests from the suite.
@AfterSuite AfterSuite annotation method runs only once after all tests from the suite.
@BeforeClass BeforeClass Method runs only once before the first test method. The Current Class method will be one from which it is invoked.
@AfterClass AfterClass annotation method runs only once when all the test methods mentioned in the current class are executed.
@BeforeTest BeforeTest method runs before any test method inside the <test> tag is run. (in testNG.xml file). It runs before the execution of classes takes place.
@AfterTest AfterTest method runs after any test method inside the <test> tag is run. (in testNG.xml file). It runs after the execution of classes takes place.
@BeforeGroups BeforeGroups method is run before the first test method, and the condition is that the group should contain the method which is invoked.
@AfterGroups AfterGroups method is run after the first test method, and the condition is that the group should contain the method which is invoked.
@BeforeMethod The BeforeMethod runs before each test method.
@AfterMethod The AfterMethod Annotation runs after each test method.
@DataProvider DataProvider annotation method returns an Object[ ][ ] type value, where each Object[ ] can be assigned as the parameter of the test method that wants to receive the data from this DataProvider annotation method.
@Listeners Defines listeners on a test class.
@Parameters Parameters annotation is used to pass the parameter’s value in the test.
@Test Marks a class or a method as a part of the test. Or in simple words, makes a method as the test method.

Order of Execution of TestNG Annotations:

@BeforeSuite

@BeforeTest

@BeforeClass

@BeforeMethod

@Test

@AfterMethod

@BeforeMethod

@Test

@AfterMethod

@AfterClass

@AfterTest

@AfterSuite

Example:

[js]import org.testng.annotations.Test;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.AfterMethod;

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 TestngAnnotation {

// test case 1

@Test

public void testCase1() {

System.out.println("in test case 1");

}

// test case 2

@Test

public void testCase2() {

System.out.println("in test case 2");

}

@BeforeMethod

public void beforeMethod() {

System.out.println("in beforeMethod");

}

@AfterMethod

public void afterMethod() {

System.out.println("in afterMethod");

}

@BeforeClass

public void beforeClass() {

System.out.println("in beforeClass");

}

@AfterClass

public void afterClass() {

System.out.println("in afterClass");

}

@BeforeTest

public void beforeTest() {

System.out.println("in beforeTest");

}

@AfterTest

public void afterTest() {

System.out.println("in afterTest");

}

@BeforeSuite

public void beforeSuite() {

System.out.println("in beforeSuite");

}

@AfterSuite

public void afterSuite() {

System.out.println("in afterSuite");

}

}

Next, <strong>testng.xml</strong>

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "<a href="http://testng.org/testng-1.0.dtd">http://testng.org/testng-1.0.dtd</a>" >

<suite name="Suite1">

<test name="test1">

<classes>

<class name="TestngAnnotation"/> <!– Java Class File name to mentioned here –>

</classes>

</test>

</suite>[/js]

The output:

in beforeSuite

in beforeTest

in beforeClass

in beforeMethod

in test case 1

in afterMethod

in beforeMethod

in test case 2

in afterMethod

in afterClass

in afterTest

in afterSuite

 

===============================================

Suite

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

The execution procedure is Summarized as follows:

  • Firstly, beforeSuite() method is executed and this is executed only once.
  • Last, of all, the afterSuite() method is executed and is executed only once.
  • Then annotations beforeTest(), beforeClass(), afterClass(), and afterTest() methods respectively of the order mentioned are executed only once.
  • beforeMethod() method executed before execution of each of the test case.
  • afterMethod() method executed before execution of each of the test case.
  • In between beforeMethod() and afterMethod(), each test case method annotations is executed.

Executing the testNG.xml file with Package rather than Classes.

Now, let’s say that there is one package named as ‘testNGPackage’ and there are multiple files in the package which are tests with annotation @Test, say TestFile1, TestFile2…. TestFile10.

So in testng.xml

[js]<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "<a href="http://testng.org/testng-1.0.dtd">http://testng.org/testng-1.0.dtd</a>" >

<suite name="Suite1">

<test name="test1">

<classes>

<class name="TestFile1"/>

<class name="TestFile2"/>

<class name="TestFile3"/>

<class name="TestFile4"/>

<class name =”TestFile10”/>

</classes>

</test>

</suite>

We can Simply say like:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "<a href="http://testng.org/testng-1.0.dtd">http://testng.org/testng-1.0.dtd</a>" >

<suite name="Suite1">

<test name="test1">

<packages>

<package name=”testNGPackage”/>

</packages>

</test>

</suite>[/js]

EXCEPTIONS IN TESTNG HANDLING:

There might be certain cases where there will be exceptions due to which the test will get fail and might hamper the execution of the Test cases and TestNG Report Cases.

But this can be handled in TestNG. In TestNG, we can handle the Unchecked exception as well, due to which the test case is failed.

To do the same, below is the syntax and example:

[js]@Test(expectedExceptions=ArithmeticException.class)

Example:

import org.testng.annotations.Test;

public class TestNGExamples {

@Test(expectedExceptions=ArithmeticException.class)

public void dividedByZeroTestCase1() throws ArithmeticException{
int num = 1/0;

}

@Test

public void dividedByZeroTestCase2(){

int num = 1/0;

}

}[/js]

When we execute the above code, the test method “dividedByZeroTestCase1” will return as “Passed” as we are handling the exceptions and the Test Method “dividedByZeroTestCase2” will return output as “Failed” with the exception as ‘“java.lang.ArithmeticException: / by zero”.

When we add the annotation @Test(expectedExceptions=<Type of Exception>.class), then we are telling the TestNG framework that there might be chances of occurrence of the exceptions which then needs to be handled.

Thus, at the time of execution, the flow will not be hampered if the exception occurred. Rather, it will continue the execution with marking the highlighted test case as PASS and mention in detail that the exception is handled.

This is how the exception handling is done in TESTNG.
Multiple exceptions can also be handled in TESTNG. It is done as follows:

@Test(expectedExceptions = {ArithmeticException.class, FileNotFoundException.class})

ENABLE AND DISABLE TESTS IN TestNG

Understand a scenario, where you have to execute the tests but have to exclude some as it is not required to be executed for the flow.

This can be done as:

@Test(enabled=<true/false>)

If marked as false, it won’t be executed else it will be executed.

TestNG TEST CASE PRIORITY

In TestNG, “Priority” is used to schedule the test cases i.e. in order to execute in particular order. For e.g., First, we need to execute a test case “registerAccount” before “login”.

In order to achieve prioritization, we need to add the 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.

Have a look at below example for the prioritization of the test cases.

The priority for test case “registerAccount” is not defined, and it will get executed first and then the other test cases based on priority.

[js]import org.testng.annotations.Test;

public class testNGPriorityExample {

@Test

public void registerAccount()

{

System.out.println("First register your account");

}

@Test(priority=2)

public void sendEmail()

{

System.out.println("Send email after login");

}

@Test(priority=1)

public void login()

{

System.out.println("Login to the account after registration");

}

}[/js]

‘RegisterAccount’ test case will be executed first and then “login” and in the last “sendEmail” as per the priority mentioned with methods.

NOTE: In case, two or more methods have the same priority, then it will execute in alphabetical order.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

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