Slide 1

Slide 1 text

Behaviour-driven development with Cucumber Kerry Buckley (@kerryb) Suffolk Developers, 30 March 2015 http://www.flickr.com/photos/yogendra174/4665988849

Slide 2

Slide 2 text

A developer tool for end-to-end testing of ruby web applications

Slide 3

Slide 3 text

A developer tool for end-to-end testing of ruby web applications LIE

Slide 4

Slide 4 text

Basics

Slide 5

Slide 5 text

Feature: Suffolk Developers talk In order to fill the schedule As a Cucumber user I want to talk about BDD with Cucumber Scenario: "BDD with Cucumber" talk Given I have managed to prepare a talk And there are people there to hear it When I give the talk Then the audience should learn something

Slide 6

Slide 6 text

Feature: Suffolk Developers talk In order to fill the schedule As a Cucumber user I want to talk about BDD with Cucumber Scenario: "BDD with Cucumber" talk Given I have managed to prepare a talk And there are people there to hear it When I give the talk Then the audience should learn something

Slide 7

Slide 7 text

Feature: Suffolk Developers talk In order to fill the schedule As a Cucumber user I want to talk about BDD with Cucumber Scenario: "BDD with Cucumber" talk Given I have managed to prepare a talk And there are people there to hear it When I give the talk Then the audience should learn something

Slide 8

Slide 8 text

Feature: Suffolk Developers talk In order to fill the schedule As a Cucumber user I want to talk about BDD with Cucumber Scenario: "BDD with Cucumber" talk Given I have managed to prepare a talk And there are people there to hear it When I give the talk Then the audience should learn something

Slide 9

Slide 9 text

Feature: Suffolk Developers talk In order to fill the schedule As a Cucumber user I want to talk about BDD with Cucumber Scenario: "BDD with Cucumber" talk Given I have managed to prepare a talk And there are people there to hear it When I give the talk Then the audience should learn something

Slide 10

Slide 10 text

Feature: Suffolk Developers talk In order to fill the schedule As a Cucumber user I want to talk about BDD with Cucumber Scenario: "BDD with Cucumber" talk Given I have managed to prepare a talk And there are people there to hear it When I give the talk Then the audience should learn something

Slide 11

Slide 11 text

Feature: Withdraw cash from account Scenario: Successful withdrawal Given I have £100 in my account When I request a withdrawal of £20 Then I should receive £20 And my balance should be £80

Slide 12

Slide 12 text

$ cucumber withdraw_cash.feature Feature: Withdraw cash from account Scenario: Successful withdrawal # withdraw_cash.feature:3 Given I have £100 in my account # withdraw_cash.feature:4 When I request a withdrawal of £20 # withdraw_cash.feature:5 Then I should receive £20 # withdraw_cash.feature:6 And my balance should be £80 # withdraw_cash.feature:7 1 scenario (1 undefined) 4 steps (4 undefined) 0m0.002s You can implement step definitions for undefined steps with these snippets: Given /^I have £(\d+) in my account$/ do |arg1| pending # express the regexp above with the code you wish you had end ...

Slide 13

Slide 13 text

Scenario: Successful login Given a user "Amy" with password "secret" When I go to the login page And I fill in "User name" with "Amy" And I fill in "Password" with "secret" And I press "Log in" Then I should see "Welcome, Amy"

Slide 14

Slide 14 text

Scenario: Adding a CaaS Linux Service in a production (or test) environment Given a fred production environment exists And I am on the last deployment page When I select "CaaS Linux Service" from "Add a new" And I press "Add service" And I fill in the following: | Role | FTP Server | | Application Installed Software | None | And I select "SC3 (redside)" from "Security Domain" And I select "2" from "Virtual CPUs" And I select "2" from "Virtual Memory" And I select "OEL 5u6 32bit" from "Operating System" And I select "None" from "MaaS(EMB) MQ Client" And I select "None" from "Oracle Client" And I check "Netbackup Client" And I check "XFB Client" And I check "BT Hunter Client" And I check "BTBPTM Client" And I check "Web Tier" And I press "Save" Then I should be on the last deployment page And I should see "Service saved" And I should see ...

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Scenario: Successful login Given a user "Amy" with password "secret" When I go to the login page And I fill in "User name" with "Amy" And I fill in "Password" with "secret" And I press "Log in" Then I should see "Welcome, Amy"

Slide 17

Slide 17 text

Scenario: User is greeted upon login Given the user "Amy" has an account When she logs in Then she should see "Welcome, Amy"

Slide 18

Slide 18 text

Given /^the user "(.*?)" has an account$/ do |name| @user = User.find_or_create_by_name name, password: "secret" end When /^s?he logs in$/ do visit "/login" fill_in "User name", with: @user.name fill_in "Password", with: @user.password click_button "Log in" end Then /^s?he should see "(.*?)"$/ do |text| page.should have_content(text) end

Slide 19

Slide 19 text

Scenario: User is greeted upon login Given the user "Amy" has an account When she logs in Then she should see "Welcome, Amy" Scenario: Secure pages require login When I visit a secure page as a guest Then I should be asked to log in When I log in with a valid account Then I should see the secure page

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Feature: My awesome system Scenario: Using the system Given the system exists When I use it Then everything should work perfectly

Slide 22

Slide 22 text

A developer tool for end-to-end testing of web applications

Slide 23

Slide 23 text

A developer tool for end-to-end testing of web applications LIE

Slide 24

Slide 24 text

[video of tests running with selenium driving firefox]

Slide 25

Slide 25 text

A tool for end-to- end testing of web applications

Slide 26

Slide 26 text

A tool for end-to- end testing of web applications LIE

Slide 27

Slide 27 text

http://commons.wikimedia.org/wiki/File:Cucumber_and_cross_section.jpg

Slide 28

Slide 28 text

https://www.flickr.com/photos/ironammonite/6012328453

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

https://www.flickr.com/photos/beautifulcataya/3660866944

Slide 31

Slide 31 text

More gherkin features

Slide 32

Slide 32 text

Feature: Posting messages to friends Scenario: Posting a text message Given I am logged in When I post a text message Then my friends should see my message Scenario: Posting a photo Given I am logged in When I post a photo Then my friends should see a thumbnail of my photo And clicking the thumbnail should show the photo

Slide 33

Slide 33 text

Feature: Posting messages to friends Scenario: Posting a text message Given I am logged in When I post a text message Then my friends should see my message Scenario: Posting a photo Given I am logged in When I post a photo Then my friends should see a thumbnail of my photo And clicking the thumbnail should show the photo

Slide 34

Slide 34 text

Feature: Posting messages to friends Background: Given I am logged in Scenario: Posting a text message When I post a text message Then my friends should see my message Scenario: Posting a photo When I post a photo Then my friends should see a thumbnail of my photo And clicking the thumbnail should show the photo

Slide 35

Slide 35 text

Scenario: When I add 1 widget to my order And I add 5 doodahs to my order And I add 2 thingummies to my order Then my basket total should be £12.34

Slide 36

Slide 36 text

Scenario: When I add the following to my order: | Quantity | Item | | 1 | widget | | 5 | doodah | | 2 | thingummy | Then my basket total should be £12.34

Slide 37

Slide 37 text

Scenario: Simple addition When I calculate 2 + 2 Then the answer should be 4 When I calculate 10 + 5 Then the answer should be 15 Scenario: Simple multiplication When I calculate 2 * 2 Then the answer should be 4 When I calculate 6 * 7 Then the answer should be 42

Slide 38

Slide 38 text

Scenario Outline: Simple arithmetic When I calculate Then the answer should be Examples: | first | operation | second | answer | | 2 | + | 2 | 4 | | 10 | + | 5 | 15 | | 2 | * | 2 | 4 | | 6 | * | 7 | 42 |

Slide 39

Slide 39 text

http://www.flickr.com/photos/yogendra174/4665988849

Slide 40

Slide 40 text

# language: fr Fonctionnalité: Addition Afin de financer mon bonus avec l'argent des pigeons En tant que trader Je souhaite pouvoir additionner 2 chiffres Plan du Scénario: Addition de produits dérivés Soit une calculatrice Etant donné qu'on tape Et qu'on tape Lorsqu'on tape additionner Alors le résultat doit être Exemples: | a | b | somme | | 2 | 2 | 4 | | 2 | 3 | 5 |

Slide 41

Slide 41 text

# language: es Característica: adición Para evitar hacer errores tontos Como un matemático idiota Quiero saber la suma de los números Esquema del escenario: Sumar dos números Dado que he introducido en la calculadora Y que he introducido en la calculadora Cuando oprimo el Entonces el resultado debe ser en la pantalla Ejemplos: | entrada_1 | entrada_2 | botón | resultado | | 20 | 30 | add | 50 | | 2 | 5 | add | 7 | | 0 | 40 | add | 40 |

Slide 42

Slide 42 text

# language: de Funktionalität: Addition Um dumme Fehler zu vermeiden möchte ich als Matheidiot die Summe zweier Zahlen gesagt bekommen Szenariogrundriss: Zwei Zahlen hinzufügen Angenommen ich habe in den Taschenrechner eingegeben Und ich habe in den Taschenrechner eingegeben Wenn ich drücke Dann sollte das Ergebniss auf dem Bildschirm sein Beispiele: | Eingabe_1 | Eingabe_2 | Knopf | Ausgabe | | 20 | 30 | add | 50 | | 2 | 5 | add | 7 | | 0 | 40 | add | 40 |

Slide 43

Slide 43 text

# language: en-lol OH HAI: STUFFING MISHUN: CUCUMBR I CAN HAZ IN TEH BEGINNIN 3 CUCUMBRZ WEN I EAT 2 CUCUMBRZ DEN I HAS 2 CUCUMBERZ IN MAH BELLY AN IN TEH END 1 CUCUMBRZ KTHXBAI

Slide 44

Slide 44 text

Getting the most out of Cucumber

Slide 45

Slide 45 text

http://www.flickr.com/photos/oskay/2156888497

Slide 46

Slide 46 text

Given /^the user "(.*?)" has an account$/ do |name| @user = User.find_or_create_by_name name, password: "secret" end Given /^a logged-in user$/ do @user = User.find_or_create_by_name "Fred", password: "secret" visit "/login" fill_in "User name", :with => @user.name fill_in "Password", :with => @user.password click_button "Log in" end When /^s?he logs in$/ do visit "/login" fill_in "User name", with: @user.name fill_in "Password", with: @user.password click_button "Log in" end Then /^s?he should see "(.*?)"$/ do |text| page.should have_content(text) end

Slide 47

Slide 47 text

Given /^the user "(.*?)" has an account$/ do |name| @user = User.find_or_create_by_name name, password: "secret" end Given /^a logged-in user$/ do @user = User.find_or_create_by_name "Fred", password: "secret" visit "/login" fill_in "User name", :with => @user.name fill_in "Password", :with => @user.password click_button "Log in" end When /^s?he logs in$/ do visit "/login" fill_in "User name", with: @user.name fill_in "Password", with: @user.password click_button "Log in" end Then /^s?he should see "(.*?)"$/ do |text| page.should have_content(text) end

Slide 48

Slide 48 text

module LoginSteps def login(name, password) visit "/login" fill_in "User name", with: name fill_in "Password", with: password click_button "Log in" end end World(LoginSteps)

Slide 49

Slide 49 text

module LoginSteps def login(name, password) visit "/login" fill_in "User name", with: name fill_in "Password", with: password click_button "Log in" end end World(LoginSteps) Given /^a logged in user$/ do @user = User.find_or_create_by_name name, password: "secret" login(@user.name, @user.password) end When /^s?he logs in$/ do login(@user.name, @user.password) end

Slide 50

Slide 50 text

A tool for
 end-to-end testing of applications

Slide 51

Slide 51 text

A tool for
 end-to-end testing of applications LIE

Slide 52

Slide 52 text

http://tooky.co.uk/2013/01/18/cucumber-and-full-stack-testing.html

Slide 53

Slide 53 text

http://blog.mattwynne.net/2012/05/31/hexagonal-rails-objects-values-and-hexagons/

Slide 54

Slide 54 text

A tool for testing applications

Slide 55

Slide 55 text

A tool for testing applications LIE

Slide 56

Slide 56 text

http://www.flickr.com/photos/elycefeliz/3224486233

Slide 57

Slide 57 text

Automated tests 35% Agreeing steps 40% Identifying scenarios 25% Made-up statistics

Slide 58

Slide 58 text

A tool to help with behaviour-driven development

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

$ cucumber talk.feature Feature: Suffolk Developers talk In order to fill the schedule As a Cucumber user I want to talk about BDD with Cucumber Scenario: "BDD with Cucumber" talk # talk.feature:6 Given I have managed to prepare my talk on time # talk_steps.rb:1 And there are people there to hear it # talk_steps.rb:4 When I give the talk # talk_steps.rb:7 Then the audience should learn something # talk_steps.rb:10 1 scenario (1 passed) 4 steps (4 passed) 0m0.002s

Slide 61

Slide 61 text

http://www.flickr.com/photos/erix/3780828260