[SpecFlow] Add Option to Run After Hooks Despite Exceptions
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:
- AfterScenario hook #1 - run for all scenarios - capture a screen shot
- AfterScenario hook #2 - run for specifically tagged scenarios - do some test-specific cleanup (like page navigation or deleting records)
- AfterScenario hook #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.
Please sign in to leave a comment.
Comments
0 comments