Having Tables in Example Table in SpecFlow

2019-07-11 18:07发布

I am using SpecFlow with VisualStudio 2013 to test a particular web form which has the following layout
******************** Start: Form ******************
Name:________________
Age:_________________
Experience:
Work place | Number of years |
___________ | _________________ |
___________ | _________________ |
___________ | _________________ |
___________ | _________________ |
******************** End: Form ******************

How do I write it so that I can feed from Example?

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

The reason why I want to do it this way, is so that I can generate the example table from an external spreadsheet programmatically and change it dynamically. Is it possible to do this? or is there anything wrong?

2条回答
ら.Afraid
2楼-- · 2019-07-11 18:32

You can, but you'll have to restucture your example 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       |

Each row in the form needs a "workspace X" and "years X" column in your examples table.


STR said:

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?

That is true. If you need an unlimited number of rows, you can still restructure your test, but I don't think you can use a Scenario Outline.

You could use:

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

The step definition would be two parts:

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

Using the SpecFlow TableExtensions in the TechTalk.SpecFlow.Assist namespace, the class above can be used to represent each row in the 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
        }
    }
}
查看更多
再贱就再见
3楼-- · 2019-07-11 18:47

If you want to define your SpecFlow example sets in a spreadsheet, you can do this using SpecFlow+ Excel. In fact you can define your entire feature files in Excel if you want to, or just extend your features with examples defined in Excel. You can make changes in Excel directly and those changes will be reflected in your test cases.

If you're interested in finding out more, there's a brief introduction at www.specflow.org/plus/excel/getting-started/ and Gaspar Nagy gave a presentation at CukeUp! 2014 that includes some examples as well as a general overview.

I should however point out that unlike SpecFlow, the SpecFlow+ components (SpecFlow+ Excel and SpecFlow+ Runner) require a license to be purchased and are not open source. You can evaluate SpecFlow+ Excel for free, but an extra scenario is generated with title “SpecFlow+ Excel Evaluation Mode” if you have not registered a license.

Note: sorry for the unclickable link, but I can't add more than 2 links :(

查看更多
登录 后发表回答