3
votes

[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

1
Which version of SpecFlow are you using? Which test runner are you using (NUnit, XUnit, SpecFlow+Runner, MSTest)?Andreas Willich
Hi, I updated my question. I am using SpecRun.SpecFlow to run my tests locally / in Visual Studio OnlineCode Rocker
is your [AfterTestRun] method in a class which is inherited?Sam Holder
Yes, the class which contains all the hooks i.e. [BeforeTestRun] , [BeforeScenario] etc is inherited by the step class, which on the last step adds data to the db.Code Rocker
Sorry didn't see your reply (if you had used @samholder in the message I would have got a notification). The inheritance is your issue. Steps are global in specflow, there is no need to inherit. See this answerSam Holder

1 Answers

1
votes

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