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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With