TestNG annotations – @BeforeGroups annotation

First, we have to understand what is a group in TestNG and why we make groups?

We have written an altogether different post that explains the TestNG groups in detail with examples. If you want to have a deep understanding of groups in TestNG, we recommend you read that article.
Sometimes, some test cases validate a certain functionality of a system but at different locations or pages of the application, so they usually are in other classes or packages, etc. Still, as they are testing the same functionality, we can club them into a group, and the cases can be recognized from that group name. It will help us understand the functionality of a test case from the group it belongs to.

Now, how to make groups in TestNG?

Well, it’s easy. We have to use the “groups” attribute of the @Test annotation of TestNG. Remember, a test case can also belong to one or more groups, as shown below.

package Test;

import org.testng.Assert;
import org.testng.annotations.Test;

public class CodekruTest {

	@Test(groups = { "group1", "group2" })
	public void test1() {
		System.out.println("Executing test1");
		Assert.assertTrue(true);
	}

	@Test(groups = { "group1" })
	public void test2() {
		System.out.println("Executing test2");
		Assert.assertTrue(true);
	}

}

Now, let’s come back to the @BeforeGroups annotation.

 TestNG will run our tests in the order found in the XML file by default. So, it will always execute the @BeforeSuite and @BeforeTest annotated methods first, then it will go to class, and only then will it find the tests that lie in a particular group and execute the @BeforeGroups annotated method.

When will @BeforeGroups annotated method be executed with respect to other @BeforeXXX annotations?

Let’s see it with an example. Here, we are taking the CodekruTestFirst class and will try to execute the @BeforeGroups annotated method along with the @BeforeTest annotation to see the order in which they are executed.

CodekruTestFirst.java

package Test;

import org.junit.BeforeClass;
import org.testng.Assert;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class CodekruTestFirst {

	@BeforeSuite
	public void beforeSuite() {
		System.out.println("beforeSuite method executed");
	}

	@BeforeClass
	public void beforeClass() {
		System.out.println("beforeClass method executed");
	}

	@BeforeMethod()
	public void beforeMethod() {
		System.out.println("beforeMethod method executed");
	}

	@BeforeTest()
	public void beforeTest() {
		System.out.println("beforeTest method executed");
	}

	@BeforeGroups(groups = "group1")
	public void beforeGroup() {
		System.out.println("beforeGroup method executed");
	}

	@Test(groups = { "group1" })
	public void test1() {
		System.out.println("Executing test1 in CodekruTestFirst class");
		Assert.assertTrue(true);
	}

}

Now, below will be our XML file to run the group1 group.

<suite name="codekru">

	<test name="codekruTest">
		<groups>
			<run>
				<include name="group1" />
			</run>
		</groups>

		<classes>
			<class name="Test.CodekruTestFirst" />
		</classes>
	</test>

</suite>

Output after running the above XML file

beforeGroup method executed
Executing test1 in CodekruTestFirst class

===============================================
codekru
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0

Here, we can see that TestNG only executed the beforeGroup() method because the @BeforeGroups annotated method had the groups attribute associated with it. So, if we want to check for the order of execution for the @BeforeGroups annotation to other annotations, we would have to associate other annotated methods with the “groups” attribute.

package Test;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class CodekruTestFirst {

	@BeforeSuite(groups = "group1")
	public void beforeSuite() {
		System.out.println("beforeSuite method executed");
	}

	@BeforeClass(groups = "group1")
	public void beforeClass() {
		System.out.println("beforeClass method executed");
	}

	@BeforeMethod(groups = "group1")
	public void beforeMethod() {
		System.out.println("beforeMethod method executed");
	}

	@BeforeTest(groups = "group1")
	public void beforeTest() {
		System.out.println("beforeTest method executed");
	}

	@BeforeGroups(groups = "group1")
	public void beforeGroup() {
		System.out.println("beforeGroup method executed");
	}

	@Test(groups = { "group1" })
	public void test1() {
		System.out.println("Executing test1 in CodekruTestFirst class");
		Assert.assertTrue(true);
	}

}

We will rerun the same XML file, and below would be its output –

beforeSuite method executed
beforeTest method executed
beforeClass method executed
beforeGroup method executed
beforeMethod method executed
Executing test1 in CodekruTestFirst class

===============================================
codekru
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0

The What If scenarios

Q – What if we are trying to execute cases from a group that spans multiple classes?

Let’s take one more class, “CodekruTestSecond” and our previous class, “CodekruTestFirst“. Now, the cases within the CodekruTestSecond class belong to the group1 and have their own @BeforeTest annotated method.

CodekruTestFirst.java

package Test;

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

public class CodekruTestFirst {

	@BeforeGroups(groups = "group1")
	public void beforeGroups() {
		System.out.println("beforeGroups method executed in CodekruTestFirst class");
	}

	@BeforeTest(groups = "group1")
	public void beforeTest() {
		System.out.println("beforeTest method executed in CodekruTestFirst class");
	}

	@Test(groups = { "group1" })
	public void test() {
		System.out.println("Executing test1 in CodekruTestFirst class");
		Assert.assertTrue(true);
	}

}

CodekruTestSecond.java

package Test;

import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class CodekruTestSecond {

	@BeforeTest(groups = "group1")
	public void beforeTest() {
		System.out.println("beforeTest method executed in CodekruTestSecond class");
	}

	@Test(groups = { "group1" })
	public void test1() {
		System.out.println("Executing test1 in CodekruTestSecond class");
		Assert.assertTrue(true);
	}

}

Now, let’s try to execute the group1 group.

<suite name="codekru">

	<groups>
		<run>
			<include name="group1" />
		</run>
	</groups>

	<test name="codekruTest">
		<classes>
			<class name="Test.CodekruTestFirst" />
			<class name="Test.CodekruTestSecond" />
		</classes>
	</test>

</suite>

Output after executing the above XML file-

beforeTest method executed in CodekruTestFirst class
beforeTest method executed in CodekruTestSecond class
beforeGroups method executed in CodekruTestFirst class
Executing test1 in CodekruTestFirst class
Executing test1 in CodekruTestSecond class

===============================================
codekru
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Here, we can see that the @BeforeGroup annotated method was only executed once.

Q – What if we are trying to execute cases from a group spanning multiple <test> tags?
<suite name="codekru">

	<groups>
		<run>
			<include name="group1" />
		</run>
	</groups>

	<test name="codekruTest">
		<classes>
			<class name="Test.CodekruTestFirst" />
		</classes>
	</test>

	<test name="codekruTest2">
		<classes>
			<class name="Test.CodekruTestSecond" />
		</classes>
	</test>

</suite>

Output after executing the above XML

beforeTest method executed in CodekruTestFirst class
beforeGroup method executed
Executing test1 in CodekruTestFirst class
beforeTest method executed in CodekruTestSecond class
Executing test1 in CodekruTestSecond class

Here, we would like you to focus on the order of execution.

  • TestNG executes the cases in the order they are found in the XML file
  • So, while executing the XML file, it encountered the CodekruTestFirst class first
  • It then went on to run its @BeforeTest annotated method of the CodekruTestFirst class and then went running the cases inside group1
  • But as the group1 had its @BeforeGroup annotated method, so it executed it first and then executed the cases inside group1
  • After the execution of CodekruTestFirst was completed, it went on to run the CodekruTestSecond class in a similar way
  • But as the @BeforeGroup annotated method was already executed, so, it won’t be executed again. The rest of the execution will be the same as it was with CodekruTestFirst class
Q – What if we have two @BeforeGroups annotated methods in a single class?

Let’s modify our CodekruTestFirst class to have two @BeforeGroups annotated methods.

package Test;

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

public class CodekruTestFirst {

	@BeforeGroups(groups = "group1")
	public void beforeGroup1() {
		System.out.println("beforeGroup1 method executed");
	}

	@BeforeGroups(groups = "group1")
	public void beforeGroup2() {
		System.out.println("beforeGroup2 method executed");
	}

	@BeforeTest(groups = "group1")
	public void beforeTest() {
		System.out.println("beforeTest method executed in CodekruTestFirst class");
	}

	@Test(groups = { "group1" })
	public void test1() {
		System.out.println("Executing test1 in CodekruTestFirst class");
		Assert.assertTrue(true);
	}

}

And below is the XML file to execute the group1 group.

<suite name="codekru">

	<test name="codekruTest">
		<groups>
			<run>
				<include name="group1" />
			</run>
		</groups>
		<classes>
			<class name="Test.CodekruTestFirst" />
		</classes>
	</test>

</suite>

Output after executing the above XML file

beforeTest method executed in CodekruTestFirst class
beforeGroup1 method executed
beforeGroup2 method executed
Executing test1 in CodekruTestFirst class

Here, we can see that TestNG executed both the annotated methods.

Please visit this article to learn more about TestNG annotations.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Other TestNG annotations

Liked the article? Share this on

Leave a Comment

Your email address will not be published.