【问题标题】:SpecFlow test with Gherkin table also trying column name in tests带有 Gherkin 表的 SpecFlow 测试也在测试中尝试列名
【发布时间】:2016-05-09 20:20:56
【问题描述】:

我有这个 Specflow 功能:

Given I have CoolData with <a> , <b> , <c> and <d>: 
| a                | b        | c         | d            |
| cool data        |          | 1.3       | Cool notes   |
| cool data        | 1.4      |           | Cool notes   |
| cool data        | 1.3      | 1.3       | Cool notes   |
| cool data        | 1.3      | 1.3       | Cool notes   |  

我有这个方法:

[Given(@"Given I have CoolData with (.*) , (.*) , (.*) and (.*) :")]
    public void GivenIhaveCoolDatawithAnd(string p0, string p1, string p2, string p3, Table table)
    {
        var cool = new CoolData
        {
            a= p0,
            b= decimal.Parse(p1),
            c= decimal.Parse(p2),
            d= p3,
        };
    }

我的问题:当我运行这个测试时,p0p1p2p3 被映射到字面意思为 "&lt;a&gt;""&lt;b&gt;""&lt;c&gt;""&lt;d&gt;" 的字符串,而不是表中的值。我究竟做错了什么?我正在尝试对此表中的每一行重复单元测试。

【问题讨论】:

  • 看起来您只需要跳过包含列标题的第一行数据。

标签: c# specflow


【解决方案1】:

您误解了小黄瓜的工作原理。您所做的是将两个概念混为一谈,即表格和场景示例。见the documentation for cucumber,看看数据表和场景大纲的区别

我相信您在这种情况下不需要表格,因此您应该使用带有示例的场景大纲,如下所示:

Scenario Outline: some title
    Given I have CoolData with <a> , <b> , <c> and <d>

Examples:
| a                | b        | c         | d            |
| cool data        |          | 1.3       | Cool notes   |
| cool data        | 1.4      |           | Cool notes   |
| cool data        | 1.3      | 1.3       | Cool notes   |
| cool data        | 1.3      | 1.3       | Cool notes   |  

这将导致生成 4 个测试,每个测试运行示例中的一行数据

你还需要调整你的 step 方法以不期望现在有一个表格(因为表格现在是一组示例)

[Given(@"Given I have CoolData with (.*) , (.*) , (.*) and (.*)")]
    public void GivenIhaveCoolDatawithAnd(string p0, string p1, string p2, string p3)
    {
        var cool = new CoolData
        {
            a= p0,
            b= decimal.Parse(p1),
            c= decimal.Parse(p2),
            d= p3,
        };
    }

【讨论】:

  • 谢谢!但是,当我按照您描述的方式使用 Examples: 关键字时,Visual Studio 的错误列表中出现错误:Custom tool error: (14:2): expected: #EOF, #TableRow, #DocStringSeparator, #StepLine, #TagLine, #ScenarioLine, #ScenarioOutlineLine, #Comment, #Empty, got 'Examples:'
  • Scenario Outline 中可能需要一个大写的“O”
  • 噢!谢谢你让我直截了当。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-11
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多