utside
in


     Development
     Cucumber                    Rspec
       Joseph Wilk / http://blog.josephwilk.net
Why are you here?
Why are you here?
In order to ...
Why are you here?
In order to ...
As a Scotland on Rails attendee
Why are you here?
In order to ...
As a Scotland on Rails attendee
I want ...
What is your
Acceptance Criteria?
Scenario: Happy SoR attendees
 Given people turned up
 When Joseph talks
 Then everyone should acquire some new knowledge
...
Pass
 Fail
Outside-in
     User
   Browser
    Views

  Controllers
    Models
Cucumber
“The feature
you wished you
had”               Views

                 Controllers
                              ...
Cucumber
“The feature
you wished you
had”               Views

                 Controllers
                              ...
Cucumber
“The feature
you wished you
had”               Views

                 Controllers
                              ...
Cucumber / Scenario
Cucumber / Scenario
Cucumber / Scenario




                      Rspec / code
                       examples
Cucumber / Scenario




                      Rspec / code
                       examples
Cucumber / Scenario




                      Rspec / code
                       examples
Cucumber / Scenario




                      Rspec / code
                       examples
Cucumber / Scenario




                      Rspec / code
                       examples
:rspec => route finder
 :cucumber => destination


London               Edinburgh
Behaviour Driven Development
with elegance and joy
Behaviour Driven Development
with elegance and joy
                               Just Cuke it!
Plaintext        Ruby
Features    step definitions
Cucumber Feature
                             Not
                           executed
Feature: Be awesome
 Narrative
Scena...
regexp match
  Step
definition
             Given /^we like (.*)$/i do |fruit|
               fruit.should == 'cukes'
Ruby ...
Plug and Pray
                                                  Rspec
          Plaintext              Ruby tests
        ...
Cucumber English
огурец    Russian               Chinese
concombre French                Korean
                       gurka    Swedish
   ...
огурец    Russian               Chinese
concombre French                Korean
                       gurka    Swedish
   ...
Webrat
 Love
visit home_path
click_link quot;Webquot;
fill_in quot;Emailquot;, :with => quot;Rat”
click_button quot;Lovequo...
Return On Investment
 Cucumber Feature
Return On Investment
 Cucumber Feature
     • Token Conversation
Return On Investment
 Cucumber Feature
     • Token Conversation
     • Acceptance Criteria
Return On Investment
 Cucumber Feature
     • Token Conversation
     • Acceptance Criteria
     • Design
Return On Investment
 Cucumber Feature
     • Token Conversation
     • Acceptance Criteria
     • Design
     • Documenta...
Return On Investment
 Cucumber Feature
     • Token Conversation
     • Acceptance Criteria
     • Design
     • Documenta...
Return On Investment
 Cucumber Feature
     • Token Conversation
     • Acceptance Criteria
     • Design
     • Documenta...
Token for Conversation
Feature Request
I want members to be able to
rent a movie with a priority
indicating how much they
want to see the film
Show me the _ _ _ _ _
Show me the VA L U _
            ____E
Root Cause Analysis
Root Cause Analysis




Why?     So we can maximise allocation of
         films
Root Cause Analysis


Why?     Keep customers happy


Why?     So we can maximise allocation of
         films
Root Cause Analysis

Why?     So they continue with their
         subscriptions


Why?     Keep customers happy


Why?   ...
Root Cause Analysis

Why?     So they continue with their
         subscriptions


Why?     Keep customers happy


Why?   ...
Lets write our
Cucumber feature
Feature (terrible)
                                                            Value

  Feature: select a movie to rent wi...
Feature (terrible)
                                                            Value

  Feature: select a movie to rent wi...
Feature (terrible)
                                                            Value

  Feature: select a movie to rent wi...
Feature (terrible)

Feature: select a movie to rent with priority
  In order to maximise allocation of films
  The website...
Feature (terrible)

Feature: select a movie to rent with priority
  In order to maximise allocation of films
  The website...
Feature (terrible)

 Feature: select a movie to rent with priority
   In order to maximise allocation of films
   The webs...
Feature (terrible)

 Feature: select a movie to rent with priority
   In order to maximise allocation of films
   The webs...
Feature (terrible)
                                                Role can change
                                       ...
Feature (terrible)
                                                Role can change
                                       ...
Nice
            Feature (terrible)

         Film Member selects a movie to rent with priority
Feature: select a movie to...
Acceptance
Definition of Done.
Scenarios
Feature: Film Member selects a movie to rent with priority
  In order to maximise allocation of films
  The Film...
Scenarios
Feature: Film Member selects a movie to rent with priority
  In order to maximise allocation of films
  The Film...
Scenarios
Feature: Film Member selects a movie to rent with priority
  In order to maximise allocation of films
  The Film...
Scenarios
Feature: Film Member selects a movie to rent with priority
  In order to maximise allocation of films
  The Film...
Let there be Cuking
Demo

             High Resolution:
http://www.screencast.com/t/aG1sE4N3Zq

            Low Resolution:
    http://www.vim...
Here’s one I
Cuked earlier
Demo

          High Resolution:
http://www.screencast.com/t/tlBYxkKci

           Low Resolution
   http://www.vimeo.com/...
Installing Cucumber

$ sudo gem install cucumber

$ script/plugin install git://github.com/aslakhellesoy/
cucumber.git



...
Fixtures
Scenario: search results found   Given /^the Movies$/i do |movie_hash|
  Given the Movies                 #{:titl...
Stick it to the World
module NavigationHelpers
  def path_to(page_name)
    case page_name
    when /the homepage/
      r...
Hooks
  Hooks are GLOBAL!

Before do
  # do something before each scenario.
end

After do
  #do something after each scena...
How many scenarios is
     enough?
• Given/When/Then
      “Finite state machine” Uncle Bob


• What’s the Return on Inves...
Cowcumbers (Bad smells)
    “fit only for consumption by cows”
Cowcumbers
                              Given /^dirty state$/ do
                                @movie = Movie.create!

...
Building your DSL
Concrete vs Abstract
                Scenarios
                                           Given I'm logged in
Given   I go...
Calling steps from steps

Given /I’m logged in/ do
  User.create!(:user => 'josephwilk', :password => quot;passquot;)
  Gi...
Feature Request

Can you add some of that
funky AJAX stuff for the
rent button
JavaScript kills Webrat
Watir, Selenium &
            Celerity
    Browser based       Headless Browser

•                   •
    Watir          ...
Webrat / Selenium
   # Sets up the Rails environment for Cucumber
ENV[quot;RAILS_ENVquot;] ||= quot;seleniumquot;
require ...
Organising
      @in-progress
      Feature: Verify billing


Tag    @js
       Scenario: Missing product description

   ...
Demo

             High Resolution:
http://www.screencast.com/t/1jRSI6TvXWu

             Low Resolution:
     http://www....
Further reading

•   http://cukes.info

•   http://wiki.github.com/
    aslakhellesoy/cucumber

•   http://rspec.info/

• ...
Thanks
    joe@josephwilk.net
http://github.com/josephwilk
Outside-in Development with Cucumber and Rspec
Outside-in Development with Cucumber and Rspec
Upcoming SlideShare
Loading in...5
×

Outside-in Development with Cucumber and Rspec

13,544

Published on

Talk given by Joseph Wilk at Scotland on Rails 2009. Examines how Cucumber and Rspec fit together in the testing workflow, how and why Cucumber is useful and how it works. Walks through a simple example based on renting DVDs showing how to use Cucumber and some of the best practices. Looks at how Webrat can drive Selenium to in-turn drive Cucumber features through the browser.

Published in: Technology
2 Comments
78 Likes
Statistics
Notes
No Downloads
Views
Total Views
13,544
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
1
Comments
2
Likes
78
Embeds 0
No embeds

No notes for slide

Transcript of "Outside-in Development with Cucumber and Rspec"

  1. 1. utside in Development Cucumber Rspec Joseph Wilk / http://blog.josephwilk.net
  2. 2. Why are you here?
  3. 3. Why are you here? In order to ...
  4. 4. Why are you here? In order to ... As a Scotland on Rails attendee
  5. 5. Why are you here? In order to ... As a Scotland on Rails attendee I want ...
  6. 6. What is your Acceptance Criteria?
  7. 7. Scenario: Happy SoR attendees Given people turned up When Joseph talks Then everyone should acquire some new knowledge And no-one should fall asleep And no rotten food should be thrown
  8. 8. Pass Fail
  9. 9. Outside-in User Browser Views Controllers Models
  10. 10. Cucumber “The feature you wished you had” Views Controllers Rspec Models “The components and the interactions you wish you had”
  11. 11. Cucumber “The feature you wished you had” Views Controllers Rspec Models “The components and the interactions you wish you had”
  12. 12. Cucumber “The feature you wished you had” Views Controllers Rspec Models “The components and the interactions you wish you had”
  13. 13. Cucumber / Scenario
  14. 14. Cucumber / Scenario
  15. 15. Cucumber / Scenario Rspec / code examples
  16. 16. Cucumber / Scenario Rspec / code examples
  17. 17. Cucumber / Scenario Rspec / code examples
  18. 18. Cucumber / Scenario Rspec / code examples
  19. 19. Cucumber / Scenario Rspec / code examples
  20. 20. :rspec => route finder :cucumber => destination London Edinburgh
  21. 21. Behaviour Driven Development with elegance and joy
  22. 22. Behaviour Driven Development with elegance and joy Just Cuke it!
  23. 23. Plaintext Ruby Features step definitions
  24. 24. Cucumber Feature Not executed Feature: Be awesome Narrative Scenario: title Example of Given <some context> behaviour And <yet more context> When <some action> And <more actions> Then <some outcome> And <more outcomes>
  25. 25. regexp match Step definition Given /^we like (.*)$/i do |fruit| fruit.should == 'cukes' Ruby tests #assert_equal(fruit, 'cukes') end Plaintext Given we like cukes Feature Cucumber
  26. 26. Plug and Pray Rspec Plaintext Ruby tests TestUnit Cucumber Ruby-Gnome2 Cucumber Sinatra Rails Merb iPhone
  27. 27. Cucumber English
  28. 28. огурец Russian Chinese concombre French Korean gurka Swedish Japanese German castravete Romanian Gurke ogórek Cucumber English Polish pepino Spanish Italian cetriolo Danish agurk ‫ﺍﳋﻴﺎﺭ‬ Arabic Indonisean ketimun
  29. 29. огурец Russian Chinese concombre French Korean gurka Swedish Japanese German castravete Romanian Gurke ogórek Cucumber English Polish pepino Spanish Treetop Rocks Italian cetriolo ANTLR Danish agurk could rock ‫ﺍﳋﻴﺎﺭ‬ Arabic Indonisean harder ketimun
  30. 30. Webrat Love visit home_path click_link quot;Webquot; fill_in quot;Emailquot;, :with => quot;Rat” click_button quot;Lovequot; Bansky
  31. 31. Return On Investment Cucumber Feature
  32. 32. Return On Investment Cucumber Feature • Token Conversation
  33. 33. Return On Investment Cucumber Feature • Token Conversation • Acceptance Criteria
  34. 34. Return On Investment Cucumber Feature • Token Conversation • Acceptance Criteria • Design
  35. 35. Return On Investment Cucumber Feature • Token Conversation • Acceptance Criteria • Design • Documentation
  36. 36. Return On Investment Cucumber Feature • Token Conversation • Acceptance Criteria • Design • Documentation • Functional test
  37. 37. Return On Investment Cucumber Feature • Token Conversation • Acceptance Criteria • Design • Documentation • Functional test • Integration test
  38. 38. Token for Conversation
  39. 39. Feature Request I want members to be able to rent a movie with a priority indicating how much they want to see the film
  40. 40. Show me the _ _ _ _ _
  41. 41. Show me the VA L U _ ____E
  42. 42. Root Cause Analysis
  43. 43. Root Cause Analysis Why? So we can maximise allocation of films
  44. 44. Root Cause Analysis Why? Keep customers happy Why? So we can maximise allocation of films
  45. 45. Root Cause Analysis Why? So they continue with their subscriptions Why? Keep customers happy Why? So we can maximise allocation of films
  46. 46. Root Cause Analysis Why? So they continue with their subscriptions Why? Keep customers happy Why? So we can maximise allocation of films
  47. 47. Lets write our Cucumber feature
  48. 48. Feature (terrible) Value Feature: select a movie to rent with priority In order to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Role Feature
  49. 49. Feature (terrible) Value Feature: select a movie to rent with priority In order to maximise allocation of films Narrative The website user Needs to be able to add movies to their rental list with a priority Role Feature
  50. 50. Feature (terrible) Value Feature: select a movie to rent with priority In order to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Role Feature
  51. 51. Feature (terrible) Feature: select a movie to rent with priority In order to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority
  52. 52. Feature (terrible) Feature: select a movie to rent with priority In order to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Noise
  53. 53. Feature (terrible) Feature: select a movie to rent with priority In order to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Too generic role
  54. 54. Feature (terrible) Feature: select a movie to rent with priority In order to maximise allocation of films The website user Film member Needs to be able to add movies to their rental list with a priority Too generic role
  55. 55. Feature (terrible) Role can change feature Feature: select a movie to rent with priority In order to maximise allocation of films The website user Film member Needs to be able to add movies to their rental list with a priority
  56. 56. Feature (terrible) Role can change feature Film Member selects a movie to rent with priority Feature: select a movie to rent with priority In order to maximise allocation of films The website user Film member Needs to be able to add movies to their rental list with a priority
  57. 57. Nice Feature (terrible) Film Member selects a movie to rent with priority Feature: select a movie to rent with priority In order to maximise allocation of films The website user Film member Needs to be able to add movies to their rental list with a priority
  58. 58. Acceptance Definition of Done.
  59. 59. Scenarios Feature: Film Member selects a movie to rent with priority In order to maximise allocation of films The Film member Needs to add movies to their rental list with a priority Scenario: High priority
  60. 60. Scenarios Feature: Film Member selects a movie to rent with priority In order to maximise allocation of films The Film member Needs to add movies to their rental list with a priority Scenario: High priority Then I should see quot;My rental listquot; And I should see quot;Casshernquot; in my rental list And quot;Casshernquot; should be marked as quot;High priorityquot;
  61. 61. Scenarios Feature: Film Member selects a movie to rent with priority In order to maximise allocation of films The Film member Needs to add movies to their rental list with a priority Scenario: High priority When I choose quot;High priorityquot; And I press quot;Rentquot; Then I should see quot;My rental listquot; And I should see quot;Casshernquot; in my rental list And quot;Casshernquot; should be marked as quot;High priorityquot;
  62. 62. Scenarios Feature: Film Member selects a movie to rent with priority In order to maximise allocation of films The Film member Needs to add movies to their rental list with a priority Scenario: High priority Given I'm logged in And I am viewing the movie quot;Casshernquot; When I choose quot;High priorityquot; And I press quot;Rentquot; Then I should see quot;My rental listquot; And I should see quot;Casshernquot; in my rental list And quot;Casshernquot; should be marked as quot;High priorityquot;
  63. 63. Let there be Cuking
  64. 64. Demo High Resolution: http://www.screencast.com/t/aG1sE4N3Zq Low Resolution: http://www.vimeo.com/3910617
  65. 65. Here’s one I Cuked earlier
  66. 66. Demo High Resolution: http://www.screencast.com/t/tlBYxkKci Low Resolution http://www.vimeo.com/3911104
  67. 67. Installing Cucumber $ sudo gem install cucumber $ script/plugin install git://github.com/aslakhellesoy/ cucumber.git $ gem sources -a http://gems.github.com $ sudo gem install aslakhellesoy-cucumber
  68. 68. Fixtures Scenario: search results found Given /^the Movies$/i do |movie_hash| Given the Movies #{:title => quot;The colour whitequot;}, | title | #{:title => quot;The colour redquot;}, | The colour white | #{:title => quot;Casshernquot;} | The colour red | movies.hashes.each do |values| | Casshern | Movie.create!(values) When I search for quot;colourquot; end Then I will see 2 results end Scenario: search results found Given 2 films with quot;colourquot; in the title When I search for quot;colourquot; Then I will see 2 results
  69. 69. Stick it to the World module NavigationHelpers def path_to(page_name) case page_name when /the homepage/ root_path else raise quot;Can't find mapping from quot;#{page_name}quot; to a path.quot; end end end World do |world| world.extend NavigationHelpers world end
  70. 70. Hooks Hooks are GLOBAL! Before do # do something before each scenario. end After do #do something after each scenario end
  71. 71. How many scenarios is enough? • Given/When/Then “Finite state machine” Uncle Bob • What’s the Return on Investment?
  72. 72. Cowcumbers (Bad smells) “fit only for consumption by cows”
  73. 73. Cowcumbers Given /^dirty state$/ do @movie = Movie.create! Relying on state (too much) end Given /^coupled by state$/ do @movie.title = quot;yuckyquot; end Given /^does a user care$/ do response.current_url == ‘silly’ end Tests without user value Given /^checking the db$/ do Movie.find(1).should_not == nil end
  74. 74. Building your DSL
  75. 75. Concrete vs Abstract Scenarios Given I'm logged in Given I go to the login page And I fill in quot;usernamequot; with quot;josephquot; And I fill in quot;passwordquot; with quot;cukerquot; And I click quot;loginquot; Domain understanding Concrete Abstract
  76. 76. Calling steps from steps Given /I’m logged in/ do User.create!(:user => 'josephwilk', :password => quot;passquot;) Given 'I fill in quot;passwordquot; with quot;josephwilkquot;' Given 'I fill in quot;passwordquot; with quot;passquot;' Given 'I click quot;loginquot;' end
  77. 77. Feature Request Can you add some of that funky AJAX stuff for the rent button
  78. 78. JavaScript kills Webrat
  79. 79. Watir, Selenium & Celerity Browser based Headless Browser • • Watir Celerity • • Selenium Culerity - http:// github.com/langalex/ culerity/tree/master
  80. 80. Webrat / Selenium # Sets up the Rails environment for Cucumber ENV[quot;RAILS_ENVquot;] ||= quot;seleniumquot; require File.expand_path(File.dirname(__FILE__) + '/../../config/environment') require 'cucumber/rails/world' require 'cucumber/formatters/unicode' Turn off #Cucumber::Rails.use_transactional_fixtures require 'webrat' Webrat.configure do |config| Switch to Selenium config.mode = :selenium end require 'cucumber/rails/rspec' require 'webrat/core/matchers' Before do Movie.delete_all Manually cleanup RentalRequest.delete_all end
  81. 81. Organising @in-progress Feature: Verify billing Tag @js Scenario: Missing product description Scenario: Several products cucumber --profile rails --tags ~@js cucumber --profile browser --tags @js
  82. 82. Demo High Resolution: http://www.screencast.com/t/1jRSI6TvXWu Low Resolution: http://www.vimeo.com/3911241
  83. 83. Further reading • http://cukes.info • http://wiki.github.com/ aslakhellesoy/cucumber • http://rspec.info/ • http://blog.josephwilk.net
  84. 84. Thanks joe@josephwilk.net http://github.com/josephwilk

×