top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Annotations and Annotation Helper Attributes in TestNG

In This blog we are going to see what is annotations and how we have to use it

with examples , workflow of that and why we need that . we will discuss about Data Provider, Annotation helper attributes and how it helps annotations.


What is Annotations?

An annotation is a tag or metadata that provides additional information about a class, interface or method. TestNG make use of these annotations to provide several features that aid in the creation of a robust testing framework.

Annotations support TestNG by controlling the execution flow of our program. They are written above their respective method and prefixed with an at “@” symbol. We can place an Annotation anywhere on the editor because it’s automatically connected to the method. In this section, we’ll cover the Configuration Annotations and Test Annotation.

Here, we will refer to each TestNG annotation in detail and study their syntax and usage.


Configuration Annotations

Configuration Annotations are like Pre-Conditions and Post-Conditions. Our test is the condition. A Pre-Condition is executed before our test and a Post-Condition is executed after our test. The following lists each Configuration Annotation:

  • @BeforeMethod

  • @AfterMethod

  • @BeforeClass

  • @AfterClass

  • @BeforeTest

  • @AfterTest

  • @BeforeSuite

  • @AfterSuite

@Before

All @Before Annotations helps us to configure and set up our test. we can set the system property, load the browser, and load the Application Under Test (AUT).


@After

All @After Annotations helps us to clean up important things like quitting the driver after our test.



Test Annotation

A Test Annotation is written as @Test and identifies our method as a Test Method. We have the option of marking our methods or entire class with @Test. After executing our test, the Console and Results tab only show results for public methods. Other methods such as default or private are allowed to be annotated with @Test but will not execute as a Test Method.


Workflow of TestNG


@Test

This marks a class or a method as a part of the test.

Example


@BeforeMethod

This method will execute before each test method.

Example

@AfterMethod

This method will run after each test method is executed.

Example

@BeforeClass

This method will run before the first method invokes of the current class.

Example


@AfterClass

This method will execute after all the test methods of the current class execute.

Example

@BeforeTest

This method runs before the execution of first @Test method. It runs only once per class.

Example

@AfterTest

This method executes after the execution of all the test methods that are inside that folder.

Example



@BeforeSuite

This method runs before the execution of all other test methods. It will run only once before all tests in the suite have to run.

Example

@AfterSuite

This method runs after the execution of all other test methods. It will run only once after all tests in the suite have run.

Example

@BeforeGroups

This method run before the test cases of that group execute. It executes just once.

@AfterGroups

This method run after the test cases of that group execute. It executes just once.


@Parameters

This annotation is used to pass parameters to test methods. And we can pass the parameters values just once while per execution in TestNG.

Example



@DataProvider

This annotation is used for providing any data to the parameterization. Data Providers pass the different parameters on a single test in a single execution.

Example



@Factory

This annotation is used to mark any test method as a factory and returns the object (Object[ ]) that is used as TestNG classes by the TestNG file.


Why do we need @Factory annotation?

Sometimes we may need to run a set of tests with different data values. To achieve this we may define a separate set of tests inside a suite in the testng XML and test the required scenarios. The problem with this approach is that if we get an extra set of data, we will need to add more tests.

TestNG solves this problem by providing the @Factory annotation feature. @Factory defines and creates tests dynamically at runtime.

Example

In this example we have two different tests with two different data values. TestNG will scan for test methods in these classes and run those tests.

Output

Combining @Factory and @DataProvider

The @DataProvider feature can also be used with the @Factory annotation for creating tests at runtime. This can be done by declaring the @Factory annotation on a constructor of a class or on a regular method.

Example

Output



@Listeners

This annotation is used to mark any test method as a factory and returns the object (Object[ ]) that is used as TestNG classes by the TestNG file.


what is Annotation Helper Attributes?

Test attributes are the test specific, and they are specified at the right next to the @Test annotation.


description

With this 'description ' attribute we can give information about the test. It is a string which is attached to the @Test annotation.

Example


groups

With this 'groups' attribute we can perform groupings of different test methods.

Example


priority

With this 'Priority' attribute we can define the order in which order we want the test cases to be executed. If test priority is not defined while, running multiple test cases, TestNG assigns all @Test a priority as zero(0).

It will run in alphabetical order. Priority determines the sequence of the execution of the test cases. The priority can hold the integer values between -5000 and 5000.

If we set priority then it will run from lowest to highest.

Example

dependsOnMethods

When the one test method wants to be dependent on the another test method, then this could be possible by the use of

'dependsOnMethods' attribute. If the first test method fails, then the dependent method on the first test method, i.e., the second test method will not run.

Example


enabled

The 'enabled' attribute contains the Boolean value. By default, its value is true. If you want to skip some test method, then you need to explicitly specify 'false' value.

Example


timeout

If one of the test cases is taking a long time due to which other test cases are failing. To overcome such situation, you need to mark the test case as fail to avoid the failure of other test cases. The time Out is a time period provided to the test case to completely execute its test case.


Example: 1

In this example we used method wise timeout

Example : 2

In this example we used Class wise timeout

Conclusion

TestNG Annotations and attributes are used to execute our test cases in smarter way. They make Selenium test scripts more manageable, sophisticated and effective. Using them is extremely helpful for testers, and makes their lives much easier.

I believe that you have learnt about TestNG Annotations and how to use attributes in testcases through this blog. Thank You For reading my blog.

32 views0 comments

Recent Posts

See All