---
title: What is TestNG Annotations?
description: "**What is TestNG Annotations?** In TestNG (a Java testing framework), an **annotation** is a form of metadata—marked by the @ symbol—that you place on classes or methods to tell TestNG how and when to execute them. Instead of hard-coding the…"
url: "https://vinothqaacademy.com/docs/what-is-testng-annotations/"
updated: "2025-08-01"
category: TestNG Framework
tags:
  - Testng Framework
---

# What is TestNG Annotations?

**What is TestNG Annotations?**

In TestNG (a Java testing framework), an **annotation** is a form of metadata—marked by the @ symbol—that you place on classes or methods to tell TestNG how and when to execute them. Instead of hard-coding the test logic or relying on naming conventions, you use annotations to:

- **Define test methods** (e.g. @Test)

- **Specify setup and teardown routines** at various scopes (suite, test, class, method)

- **Drive data-driven tests** (@DataProvider, @Parameters)

- **Control execution order and dependencies** (dependsOnMethods, alwaysRun, etc.)

- **Group tests** and apply group-specific configuration (@BeforeGroups, @AfterGroups)

Under the hood, TestNG reads these annotations via Java’s reflection API at runtime. When it sees, for example, @BeforeMethod on a method, it will automatically invoke that method before each @Test in the same class. This declarative approach makes your test code clean, flexible, and easy to configure without boilerplate.

**Use of Annotations:**

**1. Test Definition**

***@Test***
Marks a method as a test case.
**Explanation:** TestNG will run any method annotated with @Test. You can further control invocation (e.g., priority, groups, dependsOnMethods, enabled) via its attributes.

**2. Configuration (Global)**

***@BeforeSuite***
Runs once before all tests in the entire suite.
**Explanation:** Ideal for suite-wide setup tasks (e.g., loading global configuration).

***@AfterSuite***
Runs once after all tests in the entire suite.
**Explanation:** Ideal for suite-wide teardown (e.g., generating consolidated reports).

**3. Configuration (Test Tag in XML)**

***@BeforeTest***
Runs before any <test> tag in the TestNG XML.
**Explanation:** Use when you have multiple <test> sections and need setup per section.

***@AfterTest***
Runs after all methods in a <test> tag have run.
**Explanation:** Corresponds to cleanup per <test> section.

**4. Configuration (Class Level)**

***@BeforeClass***
Runs once before the first method in the current class is invoked.
**Explanation:** Class-level initialization (e.g., instantiate class-scoped resources).

***@AfterClass***
Runs once after all methods in the current class have run.
**Explanation:** Class-level cleanup (e.g., close resources opened in @BeforeClass).

**5. Configuration (Method Level)**

***@BeforeMethod***
Runs before each test method.
**Explanation:** Prepares prerequisites needed for every individual test method.

***@AfterMethod***
Runs after each test method.
**Explanation:** Cleans up after each test, ensuring isolation between tests.

**6. Group Configuration**

***@BeforeGroups***
Runs before the first test method that belongs to any of the specified groups.
**Explanation:** Use when you want setup to run once per group of tests.

***@AfterGroups***Runs after all test methods in the specified groups have run.
**Explanation:** Performs group-specific teardown.

**7. Parameterization & Data-Driven Testing**

***@DataProvider***
Marks a method as supplying data for a test method.
**Explanation:** Returns an array of parameter sets; tests annotated with @Test(dataProvider="…") will run once per set.

***@Parameters***
Injects parameters from your TestNG XML into test methods or configuration methods.
**Explanation:** Useful for passing values (e.g., browser type, URLs) without hard-coding.

***@Factory***
Marks a method as a factory that returns objects containing test methods.
**Explanation:** Dynamically generates instances of test classes (and thus tests) at runtime.

**8. Dependency & Sequencing**

***dependsOnMethods (attribute of @Test)***Specifies that this test method depends on one or more other methods.
**Explanation:** Ensures that if a prerequisite method fails/skips, dependent methods are skipped.

***dependsOnGroups (attribute of @Test)***Declares dependency on a whole group of test methods.
**Explanation:** Skips tests until all methods in the named groups have passed.

**9. Conditional Skipping**

***enabled (attribute of @Test)***Boolean flag to enable or disable a test method.
**Explanation:** Quickly include/exclude tests without removing the annotation.

***alwaysRun (attribute of configuration annotations)***Forces a configuration method to run even if dependencies have failed or been skipped.
**Explanation:** Useful for cleanup that must occur regardless of earlier failures.

**10. Miscellaneous**

***@Listeners***Associates listener classes (implementations of ITestListener, ISuiteListener, etc.) with your test class.
**Explanation:** Enables custom reporting, logging, or other behavior hooks.

***@Optional***Used in conjunction with @Parameters to specify a default value when the parameter is not found in XML.
**Explanation:** Prevents runtime errors if an expected parameter is missing.
