Installation and Setup Steps-->
1.Install SpecFlow for Visual Studio (Open Extensions
and Updates)
2. Install
SpecFlow NuGet Packages
3. Add Feature File
My Demo Project
Here, I have worked on following story
Story: Discounts on online shopping cart price for
loyalty card customers
In order to increase the reward for loyal customers
I want to add 5-10% discounts on the final shopping
cart price.
Scenario 1: 5% discount for loyalty card customer
Given a loyalty card customer in online checkout page
When the shopping card total is less than $100
Then 5% discount should be applied to the final
shopping cart price.
In the feature file, I described what the feature
needs to do Every scenario represents a different test or use case of the
feature.
Generate Bindings
When the steps are displayed in purple, this means that we are not
ready. To be able to execute the scenario you need to define the bindings for each step. We need to create step
definitions that bind the statements in the test scenario to the application
code. Once you click insider the specification file and open the context menu,
you will find the 'Generate Step Definitions' item.
The following file will be generated.
Here is how the binding class will look like.
namespace SpecflowDemo
{ [Binding] public class TestDiscountSteps { decimal discountedPrice = 0; [Given(@"a loyalty card customer in online checkout page")] public void GivenALoyaltyCardCustomerInOnlineCheckoutPage() { } [When(@"the shopping card total is less than \$(.*)")] public void WhenTheShoppingCardTotalIsLessThan(int p0) { ShoppingCartDiscountCalculator cartDiscountCalculator = new ShoppingCartDiscountCalculator(); discountedPrice = cartDiscountCalculator.GetPriceforLoyalCustomer(LoyalityCardType.Gold, 500); } [Then(@"(.*)% discount should be applied to the final shopping cart price\.")] public void ThenDiscountShouldBeAppliedToTheFinalShoppingCartPrice_(int p0) { Assert.AreEqual(450, discountedPrice); } } }
Execute SpecFlow Tests
Once we build the project, our test/scenario will show
up in the tests explorer.
Test Outcome
The generated output from the test execution is nice.
You can observe each executed step and its execution time.
Comments
Post a Comment