ScenarioContext.TestError is null if error in BeforeScenario

346 views
Skip to first unread message

Jason Torres

unread,
Aug 14, 2017, 3:11:15 AM8/14/17
to SpecFlow
Hello, 

I am trying to solve a problem and don't seem to have a solution. We are using Nunit and SpecFlow for our testing and some of our code in the BeforeScenario is failing but the ScenarioContext.TestError is null. 

I am wondering why the error is not being added into the ScenarioContext. Other things are added properly like Scenario title, etc but not the error.

You can prove this by simply adding an Assert.Fail() in your BeforeScenario hook and check on the AfterScenario ScenarioContext has TestError populated.

Is this a defect or is there some possible solution to log the error in TestError?

Thanks, 
Jason T.

Gáspár Nagy

unread,
Aug 15, 2017, 9:14:05 AM8/15/17
to SpecFlow
Hi,

I would not say it is a defect, but definitely an unsupported scenario.

Generally having a failing hook (like a before scenario) is quite a gray area. Although it is not stated officially, but I think the codebase assumes that such hooks never throw exceptions (ie handling their own errors). For example I don't know what is the expected behavior if a BeforeScenario hook fails: shall other BeforeScenario hooks run still (probably not); shall AfterScenario hooks run still (good question, they run after a test error, but here we are in a state where even the test initialization was failing).

The bottom line is: there is no specific reason for not populating the TestError in this case, but it would be even better to solve this more generally and clear out how exceptions in hooks should be handled. PRs are welcome!

Br,
Gaspar

rohit gupta

unread,
Aug 22, 2017, 10:03:24 AM8/22/17
to SpecFlow
Hi,

I also ran into a similar situation recently. I have 2 AfterScenarios in Hooks. The first AfterScenario is specific to a some particular tests and the second AfterScenario is something I would execute for all the tests after first AfterScenario execution. Now, In case the First AfterScenario throws an exception, the execution skips the second AfterScenario and moves directly to AfterTestRun.
To handle this, I have kept the first AfterScenario code within a Try-Catch block and suppressing the exception in Catch. This help me to execute the second AfterScenario regardless of the exception in the First AfterScenario.

Regards,
Rohit
Reply all
Reply to author
Forward
0 new messages