Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specflow test results accessible from AfterScenario hook?

Tags:

c#

nunit

specflow

Is there any way to access the test results (success/fail, maybe even asserts, etc) from a Specflow AfterScenario hook? I don't see anything, but it seems like something that would be included.

like image 765
Marty Avatar asked Aug 23 '13 15:08

Marty


People also ask

What happens when SpecFlow tests are executed?

When SpecFlow tests are executed, the execution engine processes the test steps, executing the necessary test logic and either finishing successfully or failing for various reasons. While executing the tests, the engine outputs information about the execution to the test output.

What are the best features of the SpecFlow hooks?

Another cool feature of the SpecFlow hooks is that you can specific execution order if multiple hooks are specified of the same type. By default, the execution order is unspecified, and they can be executed in any order. To ensure that they are performed in a specified order, the hook attribute allows an arbitrary order to be configured.

What is the “beforescenario” hook?

For Example, let’s look at “BeforeScenario” hook and from the name itself it is evident, that this event will be raised before running any scenario from the feature. Think of it as test initialize setup in other unit testing frameworks like MSUnit (for C#) and Junit (for Java) First, let’s see, how the hooks are added as part of the tests.

What is the execution order of a SpecFlow hook?

Another cool feature of the SpecFlow hooks is that you can specific execution order if multiple hooks are specified of the same type. By default, the execution order is unspecified, and they can be executed in any order.


2 Answers

You can get hold of the test result by peeking into the ScenarioContext.Current. There's a TestError property that may help you.

See this wiki (https://github.com/techtalk/SpecFlow/wiki/ScenarioContext) for more information.

like image 192
Marcus Hammarberg Avatar answered Oct 27 '22 00:10

Marcus Hammarberg


Yes, there is, but you need to use reflection. In your [AfterScenario] block use the following:

PropertyInfo pInfo = typeof(ScenarioContext).GetProperty("TestStatus", BindingFlags.Instance | BindingFlags.NonPublic);
MethodInfo getter = pInfo.GetGetMethod(nonPublic: true);
object TestResult = getter.Invoke(ScenarioContext.Current, null);

TestResult will be OK, MissingStepDefinition etc.

like image 21
user1094145 Avatar answered Oct 26 '22 23:10

user1094145