Skip to content

Commit 0048a83

Browse files
committed
After methods should be skipped when exclusion used
Closes testng-team#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 317a92d commit 0048a83

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-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-217: exception in DataProvider doesn't fail test run (Krishnan Mahadevan)
34
Fixed: GITHUB-987: Parameters threadCount and parallel doesn't work with maven (Krishnan Mahadevan)
45
Fixed: GITHUB-1472: Optimize DynamicGraph.getUnfinishedNodes (Krishnan Mahadevan & Nathan Reynolds)

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

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

277277
private static boolean isExcluded(Collection<String> excludedGroups, String... groups) {
278+
if (!excludedGroups.isEmpty() && groups.length == 0) {
279+
return true;
280+
}
278281
return isMemberOf(excludedGroups, groups);
279282
}
280283

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

+14-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,18 @@ 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+
InvokedMethodNameListener listener = new InvokedMethodNameListener();
144+
testng.addListener((ITestNGListener) listener);
145+
testng.run();
146+
assertThat(listener.getInvokedMethodNames()).containsExactly("anothertest", "tearDownSuite");
147+
}
148+
136149
private static List<String> g(String... groups) {
137150
return Arrays.asList(groups);
138151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
11+
public class Issue1574TestclassSample {
12+
@BeforeSuite
13+
public void setupSuite() {
14+
}
15+
16+
@BeforeTest
17+
public void setUpTest() {
18+
}
19+
20+
@BeforeMethod
21+
public void setUpMethod() {
22+
}
23+
24+
@AfterMethod
25+
public void tearDownMethod() {
26+
}
27+
28+
@AfterTest
29+
public void tearDownTest() {
30+
}
31+
32+
@AfterSuite(alwaysRun = true)
33+
public void tearDownSuite() {
34+
}
35+
36+
@Test(groups = {"sometest"})
37+
public void someTest() {
38+
}
39+
40+
@Test(groups = {"anothertest"})
41+
public void anothertest() {
42+
}
43+
}

0 commit comments

Comments
 (0)