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

@BeforeGroups only called if group is specified explicitly #118

Closed
quandor opened this issue Oct 28, 2011 · 10 comments · Fixed by #1995
Closed

@BeforeGroups only called if group is specified explicitly #118

quandor opened this issue Oct 28, 2011 · 10 comments · Fixed by #1995

Comments

@quandor
Copy link

quandor commented Oct 28, 2011

There are 3 Testclasses each belonging to a different group. Each Test has a method annotated with @BeforeGroups. Though this method is actually only in one case called before the group is executed.

  • successful case: explicitly specified to which group the annotation belongs via groups=.
  • 1st unsuccessful case: nothing additional specifed, though groups should be inherited by the @Test annotation of the containing class.
  • 2nd unsuccessful case: alwaysRun=true specified
    There is a maven project that reflects the described behaviour.
@paulwinters
Copy link

@cbeust

This still is an issue with the latest build of TestnG 6.9.10 with @BeforeGroups not inheriting group sets.

A more detailed break down is found here.
http://stackoverflow.com/questions/35495937/testng-beforegroupsinheritgroups-true-usage

@krmahadevan
Copy link
Member

Here's my observations ( This is as of TestNG 7.0.0-beta1 )

  • alwasyRun is NOT applicable for @BeforeGroups . The javadoc clearly says that.
  • With respect to the attribute inherited=true in @BeforeGroups, yes its not being honored and this looks like a bug.

The below sample will reproduce the problem

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

@Test(groups = "group1")
public class Group1Test {
   private Object testObject;

   @BeforeGroups
   public void setUpGroup() {
      testObject = new Object();
   }

   public void test1() {
      Assert.assertNotNull(testObject, "@BeforeGroups not invoked if nothing explicitly specified");
   }
}

testng xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite">

  <test verbose="0" name="group1 included">
    <groups>
      <run>
        <include name="group1"/>
      </run>
    </groups>
    <classes>
      <class name="testng_issue_118.Group1Test"/>
    </classes>
  </test>
</suite>

krmahadevan added a commit to krmahadevan/testng that referenced this issue Dec 31, 2018
Closes testng-team#118

As part of fixing this issue, flipped the attribute
“inheritGroups” to “false”. This was done because
this attribute has not worked till now and with the 
bug fix, it will start breaking compatibility for 
everyone, because now @BeforeGroups will inherit
any class level groups specified.

By inverting this attribute to false, backward 
compatibility is retained and it also gives a chance
to the users to turn it on explicitly when needed.
@petar-lichev
Copy link

petar-lichev commented Oct 20, 2023

@krmahadevan Doesn't this break the @BeforeClass and @ AfterClass logic?
Is it expected that the methods annotated with @BeforeClass are collected and ordered as AfterGroups methods? (I have no previous experience with TestNG)

I have a class annotated with @test(groups = { "some_group"}) which contains a method annotated with @BeforeClass and with the change here

case AFTER_GROUPS: afterGroups = configuration.getAfterGroups(); create = shouldCreateBeforeAfterGroup(afterGroups, annotationFinder, clazz, configuration.getInheritGroups()); isBeforeTestMethod = true; break;

create becomes true, while previously it was false. This broke our build because a circular dependency was found due to this change.

I short a method with the following configuration

@BeforeClass(alwaysRun = true)
  public void parentAbstractSetupClass

would be considered AfterGroups method configuration when the class is annotated with @ Test (groups{"blabla"}).

The same goes for an AfterClass.

@krmahadevan
Copy link
Member

@petar-lichev - do you have a sample project to share so that I can use it to reproduce the issue that you are experiencing?

@petar-lichev
Copy link

@krmahadevan Yes, I pushed it here - https://github.com/petar-lichev/sample-project
There are 2 branches - master and 6.14.3.

master branch uses testng version 7.4.0. When you run the SampleTest you get:

org.testng.TestNGException: The following methods have cyclic dependencies: BaseClass.setupMethod2()[pri:0, instance:null] SampleTest.setupMethod3()[pri:0, instance:null] BaseClass.setupMethod1()[pri:0, instance:null]

Which seems to come from getAfterGroupsConfigurationMethods and the change in findConfiguration
where

shouldCreateBeforeAfterGroup(afterGroups, annotationFinder, clazz, configuration.getInheritGroups());

returns true, because of the group configuration of the class level annotation of SampleTest. Which itself results in some cyclic dependency between
SampleTest.setupMethod3 -> BaseClass.setupMethod2 -> BaseClass.setupMethod1 -> SampleTest.setupMethod3

I've also pushed the branch 6.14.3(test passes there) to showcase the different behaviour in_getAfterGroupsConfigurationMethods_ .

Any idea if that is expected and it was the intended change in behaviour?

@krmahadevan
Copy link
Member

@petar-lichev

There are 2 branches - master and 6.14.3. master branch uses testng version 7.4.0.

The latest released versions are:

  • 7.5.1 - Last supported version if you are still on JDK8. No bug fixes will be done on top of this version.
  • 7.8.0 - Last released version that needs atleast JDK11. All bug fixes, feature requests etc are done on top of this.

Can you please help upgrade to 7.8.0 and update your findings ?

@petar-lichev
Copy link

petar-lichev commented Oct 21, 2023

@krmahadevan Same error. testMethodFinder.getAfterGroupsConfigurationMethods(m_testClass) throws the Exception about a circular dependency between the methods.

image

`The following methods have cyclic dependencies:
BaseClass.setupMethod2()[pri:0, instance:null]
SampleTest.setupMethod3()[pri:0, instance:null]
BaseClass.setupMethod1()[pri:0, instance:null]

at org.testng.internal.Graph.topologicalSort(Graph.java:122)
at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:376)
at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:447)
at org.testng.internal.MethodHelper.lambda$collectAndOrderMethods$1(MethodHelper.java:76)
at org.testng.util.TimeUtils.computeAndShowTime(TimeUtils.java:45)
at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:72)
at org.testng.internal.TestNGMethodFinder.findConfiguration(TestNGMethodFinder.java:212)
at org.testng.internal.TestNGMethodFinder.getAfterGroupsConfigurationMethods(TestNGMethodFinder.java:110)
at org.testng.TestClass.initMethods(TestClass.java:205)
at org.testng.TestClass.init(TestClass.java:102)`

@krmahadevan
Copy link
Member

Will take a look. Thanks.

@petar-lichev
Copy link

petar-lichev commented Oct 21, 2023

Thanks. These below are my only other observations.

private static boolean shouldCreateBeforeAfterGroup( String[] groups, IAnnotationFinder finder, Class<?> clazz, boolean isInheritGroups) { if (!isInheritGroups) { return groups.length > 0; } ITestAnnotation test = AnnotationHelper.findTest(finder, clazz); if (test == null) { return groups.length > 0; } return groups.length > 0 || test.getGroups().length > 0; }

Basically with the adding of the shouldCreateBeforeAfterGroup method in the switch case, the behaviour changed, because of the @ Test(groups={"some_group"}) in SampleTest and the following now returns true:

test.getGroups().length > 0

image

@petar-lichev
Copy link

@krmahadevan Hi, did you manage to take a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants