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

Allow Before/After Hooks to be renamed #2798

Closed
ll-cyan opened this issue Sep 22, 2023 · 5 comments · Fixed by #2881
Closed

Allow Before/After Hooks to be renamed #2798

ll-cyan opened this issue Sep 22, 2023 · 5 comments · Fixed by #2881
Labels
🙏 help wanted Help wanted - not prioritized by core team

Comments

@ll-cyan
Copy link

ll-cyan commented Sep 22, 2023

🤔 What's the problem you're trying to solve?

Cucumber for Java allows defining different @after hooks and allows ordering them, during execution all steps are just shown as After, making it difficult to discern which step failed without explicit logging inside of the steps

✨ What's your proposed solution?

Allow the annotation of the Hook to include a name or displayName such as
@after(order = 2, name="GenericCleanUp")
@after(order = 1, name="SpecificCleanUp")

and show the provided name during execution / reporting instead of showing After for every Hook annotated with @after

⛏ Have you considered any alternatives or workarounds?

Currently it is needed to either know exactly which after hooks get executed for each scenario in which order to know which one failed or to put logging into each Afterhook to check afterwards

@mpkorstanje
Copy link
Contributor

Cucumber has a few different runners and reports. Where do you see the after hooks represented as After?

@ll-cyan
Copy link
Author

ll-cyan commented Sep 25, 2023

Im running the features through IntelliJ's run dialogue as such
image
image

@matosjoa
Copy link

Same thing here, would love to see this implemented

@mpkorstanje
Copy link
Contributor

mpkorstanje commented Apr 19, 2024

Currently plugins are based on the events from io.cucumber.plugin. And eventually we'll migrate all plugins to consume Cucumber messages instead. This includes a named Hook so at that point it would make sense to introduce named hooks in Cucumber as well. But I do think we can make this report a bit more readable in short term and I would be happy to take a pull request for that.

This report is generated by the TeamCityPlugin and hooks are named here:

private String extractName(TestStep step) {
if (step instanceof PickleStepTestStep) {
PickleStepTestStep pickleStepTestStep = (PickleStepTestStep) step;
return pickleStepTestStep.getStep().getText();
}
if (step instanceof HookTestStep) {
HookTestStep hook = (HookTestStep) step;
HookType hookType = hook.getHookType();
switch (hookType) {
case BEFORE:
return "Before";
case AFTER:
return "After";
case BEFORE_STEP:
return "BeforeStep";
case AFTER_STEP:
return "AfterStep";
default:
return hookType.name().toLowerCase(Locale.US);
}
}
return "Unknown step";
}

It should be possible to include the hooks method name using hook.getCodeLocation() so that hooks are rendered as Before(<methodName>). Unfortunately the current events in the Plugin API are old and crummy so you will have to parse the code location string to extract the method name.

An example of that can be seen here for Java, and just below it is an example for Java 8.

Matcher javaMatcher = ANNOTATION_GLUE_CODE_LOCATION_PATTERN.matcher(testStep.getCodeLocation());
if (javaMatcher.matches()) {
String fqDeclaringClassName = javaMatcher.group(1);
String methodName = javaMatcher.group(2);
return String.format("java:test://%s/%s", fqDeclaringClassName, methodName);
}

If this method doesn't work, the hooks name should just be rendered as Before. Note

To test this, be mindful that you have to declare an explicit location string in the stubbed step definitions. For example:

singletonList(new StubHookDefinition("com.example.HookDefinition.<init>(HookDefinition.java:12)")),

@mpkorstanje mpkorstanje added the 🙏 help wanted Help wanted - not prioritized by core team label Apr 19, 2024
@maestro-game maestro-game linked a pull request May 15, 2024 that will close this issue
7 tasks
mpkorstanje pushed a commit that referenced this issue May 16, 2024
The TeamCityPlugin for IntelliJ IDEA now uses the hook's method name
for the name of the hook itself.

For example, if we have a hook with the method name 
`com.example.HookDefinition.beforeHook()``, the generated name will be
`Before(beforeHook)`.

If we have a hook with the method name
`com.example.HookDefinition.<init>(HookDefinition.java:12)`, the
generated name will be `Before(HookDefinition)`.

If the hook's method can't be identified then fallback to simple
hook name.

 Closes #2798
@balaram-vineeth
Copy link

@mpkorstanje is there any documentation on how to use cucumber messages?
I have been looking for some but can't seem to find any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙏 help wanted Help wanted - not prioritized by core team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants