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

Suggestion: Add Option to Run After Hooks Despite Exceptions #978

Closed
AndyLPK247 opened this issue Dec 4, 2017 · 8 comments
Closed

Suggestion: Add Option to Run After Hooks Despite Exceptions #978

AndyLPK247 opened this issue Dec 4, 2017 · 8 comments

Comments

@AndyLPK247
Copy link

It is possible to have multiple after hooks, and the order of execution may be controlled with the Order parameter. However, if an exception is thrown from an after hook method, then no subsequent after hooks of the same type are executed. This is problematic for my team because we do web testing using Selenium WebDriver. We rely upon after hooks to properly clean up our web tests. It is also arguably unintuitive - my original presumption was that after hooks would always be run for safety purposes.

Consider the following example:

  1. AfterScenario hook Syntax validation for feature files in Visual Studio #1 - run for all scenarios - capture a screen shot
  2. AfterScenario hook Intellisense for feature files in Visual Studio  #2 - run for specifically tagged scenarios - do some test-specific cleanup (like page navigation or deleting records)
  3. AfterScenario hook Create step binding skeletion from Visual Studio #3 - run for all scenarios - log out and possibly dispose the web driver

If hook #2 throws an exception, then hook #3 is not run.

There are a few workarounds:

  • I can combine all AfterScenario hooks into one. However, that is not a good solution for scalable development. Every custom case would need to be shoved into one method. Having separate hooks with careful ordering is definitely preferred.
  • I can wrap every AfterScenario hook in a "try { } catch (Exception) { }" block. This isn't pretty, but it would guarantee that the final cleanup hook would get run.

What would be really nice is if After hooks had an extra attribute parameter called "AlwaysRun" or "Guaranteed" or something similar that would make the hook run even if previous hooks aborted with exceptions. I could add such a parameter to hook #3 and not need any of the aforementioned workarounds. This sort of solution would be applicable for all after hook types: step, scenario, scenario block, and test run.

@SabotageAndi
Copy link
Contributor

@gasparnagy We had in the past a discussion about the topic of exceptions in hooks, but I don't find it anymore.
I wouldn't change the current behaviour, because this would be a breaking change for users, which would be silent for them (the test is suddenly green after the update, where before there was an error).
Putting everywhere try-catch blocks is not nice in the binding code.

But somehow I have a little aversion against the idea with the additional attribute. Doesn't feel right somehow for me.
If we do this, I would name the attribute "ContinueOnError".

@AndyLPK247
Copy link
Author

@SabotageAndi - I like your proposed parameter name "ContinueOnError" better than the ones I suggested.

Also, at the very least, could the current behavior of after hooks please be documented on the SpecFlow doc pages? The current Hooks page does not indicate that if an exception happens in an after hook, then subsequent after hooks of the same type will not be called. It would be very helpful to make this explicitly stated to avoid confusion. Users could presume either the current behavior or (like I did) that subsequent after hooks would be run despite exceptions. https://github.com/techtalk/SpecFlow/wiki/Hooks

@SabotageAndi
Copy link
Contributor

@AndyLPK247 you are right, this information is missing in the documentation
@Stephen-Mc could you add it please? Thanks!

@Stephen-Mc
Copy link
Contributor

I've added a note to the hooks section under "Hook Execution Order". This behaviour applies to both before and after hooks. Here's the change:

Note: If a hook throws an unhandled exception, subsequent hooks of the same type are not executed. If you want to ensure that all hooks of the same types are executed, you need to handle your exceptions manually.

@AndyLPK247
Copy link
Author

AndyLPK247 commented Dec 5, 2017 via email

@goncharr
Copy link

Hi @SabotageAndi , In my case I'm using an attribute "AfterTestRun" and after that running DB Hook (cleaning test data) - is it possible to add some functionality which helps to skip running DB Hook if some of the tests are failed. I've tried to use ScenarioContext.Current but unfortunately it didn't work as this Context saved only latest scenario.

@SabotageAndi
Copy link
Contributor

To make it easier for you all to vote on feature requests and influence our prioritization of them in our backlog, we created in our new support tool a special forum to have a better overview of all of them. We moved this feature request there and you can find it here. Sadly we could not take over the existing votes. So if you are still interested in this feature request, please vote again on the new list.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants