If you have just started cuking, you will find the below post helpful.

What is cucumber (in context to software testing)?

Cucumber is a ruby based tool for tomated testing framework development. It is different than other testing tools as it lets you execute plain-text functional descriptions (features) as automated tests. This tools bridges the technical gap between business analysts, developers and QA developers by allowing them to execute the user story requirements as test scenarios in plan English. This type of testing/development is also known as Behavior Driven Development (BDD).

Over the past few years, Cucumber has been one of my favorite tools for developing BDD based automated testing frameworks and test suites. I have used this tool for testing back-end as well front-end of applications.

Here is couple of tips and tricks that I use everyday while cuking:

  1. Use ‘–expand’ to expand Scenario outline tables in Cucumber output: The default cucumber option is to not expand the scenario outline table in the reporting. So when you run bundle exec features, it will print out the report like the one below:
 Scenario Outline: Multiply two numbers  
   Given I have entered "<num_1>" into the calculator
   And I press the multiply button  
   And I have entered "<num_2>" into the calculator
   And I press the equal button
   Then the result should be "<result>" on the screen
  Examples:
    |num_1 | num_2 | result |
    | 1    |   1   |   1    |  
    | 2    |   2   |   4    | 
    | 3    |   5   |   15   |

Use ‘–expand’ options to expand the scenario outline for better verbose reporting.

Here is the result of running bundle exec –expand cucumber features:

   Scenario Outline: Multiply two numbers                   
   Given I have entered "<num_1>" into the calculator
   And I press the multiply button                       
   And I have entered "<num_2>" into the calculator 
   And I press the equal button                     
   Then the result should be "" on the screen

   Examples:

     Scenario: | 1 | 1 | 1 |                   
       Given I have entered "1" into the calculator
       And I press the add button                 
       And I have entered "1" into the calculator 
       And I press the equal button               
       Then the result should be "1" on the screen

     Scenario: | 2 | 2 | 4 |                   
       Given I have entered "2" into the calculator
       And I press the add button                 
       And I have entered "2" into the calculator 
       And I press the equal button               
       Then the result should be "4" on the screen

     Scenario: | 3 | 5 | 15 |                   
       Given I have entered "3" into the calculator
       And I press the add button                 
       And I have entered "5" into the calculator 
       And I press the equal button               
       Then the result should be "15" on the screen

2. Regular expression cheat sheet for cucumber – Regular expressions are the key to Cucumber’s flexibility. Well-crafted regular expressions let you reuse step definitions, avoiding duplication and keeping your tests maintainable.

  1. Anchors: The regular expression I’m logged in matches I’m logged in and I’m logged in as an admin. To avoid ambiguous matches, use ^I’m logged in$. The caret at the beginning anchors to the beginning of the string. The dollar at the end does the same with the end of the string. Use these with all your step definitions and you won’t have surprise matches.
  2. Wildcards and quantifiers:
.* matches anything (or nothing), literally “any character (except a newline) 0 or more times”
.+ matches at least one of anything
[0-9]* or d* matches a series of digits (or nothing)
[0-9]+ or d+ matches one or more digits
“[^”]*” matches something (or nothing) in double quotes
an? matches a or an (the question mark makes the n optional)

3. Use Tags: TAGGING SCENARIOS

Another handy Cucumber feature is the ability to tag scenarios. Tagging a scenario is achieved by simply adding @tagname above the scenario definition, as such:

 Scenario: User is not signed up   
Given no user exists with an email of "email@person.com"  
When I go to the sign in page  
And I sign in as "email@person.com/password"  
Then I should see "Bad email or password" 
And I should not be signed in

To run scenarios with a specific tag:

cucumber --tags@tagname

To exclude scenarios with a certain tag, prefix the tag with a tilde:

cucumber --tags ~@wip

A common tag name that Cucumber supports out of the box is @wip (work in progress) and provides rake tasks to accommodate using this pattern:

rake cucumber:wip # runs only scenarios with a wip tag
rake cucumber:ok # runs all scenarios without a wip tag
Advertisements