An Overview On Annotations Available in TestNG

Annotation ?

Dictionary meaning of Annotation

Annotation has french origin and it is a note by way of explanation or comment added to a text or diagram. Please see image from google search below.

Annotating is any action that deliberately interacts with a text to enhance the reader’s understanding of, recall of, and reaction to the text. Sometimes called “close reading,” annotating usually involves highlighting or underlining key pieces of text and making notes in the margins of the text

Annotations in Java

Annotations became available in Java language itself beginning with version 1.5 of the JDK. annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated

 

Annotations in TestNG

TestNG is a Test framework which is used a lot by folks who are writing Selenium Automation Tests. As it allows to group, run and manage the tests better rather than running individual tests using Java main method.

In TestNG, Writing a test is typically a three-step process:

  1. Write the business logic of your test and insert TestNG annotations in your code
  2. Add the information about your test (e.g. the class name, the groups you wish to run, etc…) in a testng.xml file or in build.xml.
  3. Run TestNG.

How are tests structured in TestNG

Tests in TestNG are structured at 4 levels.

Suite->Tests->TestNG Class->Test Method

Below is a reference from TestNG documentation page (https://testng.org/doc/documentation-main.html)

  • A suite is represented by one XML file. It can contain one or more tests and is defined by the <suite> tag.
  • A test is represented by <test> and can contain one or more TestNG classes.
  • A TestNG class is a Java class that contains at least one TestNG annotation. It is represented by the <class> tag and can contain one or more test methods.
  • A test method is a Java method annotated by @Test in your source.

Here is a diagrammatic representation of the hierarchical structure on how the annotations get executed with reference to this structure.

 

Many of the annotations in TestNG are helping manage and execute things with reference to this pseudo structure.

 

Below are a quick overview on annotations available in TestNG:-

  1. @Test:- Represent a method or a class a a test.
  2. @BeforeSuite:-  An annotated method which runs before all tests run in a suite.
  3. @AfterSuite:- An annotated method which runs after all tests run in a suite.
  4. @BeforeTest:- An annotated method which will run before any test method belonging to the classes inside the <test> tag is run.
  5. @AfterTest:- An annotated method which will run after all test method belonging to the classes inside the <test> tag is run.
  6. @BeforeGroups :- An annotated method which will run before the first method of belonging group run.
  7. @AfterGroups:- An annotated method which will run after the all method of belonging group run.
  8. @BeforeClass:- An annotated method which will run before the first test method in the current class is invoked.
  9. @AfterClass:- An annotated method which will run after the all test methods in the current class is invoked.
  10. @BeforeMethod:- An annotated method which will run before each test method run.
  11. @AfterMethod:- An annotated method which will run after each test method run.
  12. @DataProvider:- Marks a method as supplying data for a test method.
  13. @Parameters:- It is used to pass parameters to @Test method.
  14. @Listeners:- Defines listeners on a test class.
  15. @Factory:- It Marks a method as a factory that returns objects that will be used by TestNG as Test classes.

If you want to read more around these – refer the official documentation

https://testng.org/doc/documentation-main.html

@AgileTA ‘s CPSAT program we cover practical ways of using TestNG for Data Driven testing and much more.

The program is only practical assessment certification program and can be taken in three streams – Java, Python and C#

Just like Selenium the assessment is open too..

Go for it guys

http://cpsat.agiletestingalliance.org/

Leave a Reply