I’m using SpecFlow with Nunit and I’m trying to setup my enviroment tests using TestFixtureSetUpAttribute, but it’s never called.
I already tried to use MSTests and ClassInitialize attribute, but the same happen. The function isn’t called.
Any ideas Why?
[Binding]
public class UsersCRUDSteps
{
[NUnit.Framework.TestFixtureSetUpAttribute()]
public virtual void TestInitialize()
{
// THIS FUNCTION IS NEVER CALLER
ObjectFactory.Initialize(x =>
{
x.For<IDateTimeService>().Use<DateTimeService>();
});
throw new Exception("BBB");
}
private string username, password;
[Given(@"I have entered username ""(.*)"" and password ""(.*)""")]
public void GivenIHaveEnteredUsernameAndPassword(string username, string password)
{
this.username = username;
this.password = password;
}
[When(@"I press register")]
public void WhenIPressRegister()
{
}
[Then(@"the result should be default account created")]
public void ThenTheResultShouldBeDefaultAccountCreated()
{
}
Solution:
[Binding]
public class UsersCRUDSteps
{
[BeforeFeature]
public static void TestInitialize()
{
// THIS FUNCTION IS NEVER CALLER
ObjectFactory.Initialize(x =>
{
x.For<IDateTimeService>().Use<DateTimeService>();
});
throw new Exception("BBB");
}
private string username, password;
[Given(@"I have entered username ""(.*)"" and password ""(.*)""")]
public void GivenIHaveEnteredUsernameAndPassword(string username, string password)
{
this.username = username;
this.password = password;
}
[When(@"I press register")]
public void WhenIPressRegister()
{
}
[Then(@"the result should be default account created")]
public void ThenTheResultShouldBeDefaultAccountCreated()
{
}
Your
TestInitializeis not called because it is inside your Steps class and not inside in an Unit Tests (because the actual unit test is inside the.cswhich is generated from your.featurefile).SpecFlow has it’s own test-lifetime events which are called hooks, these are all the predefined hooks:
[BeforeTestRun]/[AfterTestRun][BeforeFeature]/[AfterFeature][BeforeScenario]/[AfterScenario][BeforeScenarioBlock]/[AfterScenarioBlock][BeforeStep]/[AfterStep]Note that this allows for greater flexibility in setup. For additional information see the documentation.
Based on the fact that you want to use the
TestFixtureSetUpattribute you will probably need theBeforeFeaturehook which will be called once before each feature, so you need to write:Note that the
[BeforeFeature]attribute needs astaticmethod.You should also note that if you are using the VS integration there is an project item type called
SpecFlow Hooks (event bindings)which creates a binding class with some predefined hooks to help you get started.