Skip to content

Commit 49dbdc9

Browse files
committed
Restore BeforeGroups|AfterGroups functionality back
Closes #2229 Ensure that BeforeGroups and AfterGroups config Methods always get invoked irrespective of whether Group level filtering being done by users.
1 parent d9c6e45 commit 49dbdc9

File tree

6 files changed

+181
-12
lines changed

6 files changed

+181
-12
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Current
22
Fixed: GITHUB-2653: Assert methods requires casting since TestNg 7.0 for mixed boxed and unboxed primitives in assertEquals.
3+
Fixed: GITHUB-2229: Restore @BeforeGroups and @AfterGroups Annotations functionality (Krishnan Mahadevan)
34
Fixed: GITHUB-2563: Skip test if its data provider provides no data (Krishnan Mahadevan)
45
Fixed: GITHUB-2535: TestResult.getEndMillis() returns 0 for skipped configuration - after upgrading testng to 7.0 + (Krishnan Mahadevan)
56
Fixed: GITHUB-2638: "[WARN] Ignoring duplicate listener" appears when running .xml suite with <listeners> and <suite-files> (Krishnan Mahadevan)

testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java

-8
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ public boolean hasConfigurationFailureFor(
110110
* @param arguments - A {@link GroupConfigMethodArguments} object.
111111
*/
112112
public void invokeBeforeGroupsConfigurations(GroupConfigMethodArguments arguments) {
113-
if (arguments.isGroupFilteringDisabled()) {
114-
return;
115-
}
116-
117113
List<ITestNGMethod> filteredMethods = Lists.newArrayList();
118114
String[] groups = arguments.getTestMethod().getGroups();
119115

@@ -168,10 +164,6 @@ public void invokeAfterGroupsConfigurations(GroupConfigMethodArguments arguments
168164
return;
169165
}
170166

171-
if (arguments.isGroupFilteringDisabled()) {
172-
return;
173-
}
174-
175167
// See if the currentMethod is the last method in any of the groups
176168
// it belongs to
177169
Map<String, String> filteredGroups = Maps.newHashMap();

testng-core/src/main/java/org/testng/internal/invokers/GroupConfigMethodArguments.java

-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ public Object getInstance() {
3434
return instance;
3535
}
3636

37-
public boolean isGroupFilteringDisabled() {
38-
return getTestMethod().getXmlTest().isGroupFilteringDisabled();
39-
}
40-
4137
public static class Builder {
4238

4339
private ITestNGMethod testMethod;

testng-core/src/test/java/test/beforegroups/BeforeGroupsTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.IOException;
66
import java.util.ArrayList;
7+
import java.util.Arrays;
78
import java.util.Collections;
89
import java.util.HashMap;
910
import java.util.List;
@@ -23,6 +24,8 @@
2324
import test.SimpleBaseTest;
2425
import test.beforegroups.issue118.TestclassSample;
2526
import test.beforegroups.issue1694.BaseClassWithBeforeGroups;
27+
import test.beforegroups.issue2229.AnotherTestClassSample;
28+
import test.beforegroups.issue2229.TestClassSample;
2629
import test.beforegroups.issue346.SampleTestClass;
2730

2831
public class BeforeGroupsTest extends SimpleBaseTest {
@@ -65,6 +68,38 @@ public void ensureBeforeGroupsAreInvokedWhenCoupledWithAfterGroups() {
6568
assertThat(SampleTestClass.logs).isEqualTo(expected);
6669
}
6770

71+
@Test(description = "GITHUB-2229")
72+
public void ensureBeforeGroupsAreInvokedByDefaultEvenWithoutGrouping() {
73+
TestNG testng = create(TestClassSample.class, AnotherTestClassSample.class);
74+
testng.run();
75+
assertThat(testng.getStatus()).isEqualTo(0);
76+
List<String> expectedLogs =
77+
Arrays.asList(
78+
"TestA",
79+
"TestB",
80+
"TestC",
81+
"beforeGroupA",
82+
"testGroupA1",
83+
"testGroupA2",
84+
"testGroupA3",
85+
"afterGroupA",
86+
"beforeGroupB",
87+
"testGroupB",
88+
"afterGroupB");
89+
assertThat(TestClassSample.logs).containsExactlyElementsOf(expectedLogs);
90+
expectedLogs =
91+
Arrays.asList(
92+
"beforeGroups1",
93+
"test1_testGroup1",
94+
"beforeGroups2",
95+
"test2_testGroup2",
96+
"test3_testGroup1",
97+
"afterGroups1",
98+
"test4_testGroup2",
99+
"afterGroups2");
100+
assertThat(AnotherTestClassSample.logs).containsExactlyElementsOf(expectedLogs);
101+
}
102+
68103
private static void createXmlTest(XmlSuite xmlSuite, String name, String group) {
69104
XmlTest xmlTest = new XmlTest(xmlSuite);
70105
xmlTest.setName(name);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package test.beforegroups.issue2229;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import org.testng.annotations.AfterGroups;
6+
import org.testng.annotations.BeforeGroups;
7+
import org.testng.annotations.Test;
8+
9+
public class AnotherTestClassSample {
10+
11+
public static List<String> logs = new ArrayList<>();
12+
13+
@BeforeGroups(groups = "testGroup1")
14+
public void beforeGroups1() {
15+
logs.add("beforeGroups1");
16+
}
17+
18+
@BeforeGroups(groups = "testGroup2")
19+
public void beforeGroups2() {
20+
logs.add("beforeGroups2");
21+
}
22+
23+
@AfterGroups(groups = "testGroup1")
24+
public void afterGroups1() {
25+
logs.add("afterGroups1");
26+
}
27+
28+
@AfterGroups(groups = "testGroup2")
29+
public void afterGroups2() {
30+
logs.add("afterGroups2");
31+
}
32+
33+
@Test(groups = "testGroup1")
34+
public void test1() {
35+
logs.add("test1_testGroup1");
36+
}
37+
38+
@Test(groups = "testGroup2")
39+
public void test2() {
40+
logs.add("test2_testGroup2");
41+
}
42+
43+
@Test(groups = "testGroup1")
44+
public void test3() {
45+
logs.add("test3_testGroup1");
46+
}
47+
48+
@Test(groups = "testGroup2")
49+
public void test4() {
50+
logs.add("test4_testGroup2");
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package test.beforegroups.issue2229;
2+
3+
import static org.testng.Assert.assertFalse;
4+
import static org.testng.Assert.assertTrue;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import org.testng.annotations.AfterClass;
9+
import org.testng.annotations.AfterGroups;
10+
import org.testng.annotations.BeforeGroups;
11+
import org.testng.annotations.Test;
12+
13+
public class TestClassSample {
14+
15+
public static List<String> logs = new ArrayList<>();
16+
17+
boolean valueA = false;
18+
boolean valueB = false;
19+
20+
@BeforeGroups(groups = "groupA")
21+
public void beforeGroupA() {
22+
logs.add("beforeGroupA");
23+
valueA = true;
24+
}
25+
26+
@BeforeGroups(groups = "groupB")
27+
public void beforeGroupB() {
28+
valueB = true;
29+
logs.add("beforeGroupB");
30+
}
31+
32+
@BeforeGroups(groups = "groupC")
33+
public void beforeGroupC() {
34+
logs.add("beforeGroupC No Test exist, should not run.");
35+
}
36+
37+
@Test
38+
public void testA() {
39+
logs.add("TestA");
40+
}
41+
42+
@Test
43+
public void testB() {
44+
logs.add("TestB");
45+
}
46+
47+
@Test
48+
public void testC() {
49+
logs.add("TestC");
50+
}
51+
52+
@Test(groups = "groupA")
53+
public void testGroupA1() {
54+
logs.add("testGroupA1");
55+
assertTrue(valueA, "BeforeGroupA was not executed");
56+
}
57+
58+
@Test(groups = "groupA")
59+
public void testGroupA2() {
60+
logs.add("testGroupA2");
61+
assertTrue(valueA, "BeforeGroupA was not executed");
62+
}
63+
64+
@Test(groups = "groupA")
65+
public void testGroupA3() {
66+
logs.add("testGroupA3");
67+
assertTrue(valueA, "BeforeGroupA was not executed");
68+
}
69+
70+
@Test(groups = "groupB")
71+
public void testGroupB() {
72+
logs.add("testGroupB");
73+
assertTrue(valueB, "BeforeGroupB was not executed");
74+
}
75+
76+
@AfterGroups(groups = "groupA")
77+
public void afterGroupA() {
78+
logs.add("afterGroupA");
79+
valueA = false;
80+
}
81+
82+
@AfterGroups(groups = "groupB")
83+
public void afterGroupB() {
84+
logs.add("afterGroupB");
85+
valueB = false;
86+
}
87+
88+
@AfterClass
89+
public void afterClass() {
90+
assertFalse(valueA, "AfterGroupsA was not executed");
91+
assertFalse(valueB, "AfterGroupsB was not executed");
92+
}
93+
}

0 commit comments

Comments
 (0)