Open In App

TestNG @BeforeGroups Annotations

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Concept Annotations is introduced in Java 1.5 (jdk5). The Popular Annotation in Java is @override. We use the same annotation concept in TestNG.

In TestNG, there are 10 Annotations

  1. @BeforeSuite
  2. @BeforeGroups
  3. @BeforeClass
  4. @BeforeMethod
  5. @AfterGroups
  6. @AfterMethod
  7. @AfterClass
  8. @AfterTest
  9. @AfterSuite

In this article, we will learn about @BeforeGroups.

What is @BeforeGroups?

@BeforeGroups is one of the TestNG Annotations. When you annotate a method with @BeforeGroups, TestNG ensures that this method is invoked before any test method belonging to the specified groups is executed. This annotation allows developers to specify various actions to be taken before all the methods of the current group within a class finish their execution.

Example of @BeforeGroups

Let’s understand the @BeforeGroups annotation through an example.

Step 1: Open the Eclipse IDE.

Step 2: Create a Maven Project.

Step 3. After Creating Maven Project, the project explore will look like below image.

Screenshot-(306)

Package Explorer


Step 4. Create a TestNG Class that contain @BeforeGroups.

Before_Group.Java

Java
package com.geeksforgeeks.test;

import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class Before_Group {

    @BeforeGroups(groups = {"authentication"})
    public void setUpAuthentication() {
        System.out.println("Database setup complete for authentication tests.");
    }

    @Test(groups = {"authentication"})
    public void testLogin() {
        System.out.println("Login test executed.");
    }
    @Test(groups = {"authentication"})
    public void testSignup() {
        System.out.println("Signup test executed.");
    }
    @Test(groups = {"authentication"})
    public void testSignout() {
        System.out.println("Signout test executed.");
    }

}


Now, let’s explain what this code does:

  • Package Declaration:
    • The code is in the com.geeksforgeeks.test package.
  • Imports:
    • The code imports annotations from the TestNG framework (org.testng.annotations.BeforeGroups and org.testng.annotations.Test).
  • Before_Group Class:
    • This is the main test class.
  • setUpAuthentication Method (@BeforeGroups(groups = {“authentication”})):
    • This method is annotated with @BeforeGroups, indicating that it should run before any test method belonging to the specified group.
    • The groups = {“authentication”} parameter specifies that this setup method is associated with the “authentication” group.
    • Inside this method, it prints “Database setup complete for authentication tests.” to indicate that the database setup for authentication tests is complete.
  • Test Methods (@Test(groups = {“authentication”})):
    • Each test method is annotated with @Test, indicating that it is a test case.
    • All test methods are associated with the “authentication” group using groups = {“authentication”}.
    • There are three test methods: testLogin(), testSignup(), and testSignout().
    • Each test method prints a message indicating the action being tested.


Step 5: Now, we create the AnnotationsTest.xml file to configure the Before_Groups.Java.

XML
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="suite">
    <test name="test1">
        <classes>
            <class name="com.geeksforgeeks.test.Before_Group"/>
        </classes>
    </test>
</suite>


Step 6: Run the AnnotationsTest.xml. Right click on the AnnotationsTest.xml file, move the cursor down to Run As and then click on the 1 TestNG Suite.

Output

beforegroup-output

Output




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads