Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GITHUB-2135] @BeforeClass and @AfterClass in superclass ignore group… #2661

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current
Fixed: GITHUB-2135 @BeforeClass and @AfterClass in superclass ignore groups when running parallel in classes.
Fixed: GITHUB-2653: Assert methods requires casting since TestNg 7.0 for mixed boxed and unboxed primitives in assertEquals.
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public final class RuntimeBehavior {
private static final String MEMORY_FRIENDLY_MODE = "testng.memory.friendly";
public static final String STRICTLY_HONOUR_PARALLEL_MODE = "testng.strict.parallel";
public static final String TESTNG_DEFAULT_VERBOSE = "testng.default.verbose";
public static final String TESTNG_DISABLE_NEW_GROUP_BEHAVIOR = "testng.disable.new.group.beavhor";

private RuntimeBehavior() {}

Expand Down Expand Up @@ -127,4 +128,14 @@ public static boolean enforceThreadAffinity() {
public static int getDefaultVerboseLevel() {
return Integer.getInteger(TESTNG_DEFAULT_VERBOSE, 1);
}

/**
* Property to disable new behavior for groups. In Pull request #2167 Before/AfterGroups will no
* longer run if they are not added i the group filter in the suite.
*
* @return true if new group behavior is disabled.
*/
public static boolean isNewGroupBehaviorDisabled() {
return Boolean.parseBoolean(System.getProperty(TESTNG_DISABLE_NEW_GROUP_BEHAVIOR, "false"));
}
}
4 changes: 4 additions & 0 deletions testng-core-api/src/main/java/org/testng/xml/XmlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.testng.TestNGException;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.internal.RuntimeBehavior;

/** This class describes the tag <test> in testng.xml. */
public class XmlTest implements Cloneable {
Expand Down Expand Up @@ -115,6 +116,9 @@ public List<String> getIncludedGroups() {
}

public boolean isGroupFilteringDisabled() {
if (RuntimeBehavior.isNewGroupBehaviorDisabled()) {
return false;
}
return getIncludedGroups().isEmpty() && getExcludedGroups().isEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package test.beforegroups;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

/** This Class is used for live demo sessions. Do not add any test code here. */
public class OrginalBehaviorTest {

boolean valueA = false;
boolean valueB = false;

@BeforeClass
public void setup() {
System.setProperty("testng.disable.new.group.beavhor", "true");
}

@BeforeGroups(groups = "groupA")
public void beforeGroupA() {
System.out.println("beforeGroupA");
valueA = true;
}

@BeforeGroups(groups = "groupB")
public void beforeGroupB() {
valueB = true;
System.out.println("beforeGroupB");
}

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

@Test
public void testA() {
System.out.println("TestA");
}

@Test
public void testB() {
System.out.println("TestB");
}

@Test
public void testC() {
System.out.println("TestC");
}

@Test(groups = "groupA")
public void testGroupA1() {
System.out.println("testGroupA1");
Assert.assertTrue(valueA, "BeforeGroupA was not executed");
}

@Test(groups = "groupA")
public void testGroupA2() {
System.out.println("testGroupA2");
Assert.assertTrue(valueA, "BeforeGroupA was not executed");
}

@Test(groups = "groupA")
public void testGroupA3() {
System.out.println("testGroupA3");
Assert.assertTrue(valueA, "BeforeGroupA was not executed");
}

@Test(groups = "groupB")
public void testGroupB() {
System.out.println("testGroupB");
Assert.assertTrue(valueB, "BeforeGroupB was not executed");
}

@AfterGroups(groups = "groupA")
public void afterGroupA() {
System.out.println("afterGroupA");
valueA = false;
}

@AfterGroups(groups = "groupB")
public void afterGroupB() {
System.out.println("afterGroupB");
valueB = false;
}

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