建设这个.feature文件的背后会创建一个代码测试。然后您需要将每个步骤连接到一个方法。最简单的方法是,
1:调试测试,测试将失败,因为不确定。查看测试运行结果specflow帮助您通过为此测试添加模板。错误信息看起来像这样
Assert.Inconclusive失败。找不到与一个或多个步骤匹配的步骤定义。
[Binding]
public class StepDefinition1
{
[Given(@"a user signs up for a 30 day account")]
public void GivenAUserSignsUpForA30DayAccount()
{
}
[When(@"they login in 29 days")]
public void WhenTheyLoginIn29Days()
{
ScenarioContext.Current.Pending();
}
[Then(@"they will be let in")]
public void ThenTheyWillBeLetIn()
{
ScenarioContext.Current.Pending();
}
}
2:这个复制到一个新的specflow步骤的定义文件,它是填充有specflow属性基本上只是单元测试类。现在有一些技巧可以帮助你。在GivenAUserSignsUpForA30DayAccount方法中,我将创建一个将在具有30天试用帐户的测试中使用的用户。私人成员在这里可以很好地工作,所以你可以在方法之间访问它们,但是只有当所有的方法都在同一个类中时,这才有效。如果您尝试在多个要素/类之间重用方法,则需要考虑将对象保存到ScenarioContext中。3:当specflow测试运行时,它会查找具有匹配属性且具有相同字符串的方法。这里的一个诀窍是你可以使用方法属性中的通配符将参数传递给方法。有两个不同的文件卡
(。*)表示您正在向该方法传递字符串 (\ d +)表示您正在向该方法传递一个int。
因为您的When方法很常见,您可以像这样重用它。
[When(@"they login in (\d+) days")]
public void WhenTheyLoginInDays(int daysRemaining)
{
Account.DaysRemaining = daysRemaining;
}
4:最后加上你的断言到随后方法,这样最终的结果看起来是这样的。 (请注意,我个人重组功能一点的措辞,并通过它预期的结果这样的测试逻辑并不像我举的例子是讨厌的,看情形概括为数据驱动测试)
[Binding]
public class StepDefinition1
{
UserAccount user;
[Given(@"a user signs up for a 30 day account")]
public void GivenAUserSignsUpForA30DayAccount()
{
user = AccountController.CreateNewUser("bob", "password", AccountType.Trial);
}
[When(@"they login in (\d+) days")]
public void WhenTheyLoginInDays(int daysRemaining)
{
Account.DaysRemaining = daysRemaining;
}
[Then(@"they will (.*)")]
public void ThenTheyWillBeLetIn(string expected)
{
//check to see which test we are doing and then assert to see the expected result.
if(string.Compare(expected, "be let in", true)
Assert.AreEqual(LoginResult.Passed, LoginService.Login);
if(string.Compare(expected, "be asked to renew", true)
Assert.AreEqual(LoginResult.Passed, LoginService.Login);
}
}
我认为StepArgumentTransformation是在我问这个问题后的一段时间里被添加到Specflow中的。 – 2016-11-11 18:14:55
啊,真的。它仍然出现在我的搜索[相关问题](http://stackoverflow.com/questions/40553847/specflow-step-argument-transformation-on-table-cell-contents-with-createinstance),所以对于其他人' 清酒... – 2016-11-11 18:24:26