Skip to content

Commit 8b5f4d1

Browse files
authored
Skip config listener calls for skipped configs
Closes #2880 Ensure that before configuration and before invocation should be 'SKIP' when beforeMethod is 'skip'
1 parent 53edc70 commit 8b5f4d1

File tree

7 files changed

+178
-3
lines changed

7 files changed

+178
-3
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Fixed: GITHUB-2857: XmlTest index is not set for test suites invoked with YAML
77
Fixed: GITHUB-2862: Allow test classes to define "configfailurepolicy" at a per class level (Krishnan Mahadevan)
88
Fixed: GITHUB-2796: Option for onAfterClass to run after @AfterClass (Oliver Hughes)
99
Fixed: GITHUB-2857: XmlTest index is not set for test suites invoked with YAML (Sergei Baranov)
10+
Fixed: GITHUB-2880: Before configuration and before invocation set 'SKIP' when beforeMethod is 'skip' (Bob Shi)
1011

1112
7.7.1
1213
Fixed: GITHUB-2854: overloaded assertEquals methods do not work from Groovy (Krishnan Mahadevan)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,12 @@ public void invokeConfigurations(ConfigMethodArguments arguments) {
279279
&& !alwaysRun) {
280280
log(3, "Skipping " + Utils.detailedMethodName(tm, true));
281281
InvokedMethod invokedMethod = new InvokedMethod(System.currentTimeMillis(), testResult);
282+
// Set test result as 'SKIP' in 'beforeConfiguration' & 'beforeInvocation' if
283+
// config method is skip.
284+
testResult.setStatus(ITestResult.SKIP);
282285
runConfigurationListeners(testResult, arguments.getTestMethod(), true /* before */);
283286
runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);
284287
testResult.setEndMillis(testResult.getStartMillis());
285-
testResult.setStatus(ITestResult.SKIP);
286288
runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);
287289

288290
handleConfigurationSkip(

testng-core/src/test/java/test/listeners/ListenersTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import test.listeners.issue2752.ListenerSample;
2626
import test.listeners.issue2752.TestClassSample;
2727
import test.listeners.issue2771.TestCaseSample;
28+
import test.listeners.issue2880.ListenerForIssue2880;
29+
import test.listeners.issue2880.TestClassWithFailingConfigsSample;
30+
import test.listeners.issue2880.TestClassWithPassingConfigsSample;
2831

2932
public class ListenersTest extends SimpleBaseTest {
3033

@@ -101,6 +104,17 @@ public void testEnsureNativeListenersAreRunAlwaysAtEnd() {
101104
assertThat(testng.getStatus()).isEqualTo(ExitCode.FAILED);
102105
}
103106

107+
@Test(description = "GITHUB-2880", dataProvider = "issue2880-dataprovider")
108+
public void testSkipStatusInBeforeAndAfterConfigurationMethod(
109+
Class<?> clazz, XmlSuite.FailurePolicy policy, List<String> expected) {
110+
TestNG tng = create(clazz);
111+
ListenerForIssue2880 listener = new ListenerForIssue2880();
112+
tng.setConfigFailurePolicy(policy);
113+
tng.addListener(listener);
114+
tng.run();
115+
assertThat(listener.getLogs()).containsExactlyElementsOf(expected);
116+
}
117+
104118
private void setupTest(boolean addExplicitListener) {
105119
TestNG testng = new TestNG();
106120
XmlSuite xmlSuite = createXmlSuite("Xml_Suite");
@@ -200,4 +214,53 @@ private static XmlSuite getNestedSuitesViaApis() {
200214
innerSuite2.setParentSuite(containerSuite);
201215
return containerSuite;
202216
}
217+
218+
@DataProvider(name = "issue2880-dataprovider")
219+
public Object[][] getIssue2880Data() {
220+
List<String> passList =
221+
Arrays.asList(
222+
"BeforeInvocation_beforeClass_STARTED",
223+
"AfterInvocation_beforeClass_SUCCESS",
224+
"BeforeInvocation_beforeMethod_STARTED",
225+
"AfterInvocation_beforeMethod_SUCCESS",
226+
"BeforeInvocation_testMethod_STARTED",
227+
"AfterInvocation_testMethod_SUCCESS",
228+
"BeforeInvocation_afterMethod_STARTED",
229+
"AfterInvocation_afterMethod_SUCCESS",
230+
"BeforeInvocation_afterClass_STARTED",
231+
"AfterInvocation_afterClass_SUCCESS");
232+
233+
List<String> skipList =
234+
Arrays.asList(
235+
"BeforeInvocation_beforeClass_STARTED",
236+
"AfterInvocation_beforeClass_FAILURE",
237+
"BeforeInvocation_beforeMethod_SKIP",
238+
"AfterInvocation_beforeMethod_SKIP",
239+
"BeforeInvocation_testMethod_SKIP",
240+
"AfterInvocation_testMethod_SKIP",
241+
"BeforeInvocation_afterMethod_SKIP",
242+
"AfterInvocation_afterMethod_SKIP",
243+
"BeforeInvocation_afterClass_SKIP",
244+
"AfterInvocation_afterClass_SKIP");
245+
246+
List<String> failList =
247+
Arrays.asList(
248+
"BeforeInvocation_beforeClass_STARTED",
249+
"AfterInvocation_beforeClass_FAILURE",
250+
"BeforeInvocation_beforeMethod_SKIP",
251+
"AfterInvocation_beforeMethod_SKIP",
252+
"BeforeInvocation_testMethod_SKIP",
253+
"AfterInvocation_testMethod_SKIP",
254+
"BeforeInvocation_afterMethod_SKIP",
255+
"AfterInvocation_afterMethod_SKIP",
256+
"BeforeInvocation_afterClass_SKIP",
257+
"AfterInvocation_afterClass_SKIP");
258+
259+
return new Object[][] {
260+
{TestClassWithPassingConfigsSample.class, XmlSuite.FailurePolicy.SKIP, passList},
261+
{TestClassWithFailingConfigsSample.class, XmlSuite.FailurePolicy.SKIP, skipList},
262+
{TestClassWithPassingConfigsSample.class, XmlSuite.FailurePolicy.CONTINUE, passList},
263+
{TestClassWithFailingConfigsSample.class, XmlSuite.FailurePolicy.CONTINUE, failList}
264+
};
265+
}
203266
}

testng-core/src/test/java/test/listeners/github1602/IssueTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,22 @@ public Object[][] getData() {
3434
"AfterInvocation_testMethod_SUCCESS",
3535
"BeforeInvocation_afterMethod_STARTED",
3636
"AfterInvocation_afterMethod_SUCCESS");
37+
3738
List<String> baseList =
3839
Arrays.asList(
3940
"BeforeInvocation_beforeMethod_STARTED",
4041
"AfterInvocation_beforeMethod_FAILURE",
4142
"BeforeInvocation_testMethod_SKIP",
42-
"AfterInvocation_testMethod_SKIP",
43-
"BeforeInvocation_afterMethod_STARTED");
43+
"AfterInvocation_testMethod_SKIP");
44+
4445
List<String> skipList = Lists.newArrayList(baseList);
46+
skipList.add("BeforeInvocation_afterMethod_SKIP");
4547
skipList.add("AfterInvocation_afterMethod_SKIP");
48+
4649
List<String> failList = Lists.newArrayList(baseList);
50+
failList.add("BeforeInvocation_afterMethod_STARTED");
4751
failList.add("AfterInvocation_afterMethod_FAILURE");
52+
4853
return new Object[][] {
4954
{TestClassWithPassingConfigsSample.class, XmlSuite.FailurePolicy.SKIP, passList},
5055
{TestClassWithFailingConfigsSample.class, XmlSuite.FailurePolicy.SKIP, skipList},
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package test.listeners.issue2880;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
import org.testng.IInvokedMethod;
6+
import org.testng.IInvokedMethodListener;
7+
import org.testng.ITestResult;
8+
import org.testng.collections.Lists;
9+
10+
public class ListenerForIssue2880 implements IInvokedMethodListener {
11+
private List<String> logs = Collections.synchronizedList(Lists.newArrayList());
12+
13+
@Override
14+
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
15+
logs.add(
16+
"BeforeInvocation_"
17+
+ method.getTestMethod().getMethodName()
18+
+ "_"
19+
+ intToStatus(testResult));
20+
}
21+
22+
@Override
23+
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
24+
logs.add(
25+
"AfterInvocation_"
26+
+ method.getTestMethod().getMethodName()
27+
+ "_"
28+
+ intToStatus(testResult));
29+
}
30+
31+
public List<String> getLogs() {
32+
return logs;
33+
}
34+
35+
private String intToStatus(ITestResult testResult) {
36+
int status = testResult.getStatus();
37+
switch (status) {
38+
case ITestResult.CREATED:
39+
return "CREATED";
40+
case ITestResult.SUCCESS:
41+
return "SUCCESS";
42+
case ITestResult.SKIP:
43+
return "SKIP";
44+
case ITestResult.FAILURE:
45+
return "FAILURE";
46+
case ITestResult.STARTED:
47+
return "STARTED";
48+
case ITestResult.SUCCESS_PERCENTAGE_FAILURE:
49+
return "SUCCESS_PERCENTAGE_FAILURE";
50+
}
51+
return " ??? " + String.valueOf(status);
52+
}
53+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package test.listeners.issue2880;
2+
3+
import org.testng.annotations.AfterClass;
4+
import org.testng.annotations.AfterMethod;
5+
import org.testng.annotations.BeforeClass;
6+
import org.testng.annotations.BeforeMethod;
7+
import org.testng.annotations.Test;
8+
9+
public class TestClassWithFailingConfigsSample {
10+
@BeforeClass
11+
public void beforeClass() {
12+
throw new RuntimeException();
13+
}
14+
15+
@BeforeMethod
16+
public void beforeMethod() {}
17+
18+
@Test
19+
public void testMethod() {}
20+
21+
@AfterMethod
22+
public void afterMethod() {}
23+
24+
@AfterClass
25+
public void afterClass() {}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test.listeners.issue2880;
2+
3+
import org.testng.annotations.AfterClass;
4+
import org.testng.annotations.AfterMethod;
5+
import org.testng.annotations.BeforeClass;
6+
import org.testng.annotations.BeforeMethod;
7+
import org.testng.annotations.Test;
8+
9+
public class TestClassWithPassingConfigsSample {
10+
11+
@BeforeClass
12+
public void beforeClass() {}
13+
14+
@BeforeMethod
15+
public void beforeMethod() {}
16+
17+
@Test
18+
public void testMethod() {}
19+
20+
@AfterMethod
21+
public void afterMethod() {}
22+
23+
@AfterClass
24+
public void afterClass() {}
25+
}

0 commit comments

Comments
 (0)