3

[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

Code Rocker
  • 500
  • 4
  • 15
  • Which version of SpecFlow are you using? Which test runner are you using (NUnit, XUnit, SpecFlow+Runner, MSTest)? – Andreas Willich Apr 01 '16 at 08:07
  • Hi, I updated my question. I am using SpecRun.SpecFlow to run my tests locally / in Visual Studio Online – Code Rocker Apr 01 '16 at 10:01
  • is your [AfterTestRun] method in a class which is inherited? – Sam Holder Apr 04 '16 at 08:03
  • 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 Apr 04 '16 at 10:43
  • 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 answer](http://stackoverflow.com/a/29851420/97614) – Sam Holder Apr 05 '16 at 10:57

1 Answers1

1

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

Community
  • 1
  • 1
Sam Holder
  • 32,535
  • 13
  • 101
  • 181