Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Behavior Driven Development (BDD) (with Cucumber)

Behavior Driven Development (BDD) (with Cucumber)

Why normal testing is not sufficient and why you should consider BDD.

Presented at 'Agile Monday' in Nuremberg, May 2012

Klaus Kämpf

May 29, 2012
Tweet

Other Decks in Education

Transcript

  1. Behaviour Driven Development (BDD) User Story As a role In

    order to goal I want action Scenario Given context When event occurs Then ensure some outcome
  2. features/simple.feature Feature: Getting Cucumber up and running In Order to

    explore BDD As a developer I want to run Cucumber Scenario: A simple calculation Given two numbers "1" and "2" When I add both together Then I should get "3"
  3. Feature: Getting Cucumber up and running In Order to explore

    BDD As a developer I want to run Cucumber Scenario: A simple calculation # features/simple.feature:7 Given two numbers "1" and "2" # features/simple.feature:9 When I add both together # features/simple.feature:10 Then I should get "3" # features/simple.feature:11 1 scenario (1 undefined) 3 steps (3 undefined) 0m0.001s You can implement step definitions for undefined steps with these snippets: Given /^two numbers "([^"]*)" and "([^"]*)"$/ do |arg1, arg2| pending # express the regexp above with the code you wish you had end When /^I add both together$/ do pending # express the regexp above with the code you wish you had end Then /^I should get "([^"]*)"$/ do |arg1| pending # express the regexp above with the code you wish you had end If you want snippets in a different programming language, just make sure a file with the appropriate file extension exists where cucumber looks for step definitions.
  4. Feature: Getting Cucumber up and running In Order to explore

    BDD As a develper I want to run Cucumber Scenario: A simple calculation # features/simple.feature:7 Given two numbers "1" and "2" # features/step_definitions/calculator.rb:1 TODO (Cucumber::Pending) ./features/step_definitions/calculator.rb:2:in /^two numbers "([^"]*)" and "([^"]*)"$/' features/simple.feature:9:in Given two numbers "1" and "2"' When I add both together # features/step_definitions/calculator.rb:5 Then I should get "3" # features/step_definitions/calculator.rb:9 1 scenario (1 pending) 3 steps (2 skipped, 1 pending) 0m0.001s
  5. step_definitions/calculator.rb Given /^two numbers "([^"]*)" and "([^"]*)"$/ do |arg1, arg2|

    @num1 = arg1.to_i @num2 = arg2.to_i end When /^I add both together$/ do @result = @num1 + @num2 end Then /^I should get "([^"]*)"$/ do |arg1| raise unless @result == arg1.to_i end
  6. Feature: Getting Cucumber up and running In Order to explore

    BDD As a develper I want to run Cucumber Scenario: A simple calculation # features/simple.feature:7 Given two numbers "1" and "2" # features/step_definitions/calculator.rb:1 When I add both together # features/step_definitions/calculator.rb:6 Then I should get "3" # features/step_definitions/calculator.rb:10 1 scenario (1 passed) 3 steps (3 passed) 0m0.001s
  7. Capybara When /I sign in/ do within("#session") do fill_in 'Login',

    :with => '[email protected]' fill_in 'Password', :with => 'password' end click_link 'Sign in' end
  8. Selenium Selenium is a suite of tools to automate web

    browsers across many platforms. Poltergeist It allows you to run your Capybara tests on a headless WebKit browser.
  9. Further reading User stories http://www.mountaingoatsoftware.com/topics/user-stories BDD frameworks Ruby - Cucumber

    - http://cukes.info C - cbehave - http://code.google.com/p/cbehave Python - Lettuce - http://lettuce.it C# - SpecFlow - https://github.com/techtalk/SpecFlow PHP - Behat - http://behat.org Drivers (Cucumber) Capybara - https://github.com/jnicklas/capybara Selenium - http://seleniumhq.org Poltergeist - https://github.com/jonleighton/poltergeist