Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
1 of 37

The Art of Gherkin Scripting - Matt Eakin

10

Share

With the creation of the cucumber framework came the creation of the Gherkin Scripting format (also known as the Given-When-Then format). The structure of a Gherkin script is very straight-forward: Given provides you with the background When tells you what is being created Then tells you the expected results. Writing a script in a Given-When-Then format may be fairly simple. Writing a good Gherkin Script is an Art. Some are Picassos, some are Monets, some look like they were created by a toddler with a crayon. In this presentation Mr. Eakin will offer some tips on writing good Gherkin Scripts and show you how a well crafted Gherkin Script can be a beautiful work of Art.

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

The Art of Gherkin Scripting - Matt Eakin

  1. 1. Local Touch – Global Reach www.us.sogeti.com The Art of Gherkin Scripting Matthew Eakin, Manager Managed Testing Practice Sogeti, USA Matthew.Eakin@us.sogeti.com
  2. 2. 2www.us.sogeti.com Local Touch – Global Reach Agenda Where did Gherkin Scripts come from? • BDD • The cucumber automation framework What are Gherkin scripts? • Given • When • Then Expressive Scenarios • Background • Scenario Outline • Data Tables Gherkin script writing Tips
  3. 3. 3www.us.sogeti.com Local Touch – Global Reach What is Behavior-Driven Development (BDD)? • Dan North’s response to issue encountered teaching TDD. – Where to start? What to test? How much to test? Tests are not tied to a business need. • BDD is about implementing an application by describing its behavior from the perspective of its stakeholders. • BDD focuses on obtaining a clear understanding of desired software behavior through discussion with stakeholders. • A communication and collaboration framework for developers, QA, and non- technical or business participants in a software project. • Consists of 3 components: – Test-Driven Development (TDD) – Domain-Driven Design (DDD) – Acceptance Test Driven Development(ATDD)
  4. 4. 4www.us.sogeti.com Local Touch – Global Reach The Behavior-Driven Development (BDD) Timeline
  5. 5. 5www.us.sogeti.com Local Touch – Global Reach The Cucumber Framework An automated testing framework created by Aslak Hellesoy in 2008. Based on Behavior-Driven Development. Gherkin Scripting is used to describe the users behavior. Stakeholder readable–in the domain language.
  6. 6. 6www.us.sogeti.com Local Touch – Global Reach What are Gherkin Scripts? Gherkin Scripts: connects the human concept of cause and effect to the software concept of input/process/output. >Given – indicates something that we accept to be true in a scenario >When – indicates the event in a scenario >Then – indicates an expected outcome Can also use “And” and “But” steps to help make a Given, When, or Then more descriptive In the “language of the domain” – scripts are written using the language, terms and definitions of the domain. It is “stakeholder readable” – if a script is written for business acceptance it should be written so the business users can understand it and follow what is happening. If a script is written for an Integration Test of a Web Service it should be written so the developers and testers of the Web Service can understand it and follow what is happening.
  7. 7. 7www.us.sogeti.com Local Touch – Global Reach Given The purpose of Given steps is to put the system in a known state before the user (or external system) starts interacting with the system (in the When steps) Indicates something that we accept to be true in a scenario Sometimes know as preconditions GREAT place to set-up test data Hint: no functionality should be changing in the Given step. So…you should rarely have an error in the Given steps. >Examples: Given the BadPasswordUser logs into Online Banking And navigates to the Account History page And selects a Premier Checking account Given I am entitled to Initiate Payment And Payee Maintenance And I have 1 Bill Pay account And I have accounts entitled to Bill Pay And I have a funding account
  8. 8. 8www.us.sogeti.com Local Touch – Global Reach When The purpose of When steps is to describe the key action the user performs It is what the business is requesting It is what the developers are developing It is what the testers are testing The MONEY step: it is what everyone is being paid to build & test >Examples: When the user Logs in When I view the Pay Bills tab When I select a Bill Pay account
  9. 9. 9www.us.sogeti.com Local Touch – Global Reach Then The purpose of Then steps is to observe outcomes • The expected results of the When statement The observations should be related to the business value/benefit in your feature description The observations should also be on some kind of output – that is something that comes out of the system (report, user interface, message) and not something that is deeply buried inside it (that has no business value) > Examples: Then I will see a highlighted Pay Bills tab And I will see a header that reads "Enter Payment Criteria“ And I will see a table containing payment method icons to the left of each transaction Then the too many characters error message appears
  10. 10. 10www.us.sogeti.com Local Touch – Global Reach There is no After step Unfortunately Gherkin scripting does not provide an elegant way to clean-up test data or exit out of an application. There is no “After” step. Instead use “And…”
  11. 11. 11www.us.sogeti.com Local Touch – Global Reach Backgrounds Allow you to write part of a script to be executed before each Scenario in the Feature File • Allows readers to focus on what is important in the Scenario • Can be very useful if multiple Scenarios in a Feature have same “Given” steps Syntax: Background: <name> Given <action> #will be executed before each Scenario Scenario: <name1> When … Then … Scenario: <name2> When … Then …
  12. 12. 12www.us.sogeti.com Local Touch – Global Reach Background Example Scripts that looked like this… Becomes this… Scenario: Wait State Message displayed for Reference Search Given I am logged in as a BOL user When I view the Pay Bills tab Then I will see a highlighted Pay Bills tab Scenario: Wait State Message displayed for Reaction Search Given I am logged in as a BOL user When I select a Bill Pay account Then I will see a header that reads "Enter Payment Criteria" Background: logging in as a BOL user Given I am logged in as a BOL user Scenario: Wait State Message displayed for Reference Search When I view the Pay Bills tab Then I will see a highlighted Pay Bills tab Scenario: Wait State Message displayed for Reaction Search When I select a Bill Pay account Then I will see a header that reads "Enter Payment Criteria"
  13. 13. 13www.us.sogeti.com Local Touch – Global Reach Scenario Outlines Cucumber allows you to write one Gherkin Script but execute it multiple times Use keywords to tie to an example table Great if you have several TestCases where only variables change Sample Code: Scenario Outline: submit guess Given the secret code is "<code>" When I guess "<guess>" Then the mark should be "<mark>" Example: | code | guess | mark | | 1234 | 1234 | ++++ | | 1234 | 1243 | ++-- | | 1234 | 1423 | +--- | | 1234 | 4321 | ---- |
  14. 14. 14www.us.sogeti.com Local Touch – Global Reach Scenario Outline Example… Scripts that looked like this… Becomes this… Scenario: all numbers correct and in order Given the secret code is “1234" When I guess “1234" Then the mark should be “++++" Scenario: all numbers correct 2 in order Given the secret code is “1234" When I guess “1243" Then the mark should be “++--" Scenario: all numbers correct one in order Given the secret code is “1234" When I guess “1423" Then the mark should be “+---" Scenario: all numbers correct none in order Given the secret code is “1234" When I guess “4321" Then the mark should be “----” Scenario Outline: submit guess Given the secret code is "<code>" When I guess "<guess>" Then the mark should be "<mark>" Example: all numbers correct | code | guess | mark | | 1234 | 1234 | ++++ | | 1234 | 1243 | ++-- | | 1234 | 1423 | +--- | | 1234 | 4321 | ---- |
  15. 15. 15www.us.sogeti.com Local Touch – Global Reach Data Tables in Gherkin Scripts Cucumber allows you to put tables directly in your Gherkin Scripts Use a pipe “|” to separate columns in the table Use ctrl+alt+l (lowercase letter L) to align tables in cucumber For example: Given I ran the Boston Marathon in 3:45 And I ran the Columbus Marathon in 3:29 And I ran the New York Marathon in 4:10 Can become: Given I ran: | race | time | | Boston Marathon | 3:45 | | Columbus Marathon | 3:29 | | New York Marathon | 4:10 | The Scenario becomes much clearer!!
  16. 16. 16www.us.sogeti.com Local Touch – Global Reach Other ways of writing tables Cucumber gives you the flexibility to put the table headers on the left side… Given I have a vehicle with the following description: | Wheels | 2 | | Max Speed | 60 mph | | Accessories | lights, shopping basket | You can also just specify a list… Given my wife gave me the following shopping list: | onions | | tomatoes | | milk | | Fruity Pebbles |
  17. 17. 17www.us.sogeti.com Local Touch – Global Reach Tips for Writing Good Gherkin Scripts 1) Start with the WHEN 2) One WHEN per scenario – single responsibility principle 3) One action per step 4) Brian Marick’s Agile Testing Quadrants/Mike Cohn’s Test Automation Pyramid 5) Tags – a great way to organize your scripts 6) Don’t mix domains 7) Maintainability of Scripts (and Code) 8) Are any steps re-usable? 9) Do or do not, there is no try 10) Start with the conversation 11) Gherkin Scripts make great Acceptance Criteria but not a good User Story
  18. 18. 18www.us.sogeti.com Local Touch – Global Reach Tip #1: Start with the WHEN Key question to ask: What are you trying to build/test in this scenario? • What is the MAGIC?? • Should be stated in the Scenario Name This becomes your WHEN step Once you know the action, step #2 is the result (the THEN) Last step is to figure out how to get to the WHEN This is the functionality the business is requesting This is what the Programmers are building This is what the Testers are testing
  19. 19. 19www.us.sogeti.com Local Touch – Global Reach Begin with the WHEN, an example… Epic/Project: A college sports web site where you can find out all kinds of facts about any colligate sports team. Requirement 4: A search page for college basketball teams rosters. To display a teams roster select the team. Columns visible are: • Player Jersey Number • Player Name • Position • Height • Weight • Year (Fr., Soph., Jr., Sr.) • Hometown (Previous School) What is the WHEN (the action)?
  20. 20. 20www.us.sogeti.com Local Touch – Global Reach Example, ctd… Test Case: Select the team and their roster will be displayed. Gherkin Script: Given I navigate to the web page When I select “The Ohio State University Men’s Basketball” Team Then I will see the following players: | No. | Name | Pos. | Ht. | Wt. | Yr. | Hometown (Prev School) | 0 | D'Angelo Russell | Guard | 6-5 | 180 | FR | Louisville, Ky. (Montverde Academy) | 1 | Jae'Sean Tate | Forward | 6-4 | 190 |FR | Pickerington, Ohio (Pickerington Central) | 2 | Marc Loving | Forward | 6-72 | 15 | SO | Toledo, Ohio (St. John Jesuit) | 3 | Shannon Scott | Guard | 6-1 | 185 | SR | Alpharetta, Ga. (Milton) | 10 | David Bell | Center | 6-10 | 225 | FR | Garfield Heights, Ohio (Garfield Heights) | 12 | Sam Thompson | Forward | 6-7 | 200 | SR | Chicago, Ill. (Whitney Young) | 15 | Kam Williams | Guard | 6-2 | 175 | RS | FR | Baltimore, Md. (Mount St. Joseph's) | 23 | Amir Williams | Center | 6-11 | 250 | SR | Detroit, Mich. (Detroit Country Day)
  21. 21. 21www.us.sogeti.com Local Touch – Global Reach Tip #2: One WHEN per Scenario Only one WHEN per Scenario Key testing concept: you only want to test one piece of functionality per script • Isolate the functionality • Isolate the issue (if any are found) Exceptions: • End-to-End tests • Regression scripts
  22. 22. 22www.us.sogeti.com Local Touch – Global Reach Tip #3: One action per step You want to make sure each step does only one action. This makes the code more maintainable & re-usable. Problem step: • When I enter “abc” in the username and “123” in the password and click the login button Better steps: • When I enter “abc” in the username • And I enter “123” in the password • And I click the login button
  23. 23. 23www.us.sogeti.com Local Touch – Global Reach Technology-Facing Tip #4: Brian Marick’s Testing Quadrants Functional/ Integration Tests UAT Exploratory Usability Unit Tests Component Tests Performance Load Security Q2 Q3 Q1 Q4 SupportingTheTeam CritiqueProduct Business-Facing Automated & Manual Automated & Manual Automated Tools Rarely Use Gherkin Scripts Gherkin Acceptance Scripts Gherkin Integration Scripts
  24. 24. 24www.us.sogeti.com Local Touch – Global Reach The Automation Testing Pyramid Created by Mike Cohn in Succeeding with Agile Essential points: 1. Unit Tests form the base of your testing strategy. 2. You should have more Unit Tests than any other kind of test. 3. There is no direct relationship between Unit and GUI tests.
  25. 25. 25www.us.sogeti.com Local Touch – Global Reach Example of a UAT (Quadrant 3) Test User Story: As a sports fan I need to see a roster of players on a team So I can Acceptance Criteria: select a team will display a roster of that team Information: A search page for college basketball teams rosters. To display a teams roster select the team. Columns visible are: • Player Jersey Number • Player Name • Position • Height • Weight • Year (Fr., Soph., Jr., Sr.) • Hometown (Previous School) Scenario: Select the team and their roster will be displayed. Gherkin Acceptance Script: Given I navigate to the web page When I select a Team Then I will see a roster of players
  26. 26. 26www.us.sogeti.com Local Touch – Global Reach Example of an Integration (Quadrant 2) Test Gherkin Integration Script: Given I navigate to the web page When I select “The Ohio State University Men’s Basketball” Team Then I will see the following players: | No. | Name | Pos. | Ht. | Wt. | Yr. | Hometown (Prev School) | 0 | D'Angelo Russell | Guard | 6-5 | 180 | FR | Louisville, Ky. (Montverde Academy) | 1 | Jae'Sean Tate | Forward | 6-4 | 190 |FR | Pickerington, Ohio (Pickerington Central) | 2 | Marc Loving | Forward | 6-72 | 15 | SO | Toledo, Ohio (St. John Jesuit) | 3 | Shannon Scott | Guard | 6-1 | 185 | SR | Alpharetta, Ga. (Milton) | 10 | David Bell | Center | 6-10 | 225 | FR | Garfield Heights, Ohio (Garfield Heights) | 12 | Sam Thompson | Forward | 6-7 | 200 | SR | Chicago, Ill. (Whitney Young) | 15 | Kam Williams | Guard | 6-2 | 175 | RS | FR | Baltimore, Md. (Mount St. Joseph's) | 23 | Amir Williams | Center | 6-11 | 250 | SR | Detroit, Mich. (Detroit Country Day)
  27. 27. 27www.us.sogeti.com Local Touch – Global Reach Tip #5: Tags A great way to organize your scripts Syntax: @<text> Example: @acceptance @roster_display Scenario: Team roster displayed Given I navigate to the web page When I select a Team Then I will see a roster of players @integration @roster_display Scenario: Team roster displayed Given I navigate to the web page When I select “The Ohio State University Men’s Basketball” Team Then I will see the following players: | No. | Name | Pos. | Ht. | Wt. | Yr. | Hometown (Prev School) | 0 | D'Angelo Russell | Guard | 6-5 | 180 | FR | Louisville, Ky. (Montverde Academy) | 1 | Jae'Sean Tate | Forward | 6-4 | 190 |FR | Pickerington, Ohio (Pickerington Central) | 2 | Marc Loving | Forward | 6-72 | 15 | SO | Toledo, Ohio (St. John Jesuit) | 3 | Shannon Scott | Guard | 6-1 | 185 | SR | Alpharetta, Ga. (Milton) | 10 | David Bell | Center | 6-10 | 225 | FR | Garfield Heights, Ohio (Garfield Heights) | 12 | Sam Thompson | Forward | 6-7 | 200 | SR | Chicago, Ill. (Whitney Young) | 15 | Kam Williams | Guard | 6-2 | 175 | RS | FR | Baltimore, Md. (Mount St. Joseph's) | 23 | Amir Williams | Center | 6-11 | 250 | SR | Detroit, Mich. (Detroit Country Day)
  28. 28. 28www.us.sogeti.com Local Touch – Global Reach Tip #6: Don’t mix domains Adapted from Markus Gärtner’s Principles of ATDD blog http://www.shino.de/2014/05/28/principles-of-atdd-single-responsibility- principle/#more-3181 Question: What is wrong with this script? Scenario: A simple Google search scenario Given I am on the main Google search page When I fill in “q” with “ATDD by example” And I click “gbqfb” button Then I should see “ATDD by Example: A Practical Guide to Acceptance Test-Driven Development (Addison-Wesley Signature Series (Beck))” Answer: The domains are mixed.
  29. 29. 29www.us.sogeti.com Local Touch – Global Reach Solution… Scenario: A simple Google search scenario Given I am on the main Google search page When I fill in the Search field with “ATDD by example” Then I should see “ATDD by Example: A Practical Guide to Acceptance Test-Driven Development (Addison-Wesley Signature Series (Beck))”
  30. 30. 30www.us.sogeti.com Local Touch – Global Reach Tip #7: Is the Script (and supporting code) maintainable? Maintainable scripts are ones that do not need to be changed if the system changes Goal: In the GIVEN, push the brittleness into the code and out of your scripts so you rarely have to change it Example of brittle Gherkin code (this is used in all 527 scripts): Given I navigate to mattsbank.com And I enter “matt” in the UserName field And I enter “abc123” in the Password field And I click the “LogIn” button And I land on my account information page How to make it more maintainable Given I navigate to Online Banking And I log in as a ValidPasswordUser And I land on my account information page Hardcoded URL Hardcoded Login Information “Soft” URL “Soft” Login
  31. 31. 31www.us.sogeti.com Local Touch – Global Reach Why is the script more maintainable? Back-End Apps Objects Step Definitions (cucumber) Gherkin Given a ValidPasswordUser logs into Online Banking Given /^… user = User.new on(LoginPage).login(user.valid_password) End Login Page User Information Database User Password Database User Class Object PushBrittlenessDown
  32. 32. 32www.us.sogeti.com Local Touch – Global Reach Tip #8: Re-Usability of Steps Often steps will say different things but do the same thing The fewer cucumber steps you have, the more clean your code is • Easier to maintain • Easier to debug • Easier to understand Automation can significantly help re-usability Refactoring is a key development concept • You want to refactor your Gherkin scripts
  33. 33. 33www.us.sogeti.com Local Touch – Global Reach Tip #9: Master Yoda says… Either a step works or it doesn’t, there is no might or should Was: Then I should see the Wrong Account Number error message Becomes: Then the Wrong Account Number error message is displayed
  34. 34. 34www.us.sogeti.com Local Touch – Global Reach Tip #10: Start with a Conversation There is no better way to get the language of the domain than to actually hear a business person talk through it. When you first talk with the business owner about a User Story/Requirement let them talk about. • Take good notes. • Are there any special data or environment requirements needed? The Given… • What is the action they are requesting you build? The When… • What is the result of the action? The Then… Take these notes back and write your Gherkin scripts. • Use as much of the actual conversation as you can.
  35. 35. 35www.us.sogeti.com Local Touch – Global Reach Tip #11: Gherkin Scripts make Bad User Stories When you write a User Story as a Gherkin script you will still have… • I need…directly ties to the When steps in a Gherkin Script. Tells the team what functionality needs to be built/tested. But you will lose… • As a…there is no place in a Gherkin Script to tell the team what persona is using the application. • So I can…there is no place in a Gherkin script to tell the team why this particular change is needed. What is the business or user need? You still don’t know the answer.
  36. 36. 36www.us.sogeti.com Local Touch – Global Reach Tip #11b: Gherkin Scripts Make Great Acceptance Criteria When you write an Acceptance Criteria as a Gherkin script you will gain… • A clear definition of “done”…this is what the business is telling you it expects. • A “happy path” script of what the business wants…you will now have most of your first test script handed to you by the business. • A solid basis for additional test cases/objectives…with a “happy path” script already created by the business, creating test cases/objects around it becomes much easier. • Example: – When I select a team Then I will see the teams roster
  37. 37. www.us.sogeti.com Local Touch – Global Reach Thank you

×