Writing step definitions for a SpecFlow feature file

Now let us see how to write the step definitions for a feature file in SpecFlow.

Just right click on the feature file and then select “Generate Step Definitions” option as shown in below image.

Generate SpecFlow Step definitions

Generate SpecFlow Step definitions

Then below window pops up. In this window, you can select which step definitions you want to generate. You can also provide the name of step definition class.

Generate Step Definition Skeleton - SpecFlow

Generate Step Definition Skeleton – SpecFlow

Here is the sample step definition class file.

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

namespace Specflow
{
    
    [Binding]
    public class SimpleFeatureFileSteps
    {
        double balance;
        [Given(@"I have \$(.*) in my account")]
        public void GivenIHaveInMyAccount(int initialBalance)
        {
            balance = initialBalance;
        }
        
        [When(@"I withdraw \$(.*) from ATM")]
        public void WhenIWithdrawFromATM(int withdrawAmt)
        {
            balance -= withdrawAmt;   
        }
        
        [Then(@"my balance should be \$(.*)")]
        public void ThenMyBalanceShouldBe(int remaining)
        {
            Assert.IsTrue(balance == remaining,"Balance Verification");
        }
    }
}

You may also like...