What is TestNG Annotation Syntax?


TestNG is a powerful testing framework, an enhanced version of JUnit which was in use for a long time before TestNG came into existence. NG stands for 'Next Generation'.

TestNG framework provides the following features −

  • Annotations help us organize the tests easily.

  • Flexible test configuration.

  • Test cases can be grouped more easily.

  • Parallelization of tests can be achieved using TestNG.

  • Support for data−driven testing.

  • Inbuilt reporting.

Java 1.5 or higher version allows to interact with TestNG. To run any test or code only in java we must use java main method. TestNG provides us a framework that runs java code without using java main method. Apart from this, better code maintainability, reporting, flexible test configurations are additional advantages of using TestNG along with Selenium.

TestNG Annotation in Java

Annotations were formally added to the Java language in JDK 5 and TestNG made the choice to use annotations to annotate test classes. Following are some of the benefits of using annotations. More about TestNG can be found here

  • TestNG identifies the methods it is interested in by looking up annotations. Hence, method names are not restricted to any pattern or format.

  • We can pass additional parameters to annotations.

  • Annotations are strongly typed, so the compiler will flag any mistakes right away.

  • Test classes no longer need to extend anything (such as TestCase, for JUnit 3).

User can utilize all available TestNG annotations in selenium. Few of those are following:

Sr.No.

              Annotation & Description

1

@BeforeSuite

The annotated method will be run only once before all the tests in this suite have run.

2

@AfterSuite

The annotated method will be run only once after all the tests in this suite have run.

3

@BeforeClass

The annotated method will be run only once before the first test method in the current class is invoked.

4

@AfterClass

The annotated method will be run only once after all the test methods in the current class have run.

5

@BeforeTest

The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.

6

@AfterTest

The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.

7

@BeforeGroups

The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.

8

@AfterGroups

The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

9

@BeforeMethod

The annotated method will be run before each test method.

10

@AfterMethod

The annotated method will be run after each test method.

11

@Test

Marks a class or a method as part of the test.

TestNG Annotation Syntax

TestNG Annotation Syntax is uniform across all above basic annotations.

Syntax is

@<Annotation Name>
<modifier> <returnType> <functionName>(){
//to do code
}

Example

@BeforeTest
Public void initialSetUp(){
	System.out.println(“inside set up code”)
}

Updated on: 21-Aug-2023

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements