Search Here!

Saturday, June 23, 2012

BDD | Art of Writing Good Feature Files


Art of Writing Good Feature Files using Gherkin Keyword(s)

Clearly writing good feature files is important. However writing good feature files, as any good documentation, takes time, effort and collaboration.
Here’s  are the tips for what makes a good feature file:

·         About the business domain – not software design
·         In domain language
·         Specification – not a script
·         Easy to understand
·         Focused on a single thing
·         Precise and testable
·         Self-explanatory
·         Consistent
·         Easy to access


Gherkin Keyword and it’s Usage

  •   Tags

Tags are a great way to organize your features and scenarios. Consider this 

@Pegasus Release 3.11
Feature: User Creation
         In order to Create user
              As a Workspace admin
               I want to create a ws user
@sprint1
Scenario Outline: Login as Course Space Admin

  • Feature

Every .feature file conventionally consists of a single feature. A line starting with the keyword Feature followed by free indented text starts a feature. A feature usually contains a list of scenarios. You can write whatever you want up until the first scenario, which starts with the word Scenario.

Feature: WS Admin
       In order to Creation of Course and Enrolling the Students
       As a WS Admin
       I want to be create courses and users

·         Background

Background allows you to add some context to the scenarios in a single feature. A Background is much like a scenario containing a number of steps. The difference is when it is run. The background is run before each of your scenarios but after any of your Before Hooks.

Background:
 Given I have browsed url for "WSAdmin"
 And I logged in as Ws Admin

  •  Scenario

Every scenario consists of a list of steps, which must start with one of the keywords Given, When, Then, But or And. SpecFlow treats them all the same, but you shouldn’t.

Scenario: Create the new Course using create course link in right pane
       Given I have logged into the workspace as WSAdmin
       Then  It should be on "Course Enrollment" page
       When  I clicked on the "Create New Courses" link in "right" frame

  •   Scenario Outline

Copying and pasting scenarios to use different values quickly becomes tedious and repetitive. Scenario outlines allow us to more concisely express these examples through the use of a template with placeholders. The Scenario outline steps provide a template which is never directly run. A Scenario Outline is run once for each row beneath it (not counting the first row). This scenario is a data driven scenario, it runs the scenario for every iteration in your examples table.

Scenario Outline: Login
       When I enter in the username field
       And I enter in the password field
       And I press Login
       Then the response Page is Course Enrollment
       Examples:
              | username     |password |
|bdd_ws_admin1 |password2|
|bdd_ws_admin2 |password2|
|bdd_ws_admin3 |password2|


You can also use placeholders in Multiline Step Arguments, Multiline step arguments come in two flavours – tables or strings.

Tables:

Tables as arguments to steps are handy for specifying a larger data set - usually as input to a Given or as expected output from a Then.

When I click the Create New User link
Then Enter the user details in Create New User popup  
        | Field          | Value                |
        | txtPassword    | password1            |
        | txtFirstName   | QA                   |
        | txtLastName    | BDD                  |
        | txtEmail       | bdd@gmail.com        |
When I click the Save button


String:

Multiline Strings (also known as PyStrings) are handy for specifying a larger piece of text. This is done using the so-called PyString syntax. The text should be offset by delimiters consisting of three double-quote marks (""") on lines by themselves:

Scenario: Fill in The Blank Question Creation
  Given a question named "Random" with:
    """
    Some Title, Eh?
    ===============
    Here is the first paragraph of my blog post.
    Lorem ipsum dolor sit amet, consectetur adipiscing
    elit.
    """

  •   Examples

The placeholders indicate that when the Examples row is run they should be substituted with real values from the Examples table. If a placeholder name is the same as a column title in the Examples table then this is the value that will replace it.

Examples:
              | username     |password |
|bdd_ws_admin1 |password2|
|bdd_ws_admin2 |password2|
|bdd_ws_admin3 |password2|

  •   Given

The purpose of Given steps is to put the system in a known state before the user (or external system) starts interacting with the system (in the When steps). Avoid talking about user interaction in givens. If you have worked with use cases, givens are your preconditions.

Scenario: Copy courses       
        Given MasterLibrary Course is present after do search

  •  When 

The purpose of When steps is to describe the key action the user performs (or, the state transition).

Scenario:Create the new Course using create course link in right pane
       Given I have logged into the workspace as WSAdmin
       Then  It should be on "Course Enrollment" page
       When  I clicked on the "Create New Courses" link in "right" frame   
       Then  I should see the "Create New Course" popup

  •  Then

The purpose of Then steps is to observe outcomes. The observations should be related to the business value/benefit in your feature description. The observations should inspect the output of the system (a report, user interface, message, command output) and not something deeply buried inside it (that has no business value and is instead part of the implementation).

When I click the Save button
Then It should display successful message New user created successfully

  •   And, But


You can use And or But steps, allowing your Scenario to read more fluently. SpecFlow interprets steps beginning with And or But exactly the same as all other steps. It doesn’t differ between them - you should!

Scenario:Create the new Course using create course link in right pane
       Given I have logged into the workspace as WSAdmin
       Then  It should be on "Course Enrollment" page
       When  I clicked on the "Create New Courses" link in "right" frame   
       Then  I should see the "Create New Course" popup
       When  I enter the course name with prefix "ML"
       And   I clicked on the Save button of 'Create New Course' popup
       Then  It should display successful message "New course created successfully."
       But   I don’t see the "Create New Courses" link in "right" frame

  •   Comments


You can write comments for each Feature(s), Scenarios or for Steps using keyword #

# This Scenario is to Verify Course Creation
Scenario:Create the new Course using create course link in right pane

       Given I have logged into the workspace as WSAdmin
       Then  It should be on "Course Enrollment" page

  •  *

 The idea is that the indented bullets would signify a shorthand for repeating the introductory part of the step text (in this case "Then I should see").

When I enter in the username field
And I enter in the password field
And I press Login
Then I should see:
                * that there is Logout link
                * that there is MyProfile link
                * that there is Customer Support link
       Examples:
       |username     |password|
       |sel_ws_admin|password2| 

No comments:

Post a Comment