Capturing NUnit or SpecFlow on Failed Test

So I am using SpecFlow with NUnit to develop end-to-end acceptance testing of a web application with Selenium. One of the things we do to try to assess the nature of the failure is to take a screenshot of the browser screen whenever the regression test fails. I currently do this by wrapping each of my tests in try / catch blocks and taking a screenshot in a catch with Selenium, then re-throwing the exception. This works, but makes the tests dirtier and more tedious.

Is there a way in NUnit or SpecFlow to call a hook when any test fails, before any teardown method is called?

+3


source to share


1 answer


You can use ScenarioContext to determine if a script caused an error.

ScenarioContext.Current.TestError

      



If not null, then an error has occurred. You can check this and use it to determine if you need to take a screenshot or not. You can see an example reference for the SpecFlow specification .

You can also crochet this AfterScenario so you don't need to have try / catch all over the world. He just checked each test at the end to see if an error occurred and create a screenshot.

+3


source







All Articles