在 SpecFlow 的示例 Table 中有 Tables

Having Tables in Example Table in SpecFlow

我在 VisualStudio 2013 中使用 SpecFlow 来测试具有以下布局的特定 Web 表单
******************** 开始:表格 ******************
Name:________________
Age:_________________
经验:
工作地点 |年数 |
___________ | _________________ |
___________ | _________________ |
___________ | _________________ |
___________ | _________________ |
******************** 结束:表格 ******************

如何编写它以便我可以从示例中获取信息?

Given ...
When user fills 'Name-Box' with '<name>'
And user fills 'Age-Box' with '<age>'
And user fills experience with
|  Workplace   | NumberOfYears |
|  <workplace> |  <years>      |
And user clicks the 'Save' button
Then ...

Examples:
name|age
Sam|40
#note that there will be always 1 here.

Examples:
workplace|years
abc|2
xyz|3
pqr|4

我之所以要这样做,是因为我可以通过编程方式从外部电子表格生成示例 table 并动态更改它。是否有可能做到这一点?或者有什么问题吗?

你可以,但你必须重构你的例子table:

Given ...
When user fills 'Name-Box' with '<name>'
And user fills 'Age-Box' with '<age>'
And user fills experience with
    | Workplace     | NumberOfYears |
    | <workplace 1> | <years 1>     |
    | <workplace 2> | <years 2>     |
    | <workplace 3> | <years 3>     |
    | <workplace 4> | <years 4>     |
And user clicks the 'Save' button
Then ...

Examples:
    | name | age | workplace 1 | years 1 | workplace 2 | years 2 | workplace 3 | years 3 | workplace 4 | years 4 |
    | Sam  | 40  | abc         | 2       | xyz         | 3       | pqr         | 4       |             |         |
    | Sam  | 40  | aaa         | 3       | bbb         | 12      |             |         |             |         |
    | Sam  | 40  | nnn         | 2       | ooo         | 20      | ppp         | 1       | qqq         | 3       |

表单中的每一行在您的示例中需要 "workspace X" 和 "years X" 列 table。


STR 说:

But lets say we want to let the number of workplaces unlimited. Then, how can I define rows for a person with experience in 3 workplaces along with another person with experience in 15 workplaces. By having fixed number of columns, aren't we restricting the possibilities to test?

没错。如果您需要无限行数,您仍然可以重组测试,但我认为您不能使用场景大纲。

您可以使用:

And user fills experience with
    | Row | Workplace | Number of Years |
    | 1   | a         | 2               |
    | 2   | b         | 4               |
    | 3   | c         | 10              |
    | ... | ...       | ...             |
    | 15  | o         | 1               |

步骤定义分为两部分:

public class WorkExperienceRow
{
    public int RowNumber { get; set; }
    public string Workplace { get; set; }
    public int NumberOfYears { get; set; }
}

使用TechTalk.SpecFlow.Assist命名空间中的SpecFlow TableExtensions,上面的class可以用来表示SpecFlow中的每一行table.

using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Assist;

[Binding]
public class FooSteps
{
    [When(@"user fills experience with")]
    public void WhenUserFillsExperienceWith(Table table)
    {
        IEnumerable<WorkExperienceRow> experiences = table.CreateSet<WorkExperienceRow>();
        // Find the <table>

        foreach (var experience in experiences)
        {
            // Find the <tr> by index using experience.RowNumber
            // Find the text box in column #1
            // Fill in the text box with experience.Workplace
            // Find the text box in column #2
            // Fill in the text box with experience.NumberOfYears
        }
    }
}

如果您想在电子表格中定义 SpecFlow 示例集,可以使用 SpecFlow+ Excel 来完成。事实上,如果您愿意,您可以在 Excel 中定义整个功能文件,或者仅使用 Excel 中定义的示例扩展您的功能。您可以直接在 Excel 中进行更改,这些更改将反映在您的测试用例中。

如果您有兴趣了解更多信息,www.specflow 中有一个简短的介绍。org/plus/excel/getting-started/ Gaspar Nagy 给出了 presentation 在 CukeUp! 2014 年,包括一些示例和总体概述。

不过,我应该指出,与 SpecFlow 不同的是,SpecFlow+ 组件(SpecFlow+ Excel 和 SpecFlow+ Runner)需要购买许可证并且不是开源的。您可以免费评估 SpecFlow+ Excel,但如果您尚未注册许可证,则会生成标题为“SpecFlow+ Excel 评估模式”的额外场景。

注意: 抱歉无法点击 link,但我不能添加超过 2 links :(