-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Before and After methods invoked for unused step definition classes #1005
Comments
No, classes that contain step definitions are only instantiated right before the (first) step definition or hook method defined in them is called.
Yes, the semantic of a Conceptually both step definitions and hooks are global (if they are on the classpath step definitions can be used, and hooks will be executed), the class they are defined in a secondary, so there is really no concept for speaking about "the hooks of the classes from which step definitions are actually used". |
I have also found that all step definition objects are instantiated for each scenario, even if they do not contain any steps which are used in that scenario. Perhaps this behaviour is specific to the Java 8 steps (whose steps are defined at runtime, not statically) or perhaps it depends on which CI you use (I am using Picocontainer).
I tend to agree with this, although I can see that @brasmusson's suggestion might be useful in practice. I think the practical solution is to make the @before and @after methods very cheap to run if they are not needed (e.g. detect if any cleanup is needed and do nothing in @after if not). |
re test classes from project dependencies: those classes should not be on your classpath: projects should not, by convention, export their test classes. See #999 |
You may well be able to get fine-grained control of this if you switch the DI framework for the steps -- have you tried that? |
I challenge the statement "before and after methods for step definition classes", there is nothing that says that a class that define a before or after method also needs to defined a step definition. Comparing with an example in the Cucumber-Ruby project (examples/i18n/en), the step definitions are defined in the file The biggest problem with changing the behaviour "to only invoke @after and @before methods from a class, if the scenario actually uses a step definition from that class", is that since there are no requirement that a class that defines a hook also needs to define a step definition, the only safe assumption is that "in the wild" there are project which defines hooks in classes without step definitions. With this behaviour change, those projects would suddenly start to fail, and there may not be an easy way to fix that, the thinking the "before and after methods for step definitions classes" may not fit those project at all.
I ran the
I was not correct when I wrote "if they are on the classpath step definitions can be used, and hooks will be executed", the correct statement is "if they are on the classpath (of course), and they are in a package that Cucumber-JVM is instructed to search for hooks and step definitions (the The first thing I think of to solve the problem with the step definition class in the problem description if tagged hooks does not fit the problem, is not to use a before hook at all, but either to call
|
re whether definition classes are instantiated even if not needed -- I expect this is a "Java8" difference. The When Java8 step definitions are used, the steps are defined dynamically at runtime, see https://cucumber.io/docs/reference/jvm#lambda-expressions-java-8 . So cucumber won't be able to tell if a class contains any Steps or any relevant Steps without instantiating it. Certainly, it is my experience that I don't think we need to settle this as part of this issue though -- it seems tangential to me. |
I see how the Currently there are two options:
I am really looking for a mechanism that allows me to organise my step definitions by domain and be able to hook on scenarios only if this domain is used. My question is, can we do a better job? And how could we implement this (considering that my current idea - only running the hooks if a step definition from that class is used - is pretty much rejected).
That is another issue not related to this topic 😉 Got that! |
Actually now I think of it, if the domains are properly organised by packages, and the step definitions for that domain share the same package, you can just load multiple glue packages. So that seems like the preferred way of doing this then. |
It seems this issue has reached a satisfying conclusion. PS: Sup SERG. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Currently, all classes that contain step definitions are instantiated and all before and after methods are ran. This behaviour is very unpreferable. For some stories you just don't need some (potentially heavy) domains to be setup. So if one scenario requires a web container to be set up, and an other scenario does not use any step definitions from that
WebContainerStepDefinitions
class, the@Before
starting the web container should not be started for the second scenario.Users are now working around this issue by introducing annotations/labels. This is in my opinion very unwanted, as your user stories now become aware of and dependent on the implementation of your step definitions. Furthermore it is error prone, because you now have step definitions available for use, for which state may not be initialized nor will be properly cleaned up, would you forget to use the correct annotation.
In my opinion the only clean solution is to only invoke
@After
and@Before
methods from a class, if the scenario actually uses a step definition from that class.See also the little debate we had at our course: SERG-Delft/jpacman-framework#92
This issue was also mentioned in issue: #993
Ping @RichardBradley
The text was updated successfully, but these errors were encountered: