How to use Annotation in TestNG

Tram Ho

Define

In this article, I will talk about TestNG Annotation, one of the most important things of TestNG that we need to understand. Annotation in TestNG is the comment that defines what will be done next or the functions that will be run next. Annotation is a feature that adds information to an object. It can be used for classes, methods, variables, and parameters. TestNG provides a variety of annotations for different purposes, including annotations for the purpose of: pre- and post-test processing.

Why should we handle it before and after the test?

  • Need to create an environment before testing.
  • It is necessary to clear all states after the test is performed or to perform actions that are not related to the Test method but necessary such as screenshot, delete session or close connection …

TestNG provides 5 annotations in the form of Before / After:

  • BeforeSuite / AfterSuite
  • BeforeTest / AfterTest
  • BeforeGroups / AfterGroups
  • BeforeClass / AfterClass
  • BeforeMethod / AfterMethod

Declaring annotation on a class

Their running order will be shown in the following example:

And we need to add 1 config to the testng.xml file

And this is the result:

From here, we can understand the order in which they are run:

  • Once started, open from Suite> Test> Group> Class> Method
  • When finished, close from Method> Class> Group> Test> Suite

Declaring annotation when extend

In the above, we have seen how to use annation on a class, we will wonder but we write in the form of POM, including BaseTest, do not know what the order of run annotation will be like?

Take a look at the example below:

We have the BaseTest class

Class Test:

And testng.xml file:

Here is the result:

The order of running when extend is:

  • At the beginning: Parent Before> Child Before
  • At the end: Child After> Parent After

In a project, it is not necessary to use all of these annotations, but we need to know the order in which our control code runs in the order we want, such as screentshot capture at the end of each test, initialization. create connection to read Excel file …

Test in TestNG and attributes of Test annotation

Annotation Test :

This is an annotation marking the method or the Class as part of the TestNG Test. If it is marked for Class, then all methods that Public will run, non-public methods will not run. This is where the functions below it will be run like a testcase. For example:

Attributes of Test annotation

alwaysRun: with the default value of false, it will be ignored if it does not have a dependency method. If set to true, the Method will be run even if the dependency method fails. Eg: Test (alwaysRun = true)

enabled: the default value is true. Used to mark whether the method is run or not. If false, that method will be ignored, not run. If true, that method will be run. Eg: Test (enabled = true)

description: Used to add information for the test. This attribute is quite effective when we want to add a description of the test case when the Name method cannot fully describe it. Eg: Test (description = “Print the second test method”)

expectedExceptions: used to determine the exception that a Method may encounter. Eg: Test (expectedExceptions = {IOException.class})

timeOut: specifies the maximum time that the test can run, if the run time is greater than the predefined time, the test result is fail. Eg: Test (timeOut = 500)

dataProvider: fill in data for test method, serve for data-driven testing (will be written in another article)

dataProviderClass: Enter the class name that TestNG will search for the data-provider method mentioned in the dataProvider attribute. By default, it will search for that class or base class.

dependsOnGroups: fill in the list of groups on which the test depends.

dependsOnMethods: fill the list of methods on which the test depends. (will write in another article)

groups: enter the group name, the tests that share the same group name will form a collection. This collection can be run from testng.xml file

In one test, many annotations can be applied together. For example:

The article has a reference website GiangTester Blog

Share the news now

Source : Viblo