Data Tables - nripendra/xGherkin.net GitHub Wiki

Gherkin data tables

Gherkin supports providing data table as a part of given statement. This table can then be used as a data fixture for testing the scenario. If data table is provided as part of background, then fixture becomes shared across all scenarios under feature.

Example:

Given that following users exist:
  |username |password|
  -------------------
  |bob@123  |pass1   |
  |sam@123  |pass2   |

xGherkin.net too has support for data table. You need to call an overload of Do method that takes 'GherkinTable' as input.

Here's an example:

"Given that following users exist:".Do(new GherkinTable("username", "password")
{
  {"bob@123", "pass1"},
  {"sam@123", "pass2"}
}, (table) => 
{
  //Hey, now you can use the table to setup fixtures.
});

Note that it is not any different from creating a class field/property of GherkinTable instance in constructor or Background as far as c# language is concerned. But this table will be used as part of report being generated, so recommendation would be to pass table to given step.

[Feature("Data table can be a class field", "")]
public class DatatableFeature
{
  //Table should now be available to each scenario
  GherkinTable table;

  public void Background()
  {
    table = new GherkinTable("username", "password")
    {
      {"bob@123", "pass1"},
      {"sam@123", "pass2"}
    }; 

    //This statement is here simply for reports.
    "Given that following users exist:".Do(table, _ => {});
  }
  
  [Scenario("Table should be available here")]
  public void TableShouldBeAvailableHere()
  {
    "Given that feature has a GherkinTable field".Do(_ => {});
    "And table instance has been created in constructor of feature".Do(_ => {});
    "Then table should be available in this scenario".Do(_ =>
    {
       Assert.NotNull(table);
       Assert.True(table.Rows.Count > 0);
       Assert.Equal("bob@123", table.Rows[0][0] as string);
    });
  }
  
}