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

Weird @BeforeMethod and @AfterMethod behaviour with dependsOnMethods #550

Closed
mbocek opened this issue Oct 1, 2014 · 11 comments · Fixed by #2845
Closed

Weird @BeforeMethod and @AfterMethod behaviour with dependsOnMethods #550

mbocek opened this issue Oct 1, 2014 · 11 comments · Fixed by #2845

Comments

@mbocek
Copy link

mbocek commented Oct 1, 2014

Sample code:

public class BeforeAfterMethodTest {

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

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

    @BeforeMethod(dependsOnMethods = "test")
    public void beforeMethod() {
        System.out.println("before");
    }

    @AfterMethod(dependsOnMethods = "test")
    public void afterMethod() {
        System.out.println("after");
    }
}

Result:

org.testng.TestNGException: 
test.BeforeAfterMethodTest.beforeMethod() is depending on method public void test.BeforeAfterMethodTest.test(), which is not annotated with @Test or not included.
    at org.testng.internal.MethodHelper.findDependedUponMethods(MethodHelper.java:111)
    at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:240)
    at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:317)
    at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:59)
    at org.testng.internal.TestNGMethodFinder.findConfiguration(TestNGMethodFinder.java:191)
    at org.testng.internal.TestNGMethodFinder.getBeforeTestMethods(TestNGMethodFinder.java:64)
    at org.testng.TestClass.initMethods(TestClass.java:167)
    at org.testng.TestClass.init(TestClass.java:86)

Regarding documentation:

http://testng.org/javadoc/org/testng/annotations/BeforeMethod.html#dependsOnMethods()
The list of methods this method depends on. There is no guarantee on the order on which the methods depended upon will be run, but you are guaranteed that all these methods will be run before the test method that contains this annotation is run. Furthermore, if any of these methods was not a SUCCESS, this test method will not be run and will be flagged as a SKIP. If some of these methods have been overloaded, all the overloaded versions will be run.

My expectation was that the before and after method will be run only before and after test() method.
Please can you provide some fix or explanation.

Thank you.

@FibreFoX
Copy link

FibreFoX commented Oct 1, 2014

Maybe the solution would be to remove "dependsOnMethods" on after-/beforeMethod-annotations? Or to limit the type of "before"-methods to have the BeforeMethod-annotation (which sounds more natural to me)

@mbocek
Copy link
Author

mbocek commented Oct 1, 2014

The idea is runs some specific setup/teardown operation before and after specific test method. This is the reason why i want to use this mechanism.

@FibreFoX
Copy link

FibreFoX commented Oct 1, 2014

Would it be possible to create two separated test-classes for this?
For me "@BeforeMethod" means "run this before executing the @Test-method", but you are using this to bind it to "test()", which itself is a @Test-annotated method. It feels wrong to me, but why don't you just call this setup within your test()-method (which is another option instead of writing a whole new test-class)?

@mbocek
Copy link
Author

mbocek commented Oct 1, 2014

Thank you for your comments but in this point of view TestNG developers should update documentation - probably @Before_/@After_ annotation attributes (remove dependsOnMethod from attributes).

You are right i can do that, but i want to group my test methods from logical point of view to the classes, next level is packages. Regarding calling setup and tear down inside test method it is really not good concept. Especially for tear down - i.e. when test fails in exception in test logic i will loose possibility to clean it up.

@smaudet
Copy link

smaudet commented Oct 21, 2014

I think mbocek is correct, to me this behavior is wrong. Either moving the test to a separate class or removing the parameter from the annotation both feel like patchy, kludgy solutions.

@573
Copy link

573 commented Mar 17, 2015

+1 @mbocek

1 similar comment
@bredy
Copy link

bredy commented Mar 26, 2015

+1 @mbocek

@krmahadevan
Copy link
Member

@juherr - What do you think the behaviour here should be?

I can alter the implementation to throw an error that looks like below (for the same example test that is shared in this bug):

com.rationaleemotions.issue550.BeforeAfterMethodTest.beforeMethod() is configuration method that is depending upon non configuration methods.
	at org.testng.internal.MethodHelper.findDependedUponMethods(MethodHelper.java:148)
	at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:303)
	at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:397)
	at org.testng.internal.MethodHelper.lambda$collectAndOrderMethods$1(MethodHelper.java:75)

@krmahadevan
Copy link
Member

The idea is runs some specific setup/teardown operation before and after specific test method. This is the reason why i want to use this mechanism.

To achieve this, you can do something like below (My bad that this response is coming nearly 8 years after you mentioned the problem :) )

You can add ITestNGMethod as a parameter to the @BeforeMethod method and then inspect the parameter to check if it represents the method that you want to do a setup for (you can do that via either custom annotations which you can parse, or just by checking the method name)

@juherr
Copy link
Member

juherr commented Nov 28, 2022

@krmahadevan the exception message you propose is clearly better.

A similar check could be done for all methods that depend on methods of another type: test methods can only depend on test methods, but beforemethod can only depend on beforemethod too.

@krmahadevan
Copy link
Member

A similar check could be done for all methods that depend on methods of another type: test methods can only depend on test methods, but beforemethod can only depend on beforemethod too.

Yes, this goes without saying. There would be edit checks for each of the configurations that would validate the following:

  1. @Test can depend only on other @Test
  2. @BeforeMethod can depend on only another @BeforeMethod (and so on)

@krmahadevan krmahadevan added this to the 7.6.2 milestone Nov 29, 2022
krmahadevan added a commit to krmahadevan/testng that referenced this issue Dec 3, 2022
krmahadevan added a commit to krmahadevan/testng that referenced this issue Dec 3, 2022
krmahadevan added a commit that referenced this issue Dec 3, 2022
@krmahadevan krmahadevan modified the milestones: 7.6.2, 7.7.0 Dec 6, 2022
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.

7 participants