Skip to content

Commit d472bc9

Browse files
committed
After methods should be skipped when exclusion used
Closes #1574 Suppose there are one or more configuration methods which don’t belong to any group and for which “alwaysRun” attribute is not set, and when the user specifies a valid exclusion list for groups, TestNG would still execute those configuration methods. Fixed this anomaly.
1 parent f4a6eb4 commit d472bc9

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Current
2+
Fixed: GITHUB-1574: Before/After methods are not running when groups "include" in suite (Krishnan Mahadevan)
23
Fixed: GITHUB-987: Parameters threadCount and parallel doesn't work with maven (Krishnan Mahadevan)
34
Fixed: GITHUB-1472: Optimize DynamicGraph.getUnfinishedNodes (Krishnan Mahadevan & Nathan Reynolds)
45
Fixed: GITHUB-1566: Invalid XML characters in Params in testng-results.xml (Krishnan Mahadevan)

src/main/java/org/testng/internal/XmlMethodSelector.java

+4
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ private static boolean isIncluded(Collection<String> includedGroups, boolean noG
275275
}
276276

277277
private static boolean isExcluded(Collection<String> excludedGroups, String... groups) {
278+
boolean noGroups = (groups == null || groups.length == 0);
279+
if (!excludedGroups.isEmpty() && noGroups) {
280+
return true;
281+
}
278282
return isMemberOf(excludedGroups, groups);
279283
}
280284

src/test/java/test/groupinvocation/GroupSuiteTest.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.File;
1515
import java.util.ArrayList;
1616
import java.util.Arrays;
17+
import java.util.Collections;
1718
import java.util.List;
1819

1920
import static org.assertj.core.api.Assertions.assertThat;
@@ -123,7 +124,7 @@ private void runWithSuite(String[] withSuiteFiles, boolean groupsWithXml, List<S
123124
test.setExcludedGroups(excludedTestGroups);
124125
}
125126

126-
tng.setXmlSuites(Arrays.asList(suite));
127+
tng.setXmlSuites(Collections.singletonList(suite));
127128

128129
InvokedMethodNameListener listener = new InvokedMethodNameListener();
129130
tng.addListener((ITestNGListener) listener);
@@ -133,6 +134,16 @@ private void runWithSuite(String[] withSuiteFiles, boolean groupsWithXml, List<S
133134
assertThat(listener.getInvokedMethodNames()).containsExactly(methods);
134135
}
135136

137+
@Test(description = "GITHUB-1574")
138+
public void testToCheckNoConfigurationsAreExecuted() {
139+
XmlSuite suite = createXmlSuite("suite");
140+
XmlTest test = createXmlTest(suite, "test", Issue1574TestclassSample.class);
141+
test.addExcludedGroup("sometest");
142+
TestNG testng = create(suite);
143+
testng.run();
144+
assertThat(Issue1574TestclassSample.messages).containsExactly("anothertest","@AfterSuite");
145+
}
146+
136147
private static List<String> g(String... groups) {
137148
return Arrays.asList(groups);
138149
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package test.groupinvocation;
2+
3+
import org.testng.annotations.AfterMethod;
4+
import org.testng.annotations.AfterSuite;
5+
import org.testng.annotations.AfterTest;
6+
import org.testng.annotations.BeforeMethod;
7+
import org.testng.annotations.BeforeSuite;
8+
import org.testng.annotations.BeforeTest;
9+
import org.testng.annotations.Test;
10+
import org.testng.collections.Lists;
11+
12+
import java.util.List;
13+
14+
public class Issue1574TestclassSample {
15+
public static List<String> messages = Lists.newArrayList();
16+
@BeforeSuite
17+
public void setupSuite() {
18+
messages.add("@BeforeSuite");
19+
}
20+
21+
@BeforeTest
22+
public void setUpTest() {
23+
messages.add("@BeforeTest");
24+
}
25+
26+
@BeforeMethod
27+
public void setUpMethod() {
28+
messages.add("@BeforeMethod");
29+
}
30+
31+
@AfterMethod
32+
public void tearDownMethod() {
33+
messages.add("@AfterMethod");
34+
}
35+
36+
@AfterTest
37+
public void tearDownTest() {
38+
messages.add("@AfterTest");
39+
}
40+
@AfterSuite(alwaysRun = true)
41+
public void tearDownSuite() {
42+
messages.add("@AfterSuite");
43+
}
44+
45+
@Test(groups = {"sometest"})
46+
public void someTest() {
47+
messages.add("sometest");
48+
}
49+
50+
@Test(groups = {"anothertest"})
51+
public void anothertest() {
52+
messages.add("anothertest");
53+
}
54+
}

0 commit comments

Comments
 (0)