Usage of @BeforeGroups/@AfterGroups functions

134 views
Skip to first unread message

Asma Jabir

unread,
Aug 9, 2020, 1:27:21 PM8/9/20
to testng-users
Hi TestNG team,

I was under the impression that when groups are used, the test will be run, one group at a time, and the @BeforeGroups will be run before executing the first test of the group and @AfterGroups will be run after the last test of the group before moving on to the next group. But when I tried an example, this doesn't seem to be the case. Groups are not considered when running and tests seemed to be running in the defined order. The @BeforeGroups is executed before the first test of the group is executed and @AfterGroup is run after the last test of the group.

Could you please help me understand the @BeforeGroups/@AfterGroups execution logic?

Example:

@BeforeGroups("g1")
public void doBeforeGroupsG1() {
System.out.println("testClass1: before groups g1");
}

@BeforeGroups("g2")
public void doBeforeGroupsG2() {
System.out.println("testClass1: before groups g2");
}

@Test(groups = {"g1"})
public void test1() {
System.out.println("testClass1: test1->g1");
}

@Test(groups = {"g2"})
public void test2() {
System.out.println("testClass1: test2->g2");
}

@Test(groups = {"g1"})
public void test3() {
System.out.println("testClass1: test3->g1");
}

@Test(groups = {"g2"})
public void test4() {
System.out.println("testClass1: test4->g2");
}

@AfterGroups("g2")
public void doAfterGroupsG2() {
System.out.println("testClass1: after groups g2");
}

@AfterGroups("g1")
public void doAfterGroupsG1() {
System.out.println("testClass1: after groups g1");
}

Expected order of execution:
doBeforeGroupsG1
test1
test3
doAfterGroupsG1
doBeforeGroupsG2
test2
test4
doAfterGroupsG2

Actual order of execution:
doBeforeGroupsG1
test1
doBeforeGroupsG2
test2
test3
doAfterGroupsG1
test4
doAfterGroupsG2

Thanks
Asma

ocean chow

unread,
Aug 10, 2020, 10:09:08 PM8/10/20
to testng-users
according to the testng document, "beforegroup" only define which method should run before the first running "@test" method in that group, if you want to limit the order among groups, how about "dependsOnGroups" and "beforegroups" together?

在 2020年8月10日星期一 UTC+8上午1:27:21,Asma Jabir写道:

travis.n...@gmail.com

unread,
Jan 12, 2021, 2:43:03 AM1/12/21
to testng-users
According to document:

doAfterGroupsG1  is warranted for running after the last method of group 1: test1 and test3  
doBeforeGroupsG2  is warranted for running before group 2: test2 and test4

I think actual result is as expected from testng execution graph. However, your expected result also make sense too.
Reply all
Reply to author
Forward
0 new messages