Skip to content

Commit f1111e0

Browse files
authored
Merge pull request #1990 from krmahadevan/fix_165
Streamline @AfterGroups Invocation
2 parents 910cf7b + 24af2bc commit f1111e0

File tree

7 files changed

+104
-30
lines changed

7 files changed

+104
-30
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Current
2+
Fixed: GITHUB-165: @AfterGroups is not executed when group member fails or is skipped (Krishnan Mahadevan)
23
Fixed: GITHUB-118: @BeforeGroups only called if group is specified explicitly (Krishnan Mahadevan)
34
Fixed: GITHUB-182: Inherited test methods do not get expected group behavior (Krishnan Mahadevan)
45
Fixed: GITHUB-1988: Add Automatic-Module-Name to MANIFEST.MF (Krishnan Mahadevan)

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.testng.collections.Lists;
3232
import org.testng.collections.Maps;
3333
import org.testng.collections.Sets;
34+
import org.testng.internal.GroupConfigMethodArguments.Builder;
3435
import org.testng.internal.InvokeMethodRunnable.TestNGRuntimeException;
3536
import org.testng.internal.ParameterHandler.ParameterBag;
3637
import org.testng.internal.thread.ThreadExecutionException;
@@ -81,6 +82,7 @@ public List<ITestResult> invokeTestMethods(ITestNGMethod testMethod,
8182
return Collections.emptyList();
8283
}
8384

85+
Map<String, String> parameters = testMethod.findMethodParameters(context.getCurrentXmlTest());
8486
// By the time this testMethod to be invoked,
8587
// all dependencies should be already run or we need to skip this method,
8688
// so invocation count should not affect dependencies check
@@ -97,12 +99,18 @@ public List<ITestResult> invokeTestMethods(ITestNGMethod testMethod,
9799
InvokedMethod invokedMethod = new InvokedMethod(result.getInstance(), testMethod,
98100
System.currentTimeMillis(), result);
99101
invokeListenersForSkippedTestResult(result, invokedMethod);
100-
102+
testMethod.incrementCurrentInvocationCount();
103+
GroupConfigMethodArguments args = new Builder()
104+
.forTestMethod(testMethod)
105+
.withGroupConfigMethods(groupMethods)
106+
.forSuite(suite)
107+
.forInstance(instance)
108+
.withParameters(parameters)
109+
.build();
110+
this.invoker.invokeAfterGroupsConfigurations(args);
101111
return Collections.singletonList(result);
102112
}
103113

104-
Map<String, String> parameters =
105-
testMethod.findMethodParameters(context.getCurrentXmlTest());
106114

107115
// For invocationCount > 1 and threadPoolSize > 1 run this method in its own pool thread.
108116
if (testMethod.getInvocationCount() > 1 && testMethod.getThreadPoolSize() > 1) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package test.aftergroups;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.testng.TestNG;
6+
import org.testng.annotations.DataProvider;
7+
import org.testng.annotations.Test;
8+
import org.testng.xml.XmlSuite;
9+
import org.testng.xml.XmlSuite.FailurePolicy;
10+
import test.SimpleBaseTest;
11+
import test.aftergroups.issue165.TestclassSampleWithFailedMember;
12+
import test.aftergroups.issue165.TestclassSampleWithSkippedMember;
13+
import test.aftergroups.issue1880.LocalConfigListener;
14+
import test.aftergroups.issue1880.TestClassSample;
15+
16+
public class AfterGroupsBehaviorTest extends SimpleBaseTest {
17+
18+
@Test(description = "GITHUB-1880")
19+
public void ensureAfterGroupsAreInvokedWithAlwaysRunAttribute() {
20+
runTest(TestClassSample.class, "123", true, "after");
21+
}
22+
23+
@Test(dataProvider = "dp", description = "GITHUB-165")
24+
public void ensureAfterGroupsInvoked(Class<?> clazz, String expected) {
25+
runTest(clazz, "A", false, expected);
26+
}
27+
28+
@DataProvider(name = "dp")
29+
public Object[][] getData() {
30+
return new Object[][]{
31+
{TestclassSampleWithSkippedMember.class, "afterGroupsMethod"},
32+
{TestclassSampleWithFailedMember.class, "afterGroupsMethod"},
33+
};
34+
}
35+
36+
private static void runTest(Class<?> clazz, String groups, boolean shouldContinue, String expected) {
37+
XmlSuite xmlsuite = createXmlSuite("sample_suite", "sample_test", clazz);
38+
xmlsuite.addIncludedGroup(groups);
39+
TestNG testng = create(xmlsuite);
40+
if (shouldContinue) {
41+
testng.setConfigFailurePolicy(FailurePolicy.CONTINUE);
42+
}
43+
LocalConfigListener listener = new LocalConfigListener();
44+
testng.addListener(listener);
45+
testng.run();
46+
assertThat(listener.getMessages()).containsExactly(expected);
47+
48+
}
49+
50+
51+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test.aftergroups.issue165;
2+
3+
import org.testng.annotations.AfterGroups;
4+
import org.testng.annotations.Test;
5+
6+
public class TestclassSampleWithFailedMember {
7+
8+
@Test(groups = "A")
9+
public void a1() {
10+
}
11+
12+
@Test(groups = "A", dependsOnMethods = "a1")
13+
public void a2() {
14+
throw new org.testng.SkipException("skip");
15+
}
16+
17+
@AfterGroups(groups = "A")
18+
public void afterGroupsMethod() {
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test.aftergroups.issue165;
2+
3+
import org.testng.annotations.AfterGroups;
4+
import org.testng.annotations.Test;
5+
6+
public class TestclassSampleWithSkippedMember {
7+
8+
@Test(groups = "A")
9+
public void a1() {
10+
throw new org.testng.SkipException("skip");
11+
}
12+
13+
@Test(groups = "A", dependsOnMethods = "a1")
14+
public void a2() {
15+
}
16+
17+
@AfterGroups(groups = "A")
18+
public void afterGroupsMethod() {
19+
}
20+
}

src/test/java/test/aftergroups/issue1880/IssueTest.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/test/resources/testng.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
<class name="test.testng195.AfterMethodTest" />
122122
<class name="test.regression.BeforeTestFailingTest"/>
123123
<class name="test.configuration.github1700.RunTest"/>
124+
<class name="test.aftergroups.AfterGroupsBehaviorTest"/>
124125
<class name="test.testng285.TestNG285Test" />
125126
<class name="test.failedreporter.FailedReporterTest" />
126127
<class name="test.attributes.AttributeTest"/>
@@ -189,7 +190,6 @@
189190
<class name="test.github1490.VerifyDataProviderListener"/>
190191
<class name="test.methodselection.MethodSelectionTest"/>
191192
<class name="test.beforegroups.BeforeGroupsTest"/>
192-
<class name="test.aftergroups.issue1880.IssueTest"/>
193193
<class name="test.invocationcount.issue1719.IssueTest"/>
194194
</classes>
195195
</test>

0 commit comments

Comments
 (0)