SpecFlow - Data Driven Testing with Examples



We can perform data driven testing with the help of keyword Examples. We shall also take the help of keyword Scenario Outline to execute the same Scenario over multiple values.

The data sets to be taken into consideration shall be passed below the Examples section one after another separated by | symbol. So, if there are three rows, we shall have three test cases executed from a Single scenario.

Scenario Outline is used to replicate the same Scenario with a different data set. Writing the same tests with different values is cumbersome and time taking. For instance,

User Credential

We can club the above two scenarios with the Scenario Outline.

User Credentials

Thus, we see that a Scenario Outline should be accompanied with keyword Examples. A Scenario Outline is executed once for each of the rows appearing below the Examples segment.

Also, we have seen that the Given step has the <> delimiter. It points to the header of the Examples table. SpecFlow shall put the values within this table prior to the task of matching a step with a Step Definition.

To verify a Login module, we require the below steps to be executed −

  • User types the username and password.

  • Verify users should be able to login.

We shall incorporate the above steps to the Feature File.

Step 1: Create a Feature File

The details of how to create a Feature File is discussed in detail in the Chapter − Feature File.

Feature: User credential
Scenario Outline: Login module
   Given user types <username> and <password>
   Then user should be able to login
   Examples:
   | username       | password |
   | tutorialspoint1| pwd      |
   | tutorialspoint2| pwd1     |

Step 2: Step Definition File

The details of how to create a Step Definition File is discussed in detail in the Chapter – Step Definition File.

using System;
using TechTalk.SpecFlow;
namespace SpecFlowProject1.Features{
   [Binding]
   public class UserCredentialSteps{
      //regular expression used to point to data
      [Given(@"user types (.*) and (.*)")]
      public void GivenUserTypesUserAndPwds(string username, string password){   
         Console.WriteLine(username);
         Console.WriteLine(password);
      }  
      [Then(@"user should be able to login")]
      public void ThenUserShouldBeAbleToLogin(){
         Console.WriteLine("User should be able to login");
      }
   }
}

Step 3: Execution & Results

Select User credential(2), then click on Run All Tests in View.

Executions Result

Select Login module, tutorialspoint1 Scenario, then click on Open additional output for this result link.

Executions Scenario

Executions Scenarios

The Scenario got executed with username − tutorialspoint1 and password − pwd as specified in Examples(1st row).

Select Login module, tutorialspoint2 scenario, then click on Open additional output for this result link.

Login Module

Login Modules

The test got executed with username − tutorialspoint2 and password − pwd1 as specified in Examples(2nd row).

Advertisements