Geek Time Novembre 2016 : Cucumber

25
Title geek time - Novembre 2016 HALLEB Khaled OLBATI Consultant

Transcript of Geek Time Novembre 2016 : Cucumber

Page 1: Geek Time Novembre 2016 : Cucumber

Titlegeek time - Novembre 2016

HALLEB KhaledOLBATI Consultant

Page 2: Geek Time Novembre 2016 : Cucumber

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

Page 3: Geek Time Novembre 2016 : Cucumber

OLBATI © 2016 - Geek Time November 2016 3

Cucumber and BDD ?● Cucumber is for Behaviour-Driven Development

Page 4: Geek Time Novembre 2016 : Cucumber

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

Page 5: Geek Time Novembre 2016 : Cucumber

OLBATI © 2016 - Geek Time November 2016 5

Gherkin

● Description

Feature: <feature title>

As a <persona|role>

I want to <action>

So that <outcome>

Page 6: Geek Time Novembre 2016 : Cucumber

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.

Page 7: Geek Time Novembre 2016 : Cucumber

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

Page 8: Geek Time Novembre 2016 : Cucumber

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

Page 9: Geek Time Novembre 2016 : Cucumber

OLBATI © 2016 - Geek Time November 2016 9

Gherkin

● Scenario OutlineWhen 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

#

Page 10: Geek Time Novembre 2016 : Cucumber

OLBATI © 2016 - Geek Time November 2016 10

GherkinScenario 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 |

Page 11: Geek Time Novembre 2016 : Cucumber

OLBATI © 2016 - Geek Time November 2016 11

Gherkin

● ExampleA 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.

Page 12: Geek Time Novembre 2016 : Cucumber

OLBATI © 2016 - Geek Time November 2016 12

Gherkin

● Step ArgumentsIn 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.

"""

Page 13: Geek Time Novembre 2016 : Cucumber

OLBATI © 2016 - Geek Time November 2016 13

Gherkin

Given the following users exist:

| name | email | twitter |

| Aslak | [email protected] | @aslak_hellesoy |

| Julien | [email protected] | @jbpros |

| Matt | [email protected] | @mattwynne |

Page 14: Geek Time Novembre 2016 : Cucumber

OLBATI © 2016 - Geek Time November 2016 14

Gherkin

● TagsTags 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.

Page 15: Geek Time Novembre 2016 : Cucumber

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 { }

Page 16: Geek Time Novembre 2016 : Cucumber

OLBATI © 2016 - Geek Time November 2016 16

Gherkin

● Comment we just need to put # before beginning your comment.

Page 17: Geek Time Novembre 2016 : Cucumber

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.

Page 18: Geek Time Novembre 2016 : Cucumber

OLBATI © 2016 - Geek Time November 2016 18

Step Definitionspublic 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(){}

}

Page 19: Geek Time Novembre 2016 : Cucumber

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

Page 20: Geek Time Novembre 2016 : Cucumber

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>

Page 21: Geek Time Novembre 2016 : Cucumber

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 {

}

Page 22: Geek Time Novembre 2016 : Cucumber

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 ]

Page 23: Geek Time Novembre 2016 : Cucumber

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

Page 24: Geek Time Novembre 2016 : Cucumber

OLBATI © 2016 - Geek Time November 2016 24

Kata : Arabic To Roman Numerals

1 = I2 = II3 = III4 = IV5 = V6 = VI7 = VII

8 = VIII 9 = IX

10 = X20 = XX30 = XXX40 = XL50 = L60 = LX70 = LXX80 = LXXX90 = XC

100 = C500 = D

1000 = M2000 = MM

Page 25: Geek Time Novembre 2016 : Cucumber

OLBATI © 2016 - Geek Time November 2016 25

Thanks!Any questions?

[email protected]