testng: Version 7.1.0, @BeforeGroups and @AfterGroups Annotations are not working

I believe that this issue is a regression as they are working normally on version 7.0.0

TestNG Version

7.1.0

Expected behavior

The @BeforeGroups and @AfterGroups should be working properly when triggering the execution from a TestNG class, xml file or surefire plugin.

Actual behavior

The @BeforeGroups and @AfterGroups are not working when executing the the run from a TestNG class or from the surefire plugin, only works when the test is triggered from a testng.xml file when I include the group name in the run parameter.

Is the issue reproductible on runner?

I’m running the test class directly via the IDE [Eclipse] and via the cmd using surefire plugin .

  • When running the test class directly via the IDE >> the issue happens and the Groups annotations doesn’t work.
  • When running from the xml file while including the groups names in the run parameter >> the Groups annotations works fine.

Same happens with maven surefire plugin.

  • Shell
  • Maven
  • Gradle
  • Ant
  • Eclipse
  • IntelliJ
  • NetBeans
  • Surefire plugin

Sample code to reproduce:

package testNG;

import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class TestGroupsAnnotations {

	@BeforeGroups(groups = "testGroup1")
	public void beforeGroups1() {
		System.out.println("Test Before Groups Annotation");
	}
	
	@AfterGroups(groups = "testGroup1")
	public void afterGroups1() {
		System.out.println("Test After Groups Annotation");
	}


	@Test(groups = "testGroup1")
	public void test1() {
		System.out.println("Test #1");
	}
	@Test(groups = "testGroup2")
	public void test2() {
		System.out.println("Test #2");
	}
	@Test(groups = "testGroup1")
	public void test3() {
		System.out.println("Test #3");
	}
	@Test(groups = "testGroup2")
	public void test4() {
		System.out.println("Test #4");
	}
	
	
	@BeforeGroups(groups = "testGroup2")
	public void beforeGroups2() {
		System.out.println("Test Before Groups Annotation #2");
	}
	
	@AfterGroups(groups = "testGroup2")
	public void afterGroups2() {
		System.out.println("Test After Groups Annotation #2");
	}
	
}

Version 7.1.0 behavior Ec7 1 0 7 1 0

Version 7.0.0 behavior Ec7 0 0 7 0 0

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 36 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Krishnan,

At this point, I think what the original intent is or was is irrelevant (assuming there was even one 😃).

Quite a few users seem to rely on the old behavior so we should find a way to restore it.

And if we can’t make this consistent with the current design, I think it would be fine to have a special flag just to enable this behavior, so that new users don’t rely on it accidentally but old users can still use it.

I totally agree with @Elisedlund-ericsson. it is very strange that only Before/AfterGroups are excluded if not defined in suite xml but for all other methods they are executed. But the group filter in the suite xml applies on all methods that have the group, not only Before/AfterGroups.

I also have issue with this missing on the latest testng (and its forsure working in previous TestNG versions!)

I find it very stupid and strange that it will run the test case in with group:

@Test(groups = "testGroup1")
public void test1() {
	System.out.println("Test #1");
}

but not run the before/after methods:

@BeforeGroups(groups = "testGroup1")
public void beforeGroups1() {
	System.out.println("Test Before Groups Annotation");
}

@AfterGroups(groups = "testGroup1")
public void afterGroups1() {
	System.out.println("Test After Groups Annotation");
}

if it did not run the “@Test(groups = **“testGroup1”)” because that group was not selected I would be more understanding, but as it is now it runs that test case and then I expect that it would run the @BeforeGroups/@AfterGroups(groups = “testGroup1”) which I consider the test case have dependencies towards,

as the “@Test(groups = “testGroup1”)” is running, to me that is the same thing as the group being active and therefor all the groups dependencies should also run

Hi,

I think we also have phased this issue now when we Testing TestNg 7.5, we are using 6.14 at the moment and are working on uplifting to latest TestNg, but it seems not possible due to this change. We have over 500 Projects and thousands of test cases in different organizations and thousands developers. It will never be possible to adapt all of the Test and as I see it is not even a possible alternative to this change.

In many Test Cases we using groups to run different Before/After methods/class and we have test within the same test class with different groups and without groups. As I understand now we must specify the groups in the suite, but that will not work for us.

I don’t think this was a bug correction, I think that removing a very useful feature from TestNg that simplifies test cases allot.

@MohabMohie - This wasn’t a regression to begin with. This was a bug that always existed in TestNG which was fixed. So there’s no way of doing this, unless and until, you know what the groups are. If that knowledge is there, then you can very well use an implementation of IAlterSuiteListener wherein you can weave in the group information programmatically into the suite and then work with it. There’s no property that can be used to toggle this.

On a side note, I would be curious to know as to what exists within these @BeforeGroups and @AfterGroups ? It sounds like they perhaps are in the wrong place to begin with. If its a setup/tear down that you would like to be executed all the time, you can perhaps put them within an implementation of IInvokedMethodListener which you can wire in via a SPI mechanism ?

If you can add more details (please feel free to be as elaborate as possible) I can suggest some alternatives.

I can reproduce the same issue on my environment as well. Eclipse (12-2019) + JDK 13 + Maven

Working on TestNG 7.0.0 Not working on TestNG 7.1.0