Specflow 参数化整表

标签 specflow

我正在编写一个测试,我想重用它,因此我试图对整个表进行参数化。该表位于我的“然后”声明中,需要验证的表取决于团队。

目前我的场景大纲如下所示:

Given <teamName> uses this end point
And the response is a Json
When I perform a query to http:...
Then I validate all the fields I need:
|DataElement|Validation                     |jsonPath           |
|element1   |validate that it is not null   |data.structure.path|
|element2   |validate a name                |data.structure.name|

所以我知道我可以通过参数化表中的数据来验证每一行:
|DataElement|Validation                     |jsonPath           |
|<value>    |<Specific validation performed>|<Json Path to query|

然后做例子

但是根据哪个团队使用相同的端点,所需的数据元素和验证非常不同,所以我想参数化整个表对象,如下所示:

然后我验证我需要的所有字段:
<TeamTable>

Examples:
|Team A Table|
|DataElement|Validation                     |jsonPath           |
|element1   |validate that it is not null   |data.structure.path|
|element2   |validate a name                |data.structure.name|
|element1   |validate age is valid          |data.structure.age |


|Team B Table|
|DataElement|Validation                     |jsonPath                |
|element1   |validate is a Date             |data.structure.date     |
|element2   |validate something more        |data.structure.something|
|element1   |validate US postcode           |data.structure.postcode |

是否可以?如何参数化整个表?

最佳答案

Specflow 确实支持表参数,这里是一个例子:

When following transactions are added
| TransactionDate | TransactionAmount | AccountNumber | Type | CR/DR |
| 1/20/12         | 10,000            | 102           | Cash | DR    |
| 1/20/12         | 6,500             | 106           | Cash | DR    |
| 1/21/12         | 10,001            | 102           | Cash | DR    |

    [When(@"following transactions are added")]
    public void WhenFollowingTransactionsAreAdded(Table table)
    {
        // Now you can either do for each
        foreach (var row in table.Rows)
        {
             // do stuf
        }

        // Or use an assist helpers to map table to an object
        var transactions = table.CreateSet<Transaction>();
    }

有关 Assist 助手的更多帮助,请参阅 SpecFlow docs

更多关于表格的基本信息 here

关于Specflow 参数化整表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11949669/

相关文章:

c# - 在 SpecFlow 步骤文件中使用依赖注入(inject)

asp.net - 用于集成测试 asp.net 应用程序的不同资源文件

c# - Specflow 2022 生成步骤定义文件 - VSC BDD : All steps have been defined in this file already

c# - 如何使用动态值运行 Specflow/Specrun 特征文件

.net - SpecRun.SpecFlow 1.2.0 与 UAP 不兼容,Version=v10.0

watin - 支持 javascript/ajax 的 Specflow headless 浏览器测试

visual-studio-2010 - 为什么 Visual Studio 中的测试 View 如此慢?

plugins - 自定义 SpecFlow TestGeneratorProvider 注册在测试生成时导致 NullReferenceException

cucumber - SpecFlow-在不同类中具有相同正则表达式的步骤(给出)不独立执行

c# - BoDi.ObjectContainerException 接口(interface)无法解析 : OpenQA. Selenium.IWebDriver