Skip to content

Commit

Permalink
Restore BeforeGroups|AfterGroups functionality back
Browse files Browse the repository at this point in the history
Closes #2229

Ensure that BeforeGroups and AfterGroups config
Methods always get invoked irrespective of whether
Group level filtering being done by users.
  • Loading branch information
krmahadevan committed Oct 25, 2021
1 parent d9c6e45 commit 49dbdc9
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Current
Fixed: GITHUB-2653: Assert methods requires casting since TestNg 7.0 for mixed boxed and unboxed primitives in assertEquals.
Fixed: GITHUB-2229: Restore @BeforeGroups and @AfterGroups Annotations functionality (Krishnan Mahadevan)
Fixed: GITHUB-2563: Skip test if its data provider provides no data (Krishnan Mahadevan)
Fixed: GITHUB-2535: TestResult.getEndMillis() returns 0 for skipped configuration - after upgrading testng to 7.0 + (Krishnan Mahadevan)
Fixed: GITHUB-2638: "[WARN] Ignoring duplicate listener" appears when running .xml suite with <listeners> and <suite-files> (Krishnan Mahadevan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ public boolean hasConfigurationFailureFor(
* @param arguments - A {@link GroupConfigMethodArguments} object.
*/
public void invokeBeforeGroupsConfigurations(GroupConfigMethodArguments arguments) {
if (arguments.isGroupFilteringDisabled()) {
return;
}

List<ITestNGMethod> filteredMethods = Lists.newArrayList();
String[] groups = arguments.getTestMethod().getGroups();

Expand Down Expand Up @@ -168,10 +164,6 @@ public void invokeAfterGroupsConfigurations(GroupConfigMethodArguments arguments
return;
}

if (arguments.isGroupFilteringDisabled()) {
return;
}

// See if the currentMethod is the last method in any of the groups
// it belongs to
Map<String, String> filteredGroups = Maps.newHashMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ public Object getInstance() {
return instance;
}

public boolean isGroupFilteringDisabled() {
return getTestMethod().getXmlTest().isGroupFilteringDisabled();
}

public static class Builder {

private ITestNGMethod testMethod;
Expand Down
35 changes: 35 additions & 0 deletions testng-core/src/test/java/test/beforegroups/BeforeGroupsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -23,6 +24,8 @@
import test.SimpleBaseTest;
import test.beforegroups.issue118.TestclassSample;
import test.beforegroups.issue1694.BaseClassWithBeforeGroups;
import test.beforegroups.issue2229.AnotherTestClassSample;
import test.beforegroups.issue2229.TestClassSample;
import test.beforegroups.issue346.SampleTestClass;

public class BeforeGroupsTest extends SimpleBaseTest {
Expand Down Expand Up @@ -65,6 +68,38 @@ public void ensureBeforeGroupsAreInvokedWhenCoupledWithAfterGroups() {
assertThat(SampleTestClass.logs).isEqualTo(expected);
}

@Test(description = "GITHUB-2229")
public void ensureBeforeGroupsAreInvokedByDefaultEvenWithoutGrouping() {
TestNG testng = create(TestClassSample.class, AnotherTestClassSample.class);
testng.run();
assertThat(testng.getStatus()).isEqualTo(0);
List<String> expectedLogs =
Arrays.asList(
"TestA",
"TestB",
"TestC",
"beforeGroupA",
"testGroupA1",
"testGroupA2",
"testGroupA3",
"afterGroupA",
"beforeGroupB",
"testGroupB",
"afterGroupB");
assertThat(TestClassSample.logs).containsExactlyElementsOf(expectedLogs);
expectedLogs =
Arrays.asList(
"beforeGroups1",
"test1_testGroup1",
"beforeGroups2",
"test2_testGroup2",
"test3_testGroup1",
"afterGroups1",
"test4_testGroup2",
"afterGroups2");
assertThat(AnotherTestClassSample.logs).containsExactlyElementsOf(expectedLogs);
}

private static void createXmlTest(XmlSuite xmlSuite, String name, String group) {
XmlTest xmlTest = new XmlTest(xmlSuite);
xmlTest.setName(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package test.beforegroups.issue2229;

import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class AnotherTestClassSample {

public static List<String> logs = new ArrayList<>();

@BeforeGroups(groups = "testGroup1")
public void beforeGroups1() {
logs.add("beforeGroups1");
}

@BeforeGroups(groups = "testGroup2")
public void beforeGroups2() {
logs.add("beforeGroups2");
}

@AfterGroups(groups = "testGroup1")
public void afterGroups1() {
logs.add("afterGroups1");
}

@AfterGroups(groups = "testGroup2")
public void afterGroups2() {
logs.add("afterGroups2");
}

@Test(groups = "testGroup1")
public void test1() {
logs.add("test1_testGroup1");
}

@Test(groups = "testGroup2")
public void test2() {
logs.add("test2_testGroup2");
}

@Test(groups = "testGroup1")
public void test3() {
logs.add("test3_testGroup1");
}

@Test(groups = "testGroup2")
public void test4() {
logs.add("test4_testGroup2");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package test.beforegroups.issue2229;

import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class TestClassSample {

public static List<String> logs = new ArrayList<>();

boolean valueA = false;
boolean valueB = false;

@BeforeGroups(groups = "groupA")
public void beforeGroupA() {
logs.add("beforeGroupA");
valueA = true;
}

@BeforeGroups(groups = "groupB")
public void beforeGroupB() {
valueB = true;
logs.add("beforeGroupB");
}

@BeforeGroups(groups = "groupC")
public void beforeGroupC() {
logs.add("beforeGroupC No Test exist, should not run.");
}

@Test
public void testA() {
logs.add("TestA");
}

@Test
public void testB() {
logs.add("TestB");
}

@Test
public void testC() {
logs.add("TestC");
}

@Test(groups = "groupA")
public void testGroupA1() {
logs.add("testGroupA1");
assertTrue(valueA, "BeforeGroupA was not executed");
}

@Test(groups = "groupA")
public void testGroupA2() {
logs.add("testGroupA2");
assertTrue(valueA, "BeforeGroupA was not executed");
}

@Test(groups = "groupA")
public void testGroupA3() {
logs.add("testGroupA3");
assertTrue(valueA, "BeforeGroupA was not executed");
}

@Test(groups = "groupB")
public void testGroupB() {
logs.add("testGroupB");
assertTrue(valueB, "BeforeGroupB was not executed");
}

@AfterGroups(groups = "groupA")
public void afterGroupA() {
logs.add("afterGroupA");
valueA = false;
}

@AfterGroups(groups = "groupB")
public void afterGroupB() {
logs.add("afterGroupB");
valueB = false;
}

@AfterClass
public void afterClass() {
assertFalse(valueA, "AfterGroupsA was not executed");
assertFalse(valueB, "AfterGroupsB was not executed");
}
}

0 comments on commit 49dbdc9

Please sign in to comment.