Download - Cucumber Crash Course

Transcript
Page 1: Cucumber Crash Course

presents

RELEASE THE GHERKIN!©2012 AgilityFeat Inc

Page 2: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Page 3: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

What it is

• Straight-forward language• Common Language• Communication Tool• Concrete Examples• Describes behavior

Page 4: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

#protip

“make readability your main goal. Otherwise, a reader can easily feel more like they’re a reading computer program than a specification document” - Aslak Hellesøy (creator of Cucumber)

Page 5: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Cucumber’s Natural Habitat

Page 6: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Features

Step Definitions

Ruby Magic

Application Under Test

Plain textFilename ends in .featureDescribes behavior of systemLives in features directory

Ruby codeFilename ends in.rbDoes regex match of text from feature filesLives in features/step_definition directory

Your preferred gems go hereReferenced in features/support/env.rb

We will be testing a web-page

Page 7: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Feature: Brown Bag Demo

Scenario: Google SearchGiven I am on “http://www.google.com”When I search for “Hello World”Then I should see results containing the search phrase

Example

Page 8: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

SolutionGiven /^I am on “([^”]*)”$/ do |arg1|

visit(arg1)end

When /^I search for “([^”]*)”$/ do |arg1|@search_phrase = arg1within(:css, “#gbqfqw”) dofill_in ‘q’, :with => @search_phraseend

end

Then /^I should see results containing the search phrase$/ dopage.should have_content(@search_phrase)

end

Page 9: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Feature: Class Demonstration

Scenario: Show a Hello WorldGiven I want to say “Hello World!”

When I execute the application Then I should see the message

Page 10: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Feature: Class Demonstration

Scenario: Show a Hello WorldGiven I want to say “Hello World!”

When I execute the application Then I should see the message

And it should be italicBut it should not be bold

Page 11: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Feature: Class Demonstration

Scenario: Show a Hello World* I want to say “Hello World!”* I execute the application* I should see the message* it should be italic* it should not be bold

Page 12: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Feature: Class Demonstration

Scenario: Show a Hello WorldGiven I want to say “Hello World!”

When I execute the application Then I should see the message

And it should be italicBut it should not be bold

Page 13: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Feature: Class DemonstrationIn order to expose you to new ideasAs an instructorI want to show you an end-to-end example

Scenario: Show a Hello WorldGiven I want to say “Hello World!”

When I execute the application Then I should see the message

And it should be italicBut it should not be bold

Page 14: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Feature: Class DemonstrationAs an instructorI would like to show an end-to-end exampleSo that I can expose you to new ideas

Background: Given I want to say “Hello World!”When I execute the applicationThen I should see the message

Scenario: Show an italicized Hello WorldAnd it should be italic

Scenario: Show a bold Hello WorldBut it should not be bold

Page 15: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Feature: International Display

Scenario: Display Hello World in EnglishGiven I want to say “Hello World!”When I execute the applicationAnd I specify English as the languageThen the output should be “Hello World!”

Scenario: Display Hello World in SpanishGiven I want to say “Hello World!”When I execute the applicationAnd I specify Spanish as the languageThen the output should be “¡Hola, mundo!”

Scenario: Display Hello World in GermanGiven I want to say “Hello World!”When I execute the applicationAnd I specify German as the languageThen the output should be “Hallo Welt!”

Page 16: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Feature: Internationalize Hello World

Scenario Outline: Display in multiple languagesGiven I want to say “Hello World!”When I execute the applicationAnd I specify “<language>” as the languageThen the output should be “<output>”

Examples:| language | output |

| English | Hello World! | | Spanish | ¡Hola, mundo! |

| German | Hallo Welt! |

Page 17: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Feature: Travel The World

Scenario: Keep Track Of Visits Given a list like this: | Location | Times visited |

| Costa Rica | 0|

| Germany | 0|

When I travel to Costa RicaThen the list should look like this:| Location | Times visited | | Costa Rica | 1

|| Germany | 0

|

Page 18: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Doc strings

Scenario: Look up hostel reservation e-mailWhen I search for “Alexander House”Then I should see the following message:""" Welcome Mr. Haeffner.You’ll be staying in the bunk house this visit.Please refrain from stealing our towels (again).""”

Page 19: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Recap -- Features

• Plain text files that end in .feature• They live in the root of the features directory• Given, When, Then, And, But are trigger words• They matter most for readability• Descriptive text can go at the top• There are several approaches to abstract features

and make them more readable• Usage depends on intent & team understanding

Page 20: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Recap -- Step Definitions

• Ruby files that live in features/step_definitions• Matches plain text from feature files• Stores it into a variable• You can set this to a class level variable for use

across your test scenario• Use a gem to get where you need to go (e.g.

Capybara for web testing)

Page 21: Cucumber Crash Course

© 2012 AgilityFeat, Inc.

Recap -- System Setup

• Gems you want to use can be added to env.rb• It lives in the features/support directory• If/when you create helper methods, create a ruby

file and place them in this directory• If you add something to your test harness and

execution order is important, place it in env.rb – it loads before everything else