Scenario outline in SpecFlow

Scenario outlines are used to execute same steps but with different set of data.

Consider below feature file. Note that outlines are marked by Scenario Outline keyword in Gherkin. Test data is marked by Examples: keyword. Note that we have got 2 steps in below scenario. If we execute below scenario, same 2 steps will run with each set of data in examples.

Feature: Outlines in SpecFlow

@outline
Scenario Outline: Outline Scenario Example
	Given I multiply <x> and <y>
	Then result should be <z>

	Examples: 
	| x  | y    | z   |
	| 11 | 11   | 121 |
	| 0  | 1292 | 0   |

 

Here is the step definition class for above scenario.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using TechTalk.SpecFlow;

namespace SpecFlowRunnerProject
{
    [Binding]
    public class OutlinesInSpecFlowSteps
    {
        double result = 0;
        [Given(@"I multiply (.*) and (.*)")]
        public void GivenIMultiplyAnd(double p0, double p1)
        {
            result = p0 * p1;
            Console.WriteLine("Result is " + result);
        }
        
        [Then(@"result should be (.*)")]
        public void ThenResultShouldBe(int p0)
        {
            Assert.IsTrue(result == p0);
        }
    }
}

You may also like...