Cucumber BDD

30
Behavior Driven Development Pravin D’silva MCA Goa University

description

Everything you want to know about BDD along with a step by step example :)

Transcript of Cucumber BDD

Page 1: Cucumber BDD

Behavior Driven Development

Pravin D’silvaMCAGoa

University

Page 2: Cucumber BDD

AGENDA

Introduction TDD BDD Cucumber Gherkin Step Definitions Demo (Ruby) How to extend

Page 3: Cucumber BDD

INTRODUCTION

People have a lot of ideas in mind. The key to convert ideas to software:

COMMUNICATION Various stakeholders involved in completing

the task

Page 4: Cucumber BDD

WRONG PERCEPTION

Page 5: Cucumber BDD

TEST DRIVEN DEVELOPMENT

Automated Test Cases Produce minimum code to pass test Refactor code to acceptable standards

Page 6: Cucumber BDD

TDD

Advantages: Programmers tend to

be more productive Can drive the design

of a program Offers the ability to

take small steps Can lead to more

modularized, flexible, and extensible code

 Shortcomings Reliance on unit tests

might not perform sufficient full functional testing

Tests may share the same blind spots with the code

Tests become part of the maintenance overhead of a project

Rewrite the tests when requirements change

Page 7: Cucumber BDD

WHAT IS BDD?

“BDD builds upon TDD by formalizing the good habits of the best TDD practitioners.”

- Matt Wynne

 So what are those good habits? Working outside-in, starting from a business

or organizational goal Using examples to clarify requirements Developing and using a ubiquitous language

Page 8: Cucumber BDD

USE OF EXAMPLES Take care to write the acceptance tests as

examples that anyone on the team can read Get feedback from the business stakeholders Ensure the acceptance tests can easily be

read and written by anyone on the team Easier to validate Readable by computer

TRUE VALUE OF ACCEPTANCE TESTS communication collaboration tool

Page 9: Cucumber BDD

In BDDAcceptance Tests

areEXECUTABLE

SPECIFICATIONS

Page 10: Cucumber BDD

CUCUMBER TESTING STACK

Gherkin: 1. Specifications from plain-language text files called features.2. Each scenario is a list of steps for Cucumber to work through

Step Definitions:Map the business-readable language of each step into Ruby code to carry outwhatever action is being described by the step.

Automation library:One or two lines of Ruby that delegateto a library of support code, specific to the domain of your application.

Page 11: Cucumber BDD

CUCUMBER TESTING STACK

Page 12: Cucumber BDD

GHERKIN

Cucumber tests are expressed using a syntax called Gherkin.

Gherkin files: plain text and have a .feature extension

Use of Concrete Examples Lets take a scenario

Customers should be prevented from entering invalid credit card details.

Page 13: Cucumber BDD

PROBLEMSCustomers should be prevented from entering

invalid credit card details.

Ambiguity and Misunderstanding Lacks precision Worthy but vague

Lets take another scenarioIf a customer enters a credit card number that isn’t exactly 16 digits long, when they try to submit the form, it should be redisplayed with an error message advising them of the correct number of digits.

Page 14: Cucumber BDD

FEATURE Describe the features that a user will be able to enjoy when

using a programFeature: Feedback when entering invalid credit card details.In user testing we've seen a lot of people who made mistakes

entering their credit card. We need to be as helpful as possible here to avoid losing users at this crucial stage of the transaction.

Background: Given I have chosen some items to buy And I am about to enter my credit card details

Scenario: Credit card number too short When I enter a card number that's only 15 digits long And all the other details are correct And I submit the formThen the form should be redisplayed And I should see a

message advising me of the correct number of digits

Page 15: Cucumber BDD

SCENARIO

Each scenario is a single concrete example of how the system should behave in a particular situation

Scenarios all follow the same pattern: 1. Get the system into a particular state. 2. Poke it (or tickle it, or ...). 3. Examine the new state.

Start with a context, go on to describe an action, and then finally check

that the outcome was what we expected. Each scenario tells a little story describing something that the system should be able to do.

Page 16: Cucumber BDD

SUPPORT FOR VARIOUS LANGUAGES

Gherkin support in Hindi"native": "हिं��दी�","feature": "रूप लेख"    

"background": "प�ष्ठभू�मि�"

"scenario": "परि�दृश्य"  "scenario_outline": "परि�दृश्य रूप�ख�"  "examples": "उदी���ण"  

"given": "*|अग�|यदिदी|चूं��कि "  

"when": "*|जब“"then": "*|तब" "and": "*|औ�|तथा�"  "but": "*|प�"

Norwegian:Egenskap: SummeringFor å unngå at firmaet går konkursMå regnskapsførerere bruke en regnemaskin for å legge sammen tallScenario: to tallGitt at jeg har tastet inn 5Og at jeg har tastet inn 7Når jeg summererSå skal resultatet være 12

Page 17: Cucumber BDD

STEP LANGUAGES

Translates from plain language into Ruby

On the inside it tells your system what to do using Ruby automation code.

Ruby has an incredibly rich set of libraries for automating a whole variety of systems, from JavaScript-heavy web applications to REST web services.

Page 18: Cucumber BDD

FEATURE TO STEP DEFINITION

Feature:

Given I have $100 in my account

Regular Expression: /I have \$100 in my Account/

Page 19: Cucumber BDD

KEYWORDS

Feature Background Scenario Given When Then And But * Scenario Outline Examples

Page 20: Cucumber BDD

DEMO EXAMPLE

Page 21: Cucumber BDD

AFTER CREATING THE STEP DEFINITIONS FILE

The scenario has graduated from undefined to pending.

Page 22: Cucumber BDD

Given /^the input "([^"]*)"$/ do |input|

@input = input

end

Page 23: Cucumber BDD

When /^the calculator is run$/ do

@output = `ruby calc.rb #{@input}`

raise('Command failed!') unless $?.success?

end

Calc.rb is not found

Page 24: Cucumber BDD

After calc.rb is created (still blank)

Page 25: Cucumber BDD

print eval(ARGV[0]) in calc.rb

Then /^the output should be "([^"]*)"$/ do |expected_output|

@output=expected_output

end

Page 26: Cucumber BDD

ADDING ANOTHER SCENARIO

Scenario Outline: Add two numbersGiven the input "<input>"When the calculator is runThen the output should be "<output>"

Examples:| input | output || 2+2 | 4 || 98+1 | 99 |

Page 27: Cucumber BDD

ALL TESTS PASSED

Page 28: Cucumber BDD

ADDITIONAL USES OF CUCUMBER Databases Testing REST web services Capybara to Test Ajax Web Applications Aruba

Page 29: Cucumber BDD

USE CUCUMBER WITH JVM Install Maven Install m2e plugins for Eclipse Add Cucumber dependencies in pom.xml Run Maven builds and test using Cucumber