Tuesday 14 June 2016

SPECFLOW WITH VISUAL STUDIO

As software development becomes complicated, writing unit tests provides a protection against constant changes and modifications.
other alternatives to specflow:
you can also use other test engines, like MsTest, xUnit, MbUnit or NUnit.
SpecFlow tests are usually placed into one or more separate projects in project's solution.
SpecFlow aims to bridge the communication gap between domain experts and developers by binding business readable behaviour specifications.
to provide a pragmatic and frictionless approach.
specification is written using the Gherkin language.
Gherkin is a domain specific language that lets us describe how our applications should behave without having to explain implementation details.




Steps to work with visual studio:
1. create a class library project.

2. now goto tool-> update-> search specflow and install.

3. now goto tool -> nuget -> update -> search specflow and install.

4. after that right click on project -> add -> new item -> select sepcflow type feature
it's default name is "specflowfeature1steps" and then click ok(remember the color of the content for this feature file)

5. now right click on this feature to generate definition file.

6. now you will see the color of the feature file is changed. means definition file is generated which is regular c# file with binding attribute.

7. after right click on the feature you will pop up and then click generate.
which is by default select the address to save definition file.click to save.

8. Now its time to execute specflow test to do that click on test -> windows -> Test Explorer

9. Now open AppConfig and under specFlow tag write <unitTestProvider name="NUnit"/>
now save ,after that run tests if it gives "error for Nunit could not found" then goto nuget and install specflow.nunit package.


10. Now run test.



Syntax:
*Each Gherkin file begins with a Feature keyword. Feature defines the logical test functionality you will test in this feature file.
Feature: LogIn Action Test
Description: This feature will test a LogIn and LogOut functionality
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters UserName and Password
Then Message displayed Login Successfully


*Background keyword is used to define steps which are common to all the tests in the feature file.
Feature: Add to Cart
This feature will test functionality of adding different products to the User basket from different flow

Background: User is Logged In

Scenario: Search a product and add the first result/product to the User basket
Given User searched for Lenovo Laptop
When Add the first laptop that appears in the search result to the basket
Then User basket should display with 1 item


* Data Driven Testing, which allows to automatically run a test case multiple times with different input and validation values.
* SpecFlow inherently supports Data Driven Testing using Scenario Outline.
* Scenario Outline – This is used to run the same scenario for 2 or more different set of test data.



11. And now add new folder named as "features" and add sceflow feature as "LogIn_Feature" and copy this code
Feature: Login_Feature
In order to access my account
As a user of the website
I want to log into the website

@mytag
Scenario: Successful Login with Valid Credentials
Given User is at the Home Page
And Navigate to LogIn Page
When User enter UserName and Password
And Click on the LogIn button
Then Successful LogIN message should display

Scenario: Successful LogOut
When User LogOut from the Application
Then Successful LogOut message should display

and then generate definition file after that run test.

12. After that next target is to test or run the feature file and in order to test the feature file:
Now we add SpecFlow Step Definition file:
A Step Definition file is a small piece of code with a pattern attached to it or in other words a Step Definition is a C# method in a class with an annotation above it.




No comments:

Post a Comment