-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Private @Before* methods aren't called when defined in a base class #2097
Comments
Right, it is the expected behavior:
https://testng.org/doc/documentation-main.html#annotations @krmahadevan Could you confirm the issue? I'm surprised it is not working like that. |
@juherr @zhogov we noticed this behavior too at my organization. I think the reason that inheritance only works with public annotated methods, is probably due to how Java does inheritance in general. Leaving annotations aside, private methods in a superclass are not inherited by child classes. And since annotations are tied to methods, it would make sense that those annotated private methods are not accessible to child classes, and thus do not run. When TestNG decides to run all annotated methods like @BeforeMethod/@AfterMethod in a class, it would look at those defined in the class itself, which will not include any private ones defined in the parent class :) @juherr I would think this can be good for design, as it would allow you to define parent-only annotated methods that you do not wish your child classes to run. I'm glad @zhogov you brought it up here though! Hope this might shed some light on this behavior. |
@Proryanator I would expect this to work consistently across all method visibilities. As for
Maybe, we just need some clarification/decision from framework authors since it is a gray area now |
@Proryanator Good catch, I didn't notice the The behavior with private test methods is not documented and works with I think we should drop the support of private test methods in the next major version but we can just add a warning message. @krmahadevan WDYT? |
@juherr - Sure... We can add warning messages whenever TestNG is being used to invoke private methods... And then strip support for them later.. Kind of doesn't make sense to be honest to have private methods annotated using TestNG annotations and then having them be invoked. |
Do you think that could be like one of the intellisense errors you get if you tagged a private method with @BeforeMethod? Similar to that of deprecated but in a different sense. I would assume this would apply for any TestNG annotation applied to a private method too, so that message ought to say those too. Or one you'd see as a console warning? I'm not sure how these sorts of things work when warning downstream users :) |
Peeking through old open issues, looks like there's still an open one here for the same issue. In general how does closing of old old issues work? A few members with the rights to close old open/duplicate issues? Just curious. |
Yes. That is correct. |
#473 has a similar title but looks a bit different. @Proryanator At our side, only console warnings are possible but IDEs can manage a better display if they want to warn against this bad usage (ping @missedone @akozlova ) |
IDEA could warn and actually warn about junit malformed methods but not about testNG. We plan to prettify the inspections for both test frameworks and this issue also should be addressed. Sorry for the inconvenience |
@krmahadevan these private test methods from parent class are excluded at the beginning by the |
@jiong-fu - Sure it is. But then again it would be a one time exercise that gets done at the beginning of the execution right? Also I am not sure, how much of an additional cost are we adding here which warrants a concern. Would you happen to have any info around that ? |
Yes, the collection of the test methods to be executed gets done only once by initialization method.
I assume that you referred to the additional cost in performance? I am not sure if the performance was somehow measured / monitored before and if it is a big concern. |
It shouldn't be any more cost that it's already doing, canInclude() already looks at whether it's public or private, so we're already doing reflection there 🤔 Right in extractMethods, that unused part of the if, you could log the warning there. Of course, if any other rules change inside of canInclude, you can't necessarilly say "Skipping @BeforeMethod because it's private" just from canInclude(). |
In addition to visibility check, a method should be checked whether it is annotated as TestNG configuration method. Otherwise for example an unannotated private method in base class is excluded only because it is not visible to the child.
With the current implementation, it can only be concluded that a method should not be included, but the reason may vary. |
Ah I see, those private methods I mentioned are just for methods in general. Yeah you're right, somewhere outside of there you'd need to also check if they're TestNG annotated methods before showing the warning, good catch! |
TestNG Version
6.14.3 and 7.0.0-beta3, Java 8 and 11
Actual behavior
Private
@Before*
methods aren't called when defined in a base class, but work when defined in the same class.Expected behavior
Private
@Before*
methods should be called, whether they were inherited or notIs the issue reproductible on runner?
Test case sample
Following test passes - calls
@BeforeMethod
Following test fails -
@BeforeMethod
is not called:Test case project: https://github.com/zhogov/testng-issue-2097
The text was updated successfully, but these errors were encountered: