Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Specflow [AfterTestRun] hook called twice

[AfterTestRun]

This hook for me is being called twice.

My C# code is correct and at the end of each Scenario I am saving my results to a Concurrent Bag.

Then I use the [AfterTestRun] hook to call the Concurrent Bag and save the data to a database. I see duplicated data, so I assume it’s being called twice.

Additional Info:
I am using SpecRun to run my tests in parallel with the following profile

Execution stopAfterFailures="1" retryCount="0" testThreadCount="3" testSchedulingMode="Sequential"

Packages Installed
SpecFlow Version 2.0.0
SpecRun.SpecFlow 1.3.0
SpecRun.Runner 1.3.0

I am using SpecRun.SpecFlow to run my tests.

Also, how will this hook behave if one has multiple scenarios within each feature? Currently I have 1.

Thanks

like image 470
Code Rocker Avatar asked Oct 29 '25 12:10

Code Rocker


1 Answers

Steps are global in specflow, inheritance to get step reuse is unnecessary. In fact if you do inherit step classes the the steps they contain end up being duplicated, and you see the issue you have here. See this answer for additional details.

the simple solution is to place the [BeforeScenario] methods into their own class, and do not have your step classes inherit this. If you need to share state between your steps and your before/after scenarios then use one of the state sharing techniques outlined here

like image 191
Sam Holder Avatar answered Oct 31 '25 01:10

Sam Holder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!