听起来您正在寻找纯粹的数据驱动测试。场景概述是如何在 SpecFlow 和行为驱动开发中实现这一点。您希望使用数据驱动测试的程度使该测试不适合 BDD 测试。这就是您遇到问题的原因 - 话虽如此,可能有一种方式可以在中间相遇。它将涉及场景大纲和一些修改的步骤,因此场景大纲中的占位符引用字典中的键。每一步都需要接受这个key作为参数,并据此处理数据:
When the form is filled in with "<Dictionary Key>"
Then the thing exists for "<Dictionary Key>"
Examples:
| Dictionary Key |
| key1 |
| key2 |
字典是字典的字典:
private readonly Dictionary<string, Dictionary<string, object>> testCaseData = new Dictionary<string, Dictionary<string, object>>()
{
{
"key1",
new Dictionary<string, object>()
{
{ "field1", "value1" },
{ "field2", "value2" }
}
}, {
"key2",
new Dictionary<string, object>()
{
{ "field1", "value3" },
{ "field2", "value4" }
}
}
}
还有你的步骤:
[When(@"the form is filled in with ""(.*)"""]
public void WhenSomething(string testCaseKey)
{
var formData = testCaseData[testCaseKey];
// Enter formData["field1"] in web form
// Enter formData["field2"] in web form
// Submit form
}
[Then(@"Then the thing exists for ""(.*)"""]
public void ThenTheThingExistsFor(string testCaseKey)
{
var expectedData = testCaseData[testCaseKey];
// Make your assertions
}
这至少应该消除功能文件中数据的复杂性,这样您就可以将每个步骤的措辞重点放在业务运营上。您也不一定需要在 C# 中对数据进行硬编码。我想你可以将它保存为文本、CSV 或 excel 电子表格,阅读它,然后将其解析成可用的东西,如果效果更好的话。