Read data from excel and write to feature file using specflow

Tech picture Tech · Feb 21, 2015 · Viewed 8.5k times · Source

I am currently using specflow for my unit testing, however my complexity is in the fact that I want to read(retrieve) my input data from an excel spread sheet, before running my tests.

For example:

When I get a customers product in XXXXX with product number 1234567

Then The following product details are returned

| Model    | Product Type | SerialNumber |
| blah     | zzzzz        | 12345        |   

So I want to basically be able to change the input data by reading from execel file and writing to my feature file. Has this been done before and is it possible? Do I need to install some sort of plugin?

Answer

chr99ha picture chr99ha · Feb 21, 2015

You can implement your own step definition that reads up the data from an Excel sheet. The simplest way to do this is passing the Excel sheet name as a step parameter, and then implementing the code to read up the data from Excel. To learn more about writing step definitions, start here: http://www.specflow.org/documentation/Step-Definitions/

Alternatively, you can just install SpecFlow+Excel which provides that out of the box for you in many different ways (e.g. just linking Excel files to Gherkin scenarios that are written in plain text feature files, or writing and executing the entire Gherkin scenario in Excel). SpecFlow+Excel also supports specifying just the scenario outline example rows in (different) Excel files, which is not just reading up data from Excel in step definition, but also influencing the scenario generation and execution with Excel. More info about SpecFlow+Excel can be found here: http://www.specflow.org/plus/Excel/

Disclaimer: SpecFlow+Excel is a commercial plugin that builds on SpecFlow, maintained by the SpecFlow team. You can try it out free of charge.