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.

Geek Time Novembre 2016 : Cucumber

197 views

Published on

Présentation et atelier Cucumber
Par Khaled HALLEB, consultant OLBATI
Au Geek Time de Novembre 2016

Published in: Software
  • Be the first to comment

  • Be the first to like this

Geek Time Novembre 2016 : Cucumber

  1. 1. Title geek time - Novembre 2016 HALLEB Khaled OLBATI Consultant
  2. 2. OLBATI © 2016 - Geek Time November 2016 2 Cucumber? ● Tool for running automated tests ■ Support BDD ( using Gherkin language) ■ A single source of truth ■ Living documentation ■ Focus on the customer ■ Less rework
  3. 3. OLBATI © 2016 - Geek Time November 2016 3 Cucumber and BDD ? ● Cucumber is for Behaviour-Driven Development
  4. 4. OLBATI © 2016 - Geek Time November 2016 4 Gherkin ● Gherkin is plain-text English (or one of 60+ other languages) with a little extra structure ● Feature (Functionality) written in .feature files and defined by ● Feature − Name of the feature under test. ● Description (optional) − Describe about feature under test. ● Scenario − What is the test scenario. ● Steps
  5. 5. OLBATI © 2016 - Geek Time November 2016 5 Gherkin ● Description Feature: <feature title> As a <persona|role> I want to <action> So that <outcome>
  6. 6. OLBATI © 2016 - Geek Time November 2016 6 Gherkin ● Scenario A scenario is a concrete example that illustrates a business rule. It is a list of steps. You can have as many steps as you like, but we recommend you keep the number at 3-5 per scenario. If they become longer than that they lose their expressive power as specification and documentation.
  7. 7. OLBATI © 2016 - Geek Time November 2016 7 Gherkin ● Step ● Given − Prerequisite before the test steps get executed. ● When − Specific condition which should match in order to execute the next step. ● Then − What should happen if the condition mentioned in WHEN is satisfied. If there are multiple Given or When steps underneath each other, you can use And or But
  8. 8. OLBATI © 2016 - Geek Time November 2016 8 Gherkin ● Backgound Occasionally you'll find yourself repeating the same Given steps in all of the scenarios in a feature file. Since it is repeated in every scenario it is an indication that those steps are not essential to describe the scenarios, they are incidental details. You can literally move such Given steps to the background by grouping them under a Background section before the first scenario: Background: Given a $100 microwave was sold on 2015-11-03 And today is 2015-11-18
  9. 9. OLBATI © 2016 - Geek Time November 2016 9 Gherkin ● Scenario Outline When you have a complex business rule with severable variable inputs or outputs you might end up creating several scenarios that only differ by their values. 1- Scenario: feeding a small suckler cow Given the cow weighs 450 kg When we calculate the feeding requirements Then the energy should be 26500 MJ And the protein should be 215 kg 2- Scenario: feeding a medium suckler cow Given the cow weighs 500 kg When we calculate the feeding requirements Then the energy should be 29500 MJ And the protein should be 245 kg #
  10. 10. OLBATI © 2016 - Geek Time November 2016 10 Gherkin Scenario Outline: feeding a suckler cow Given the cow weighs <weight> kg When we calculate the feeding requirements Then the energy should be <energy> MJ And the protein should be <protein> kg Examples: | weight | energy | protein | | 450 | 26500 | 215 | | 500 | 29500 | 245 | | 575 | 31500 | 255 | | 600 | 37000 | 305 |
  11. 11. OLBATI © 2016 - Geek Time November 2016 11 Gherkin ● Example A Scenario Outline section is always followed by one or more Examples sections, which are a container for a table. The table must have a header row corresponding to the variables in the Scenario Outline steps. Each of the rows below will create a new Scenario, filling in the variable values.
  12. 12. OLBATI © 2016 - Geek Time November 2016 12 Gherkin ● Step Arguments In some cases you might want to pass a larger chunk of text or a table of data to a step---something that doesn't fit on a single line. For this purpose Gherkin has Doc Strings and Data Tables. Given a blog post named "Random" with Markdown body """ Some Title, Eh? =============== Here is the first paragraph of my blog post. Lorem ipsum dolor sit amet, consectetur adipiscing elit. """
  13. 13. OLBATI © 2016 - Geek Time November 2016 13 Gherkin Given the following users exist: | name | email | twitter | | Aslak | aslak@cucumber.io | @aslak_hellesoy | | Julien | julien@cucumber.io | @jbpros | | Matt | matt@cucumber.io | @mattwynne |
  14. 14. OLBATI © 2016 - Geek Time November 2016 14 Gherkin ● Tags Tags are a way to group Scenarios. They are @-prefixed strings and you can place as many tags as you like above Feature, Scenario, Scenario Outline or Examples keywords. Space character are invalid in tags and may separate them. Tags are inherited from parent elements. For example, if you place a tag above a Feature, all scenarios in that feature will get that tag.
  15. 15. OLBATI © 2016 - Geek Time November 2016 15 Gherkin @tag1 @tag2 @tag3 Feature: name @RunWith(Cucumber.class) @Cucumber.Options(format = {"pretty", "html:target/cucumber"}, tags = {"~@tag1"}) public class runTest { }
  16. 16. OLBATI © 2016 - Geek Time November 2016 16 Gherkin ● Comment we just need to put # before beginning your comment.
  17. 17. OLBATI © 2016 - Geek Time November 2016 17 Step Definitions Cucumber doesn't know how to execute your scenarios out-of-the-box. It needs Step Definitions to translate plain text Gherkin steps into actions that will interact with the system.
  18. 18. OLBATI © 2016 - Geek Time November 2016 18 Step Definitions public class OnesNumbersSteps { @Given("....(d+)$") public void the_menu_contains_the_following_dishes(final Integer quantity) {} @When("...") public void the_customer_order() {} @Then("^...$") public void the_result_should_be_I(){} }
  19. 19. OLBATI © 2016 - Geek Time November 2016 19 Cucumber implementations ■ Ruby/JRuby ■ JRuby (using Cucumber-JVM) ■ Java ■ Groovy ■ JavaScript ■ JavaScript (using Cucumber-JVM and Rhino) ■ Clojure ■ Gosu ■ Lua ■ .NET (using SpecFlow) ■ PHP (using Behat) ■ Jython ■ C++ ■ Tcl
  20. 20. OLBATI © 2016 - Geek Time November 2016 20 Cucumber-JVM ● Maven dependency <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.2.4-SNAPSHOT</version> <scope>test</scope> </dependency>
  21. 21. OLBATI © 2016 - Geek Time November 2016 21 Running Cucumber ● JUnit Runner import cucumber.api.junit.Cucumber; import org.junit.runner.RunWith; @RunWith(Cucumber.class) public class RunCukesTest { }
  22. 22. OLBATI © 2016 - Geek Time November 2016 22 Running Cucumber ● CLI Runner mvn test -Dcucumber.options="--help" will print out: Usage: java cucumber.api.cli.Main [options] [[[FILE|DIR][:LINE[:LINE]*] ]+ | @FILE ]
  23. 23. OLBATI © 2016 - Geek Time November 2016 23 Running Cucumber ● Third party runners IntelliJ IDEA and Eclipse have plugins that can run scenarios from within an IDE: ■ IntelliJ IDEA ■ Cucumber-Eclipse
  24. 24. OLBATI © 2016 - Geek Time November 2016 24 Kata : Arabic To Roman Numerals 1 = I 2 = II 3 = III 4 = IV 5 = V 6 = VI 7 = VII 8 = VIII 9 = IX 10 = X 20 = XX 30 = XXX 40 = XL 50 = L 60 = LX 70 = LXX 80 = LXXX 90 = XC 100 = C 500 = D 1000 = M 2000 = MM
  25. 25. OLBATI © 2016 - Geek Time November 2016 25 Thanks! Any questions? halleb.khaled@gmail.com

×