BDD with
Ruby
Gherkin
Cucumber
Capybara
Gabi Kis
TDT - July 3, 2013
Agenda
• TDD
• BDD
• Gherkin
• Cucumber
• Capybara
• Demo (Ruby)
• Conclusions
What is TDD?
• Test-driven
development
(TDD)
o developer writes an
automated test case
o then produces the
minimum code to pass
test
o refactors the code to
acceptable standards
TDD
• Advantages:
o Programmers tend to be
more productive
o Can drive the design of a
program
o Offers the ability to take
small steps
o Can lead to more
modularized, flexible, and
extensible code
• Shortcomings
o Reliance on unit tests might
=> not perform sufficient full
functional testing
o Tests may share the same
blind spots with the code
o Tests become part of the
maintenance overhead of a
project
o Rewrite the tests when
requirements change
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?
o Working outside-in, starting from a business or organizational goal
o Using examples to clarify requirements
o Developing and using a ubiquitous language
BDD - Gherkin
Story card
Acceptance criteria
User story
As a <role>
I want to <feature>
So that <benefit>
Scenario
Given <initial context>
When <event occurs>
Then <ensure some outcomes>
Example:
Feature: Customer withdraws cash
As a customer,
I want to withdraw cash from an ATM,
So that I don’t have to wait in line at the bank
Scenario: Account is in credit
Given the account is in credit
And the card is valid
And the dispenser contains cash
When the customer requests cash
Then ensure the account is debited
And ensure cash is dispensed
And ensure the card is returned
Scenario: Account is overdrawn past overdraft limit
Given the account is overdrawn
And the card is valid
When the customer requests cash
Then ensure a rejection message is displayed
And ensure cash is not dispensed
And ensure the card is returned
Gherkin exercise
Feature: [ ]
In order to [ ]
As a [ ]
I want to [ ]
Scenario: [ ]
Given [ ]
When [ ]
Then [ ]
What is Gherkin?
The language I use for writing Cucumber features!
What is Cucumber?
• is a tool for running automated acceptance tests
written in a BDD style
• is written in the Ruby programming language
• projects are available for other platforms beyond
Ruby
Cucumber implementation
/features/adding.feature
Feature: Adding
Scenario: Add two numbers
Given the input "2+2“
When the calculator is run
Then the output should be "4"
/step_definitions/calculator_steps.rb
Given /^the input "([^"]*)"$/ do |input|
@input = input
end
When /^the calculator is run$/ do
@output = `ruby calc.rb #{@input}`
raise('Command failed!') unless $?.success?
end
Then /^the output should be "([^"]*)"$/ do |exp_out|
@output.should == exp_out
end
Cucumber testing stack
Capybara
• provides an API for accessing web pages and
interacting with them in a way that is very similar
to how a real user would
• allows you to plug in different web drivers
– including selenium
• it can use real browsers: Firefox, IE or Chrome,
or a number of different browser simulators
Capybara coding
Demo!!!
Create a project from scratch using the tech
stack from this presentation.
Why use Cucumber?
• It is relatively easy to set up.
• It supports multiple report formats, including HTML, JUNIT, PDF.
• It supports writing specifications in about 40 spoken languages
Test scenarios can be read by the stakeholders
• It allows scripting, abstraction and component reuse on several levels
• Although Cucumber is a Ruby tool, it can be used on other languages as .NET or JVM languages.
• It's integrated with all the most popular web testing libraries.
• It allows you to cross reference and index tests using tags, so that you can quickly find all tests
related to a function or run a group of related tests (e.g. quick tests, slow tests, integration tests,
accounting tests).
• It allows you to quickly write and execute a scenario using tabled data
Final Questions
Thank you!
Gabi Kis
Skype: gabikissv
Email: gabi.kis@evozon.com

Behavior Driven Development - TdT@Cluj #15

  • 1.
  • 2.
    Agenda • TDD • BDD •Gherkin • Cucumber • Capybara • Demo (Ruby) • Conclusions
  • 3.
    What is TDD? •Test-driven development (TDD) o developer writes an automated test case o then produces the minimum code to pass test o refactors the code to acceptable standards
  • 4.
    TDD • Advantages: o Programmerstend to be more productive o Can drive the design of a program o Offers the ability to take small steps o Can lead to more modularized, flexible, and extensible code • Shortcomings o Reliance on unit tests might => not perform sufficient full functional testing o Tests may share the same blind spots with the code o Tests become part of the maintenance overhead of a project o Rewrite the tests when requirements change
  • 5.
    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? o Working outside-in, starting from a business or organizational goal o Using examples to clarify requirements o Developing and using a ubiquitous language
  • 6.
    BDD - Gherkin Storycard Acceptance criteria User story As a <role> I want to <feature> So that <benefit> Scenario Given <initial context> When <event occurs> Then <ensure some outcomes> Example: Feature: Customer withdraws cash As a customer, I want to withdraw cash from an ATM, So that I don’t have to wait in line at the bank Scenario: Account is in credit Given the account is in credit And the card is valid And the dispenser contains cash When the customer requests cash Then ensure the account is debited And ensure cash is dispensed And ensure the card is returned Scenario: Account is overdrawn past overdraft limit Given the account is overdrawn And the card is valid When the customer requests cash Then ensure a rejection message is displayed And ensure cash is not dispensed And ensure the card is returned
  • 7.
    Gherkin exercise Feature: [] In order to [ ] As a [ ] I want to [ ] Scenario: [ ] Given [ ] When [ ] Then [ ]
  • 8.
    What is Gherkin? Thelanguage I use for writing Cucumber features!
  • 9.
    What is Cucumber? •is a tool for running automated acceptance tests written in a BDD style • is written in the Ruby programming language • projects are available for other platforms beyond Ruby
  • 10.
    Cucumber implementation /features/adding.feature Feature: Adding Scenario:Add two numbers Given the input "2+2“ When the calculator is run Then the output should be "4" /step_definitions/calculator_steps.rb Given /^the input "([^"]*)"$/ do |input| @input = input end When /^the calculator is run$/ do @output = `ruby calc.rb #{@input}` raise('Command failed!') unless $?.success? end Then /^the output should be "([^"]*)"$/ do |exp_out| @output.should == exp_out end
  • 11.
  • 12.
    Capybara • provides anAPI for accessing web pages and interacting with them in a way that is very similar to how a real user would • allows you to plug in different web drivers – including selenium • it can use real browsers: Firefox, IE or Chrome, or a number of different browser simulators
  • 13.
  • 14.
    Demo!!! Create a projectfrom scratch using the tech stack from this presentation.
  • 15.
    Why use Cucumber? •It is relatively easy to set up. • It supports multiple report formats, including HTML, JUNIT, PDF. • It supports writing specifications in about 40 spoken languages Test scenarios can be read by the stakeholders • It allows scripting, abstraction and component reuse on several levels • Although Cucumber is a Ruby tool, it can be used on other languages as .NET or JVM languages. • It's integrated with all the most popular web testing libraries. • It allows you to cross reference and index tests using tags, so that you can quickly find all tests related to a function or run a group of related tests (e.g. quick tests, slow tests, integration tests, accounting tests). • It allows you to quickly write and execute a scenario using tabled data
  • 16.
  • 17.
    Thank you! Gabi Kis Skype:gabikissv Email: gabi.kis@evozon.com

Editor's Notes

  • #4 TDD is a style of programming where you write your tests before you write your code. This helps produce programs that are a bit more error free.
  • #6 ubiquitous =existingorbeingeverywhere,especiallyatthesametime;omnipresent
  • #7 Method names are sentences
  • #14 DSL – Domain Specific Language