我正在将 SpecFlow 与 Nunit 一起使用,并且我正在尝试使用 TestFixtureSetUpAttribute 设置我的环境测试,但它从未被调用过。
我已经尝试使用 MSTests 和 ClassInitialize 属性,但同样的情况发生了。该函数未被调用。
任何想法为什么?
[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()
{
}
解决方案:
[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()
{
}