Reputation: 559
I have a solution which contains a set of tests which can be run using nuints. I am trying to integrate specflow into it:
[TestFixture]
[Binding]
public class TestBase
{
protected IWindsorContainer Container {get; set; }
protected IMongoCollectionManager CollectionManager { get; set; }
protected IDatabaseManager DatabaseManager { get; set; }
[TestFixtureSetUp]
[BeforeScenario,CLSCompliant(false)]
public void Setup()
{
Container = TestFixture.Container;
CollectionManager = Container.Resolve<IMongoCollectionManager>();
DatabaseManager = Container.Resolve<IDatabaseManager>();
}
[TestFixtureTearDown]
public void TearDown()
{
Container.Release(CollectionManager);
Container.Release(DatabaseManager);
}
}
Everytime I run the feature file which basically just has one scenario it executes the before tests four times, before it even goes to the given of that scenario but when I run it using simple nuint it works correctly and gets called only once. Can any one help me figure out why please? Thanks, Bijo
Upvotes: 1
Views: 1052
Reputation: 1
Two things spring to mind:
Don't mix the NUnit attributes and SpecFlow attributes. You can get the same effect by wrapping up the code that does 'the work' into a separate class and compose it from a separate hooks / unit test class, adorned with their specific attributes.
When I have had a BeforeScenario execute multiple times it's been due to inheritance in the specflow hooks. I believe this is because the attribute gets inherited by the subclass and both base and sub are matched by specflow.
Upvotes: 0
Reputation: 53
Since they are automatically wired up by SpecFlow, you shouldn't need these NUnit attributes in your step class:
[TestFixture]
[TestFixtureSetUp]
[TestFixtureTearDown]
I am not sure how many scenarios you have in your feature, but BeforeScenario will run before each scenario. If you want code to run once before or after the feature you can use these hooks:
[BeforeFeature]
[AfterFeature]
If you want code to run once before or after each scenario in a feature you can use these hooks:
[BeforeScenario]
[AfterScenario]
Also, if you need CLSCompliant(false) on your method, you should add it to its own attribute:
[CLSCompliant(false)]
You can check out the SpecFlow docs to get more information on the SpecFlow Hook attributes on https://github.com/techtalk/SpecFlow/wiki/Hooks.
Upvotes: 2