behaviour driven development with cucumber for java … · behaviour driven development with...

65
Behaviour Driven Development with Cucumber for Java Thomas Sundberg Developer 20+ years Master Degree in Computer Science I write computer programs. @thomassundberg [email protected] http://thomassundberg.wordpress.com

Upload: dinhnhan

Post on 29-Aug-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Behaviour Driven Development with Cucumber for Java

Thomas Sundberg

Developer 20+ yearsMaster Degree in Computer Science

I write computer programs.

@[email protected]

http://thomassundberg.wordpress.com

Page 2: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Goal

Introduce Cucumber for Java

Page 3: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Tools are useless

If you don't know how to use them

Page 4: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Outline

Why

What

How

Page 5: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Why

Communication

Page 6: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Bandwidth

Low High

Page 7: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

A specification

Page 8: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

A specification

Page 9: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Examples

Create concrete examples

Mark Twain:

There is nothing so annoying as a good example

Page 10: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Executable

How should the examples be expressed?

Page 11: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Code

@Testpublic void orderLessThenAMonthBeforeChristmasShouldGiveZeroShipping() { Calendar purchaseDate = GregorianCalendar.getInstance(); purchaseDate.set(Calendar.YEAR, 2012); purchaseDate.set(Calendar.MONTH, Calendar.NOVEMBER); purchaseDate.set(Calendar.DAY_OF_MONTH, 24);

int christmasEve = 24; int expectedShipping = 0; String expectedCurrency = "euro";

Date purchase = purchaseDate.getTime(); Shipping shipping = new Shipping(purchase, christmasEve);

int actualCost = shipping.getCost();

assertThat(actualCost, is(expectedShipping));}

Page 12: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Scenario

Scenario: Free shipping a month before Christmas Given a customer that Christmas is celebrated 24 of December When a customer buys a book on 2012-12-10 Then the shipping cost should be 0 euro

Page 13: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

@Testpublic void orderLessThenAMonthBeforeChristmasShouldGiveZeroShipping() { Calendar purchaseDate = GregorianCalendar.getInstance(); purchaseDate.set(Calendar.YEAR, 2012); purchaseDate.set(Calendar.MONTH, Calendar.NOVEMBER); purchaseDate.set(Calendar.DAY_OF_MONTH, 24);

int christmasEve = 24; int expectedShipping = 0; String expectedCurrency = "euro";

Date purchase = purchaseDate.getTime(); Shipping shipping = new Shipping(purchase, christmasEve);

int actualCost = shipping.getCost();

assertThat(actualCost, is(expectedShipping));}

Scenario: Free shipping a month before Christmas Given a customer that Christmas is celebrated 24 of December When a customer buys a book on 2012-12-10 Then the shipping cost should be 0 euro

Which is easier to understand?

Page 14: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –
Page 15: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Regression testing

Can we deliver?

Page 16: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Living documentation

These examples are actually working

Page 17: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

What

Page 18: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Behaviour Driven Development

Page 19: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Behaviour Driven Development

Page 20: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Black box

Page 21: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

High level

Not always end to end

Not always through the GUI

Page 22: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Common language

Used by all involved

Customer

Developers

Testers

Page 23: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Three core principles

Business and Technology should refer to the same system in the same way

Any system should have an identified, verifiable value

Up-front analysis, design and planning all have a diminishing return

Page 24: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

How

Page 25: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Tools

Technical Non technical

JBehaveTumbler

JUnit robotframework

FitNesse

Page 26: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

The right tool for the job

Page 27: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Audience

Readers

Customers

Developers

Maintainers

Product owner

Developers

Page 28: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Why Cucumber

It is one of the least technical tools

It descends from RSpec

It is a very active open source project

Official release Mars 2012

It supports a variety of languages

Page 29: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Pattern

Specification

System under test, SUT

Steps – Glue (Native code)

Execute SUT Assert SUT

ExecuteSpecification

Page 30: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Gherkin

Small API

Features

Scenarios

Background

Scenario outlines

Translated to 47 languages

Page 31: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Keywords

Given – setup SUT

When – execute SUT

Then – verify SUT

And, But – flow to the language

Page 32: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Gherkin example

Feature: Hello World

Scenario: Say hello

Given I have a hello app with "Howdy"

When I ask it to say hi

Then it should answer with "Howdy World"

Page 33: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Advantages

Easy to read

Easy to understand

Easy to discuss

Easy to parse

Page 34: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Features

Order not relevant

When

Given

Then

Page 35: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Backgrounds

Executed before each scenario

Powerful

Page 36: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Scenario outline

Substitute keywords with values from a table

Each row in the table makes up one scenario

Page 37: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Steps – Glue

Java methods

Global

You can't describe two different things with the same words

Annotated with the keywords

Found by regular expressions

Page 38: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

An example

@When("^I add (\\d+) copies the (.*) book$")

public void methodName(int copies, String title) throws Throwable {

StepHelper stepHelper = new StepHelper();

stepHelper.addCopies(title, copies);

}

Page 39: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Hooks

@Before

@After

Executed before or after each step

Very often

Page 40: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Transformers

Transform a String to a class@When("^a customer buys a book on (.*)$")

public void methodName(@Format("yyyy-MM-dd") Date purchaseDate) throws Throwable {

this.purchaseDate = purchaseDate;

}

Page 41: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Drivers

JUnit

Command line

Page 42: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Continuous Integration

Maven

Ant

Page 43: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Example

A Maven project

Simple model

Use a Continuous Integration server

Extend to a web application

Page 44: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Live coding

Page 45: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Small example

All large systems consists of small pieces

You can only view a small portion of a system at one time

~30 – 50 loc

http://www.casualmiracles.com/2010/02/21/large-systems/

Page 46: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Workflow

Page 47: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

1. Describe the behaviour in plain text

Page 48: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

2. Write a step definition in Ruby

Page 49: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

2. Write a step definition in Ruby Java

Page 50: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

3. Run it and watch it fail

Page 51: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

4. Write code to make the step pass

Page 52: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

5. Run it again and see the step pass

Page 53: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

6. Repeat step 2 – 5 until green like a Cuke

Page 54: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

7. Repeat step 1 – 6 until the money runs out

1. Describe the behaviour in plain text

2. Write a step definition

3. Run it and watch it fail

4. Write code to make the step pass

5. Run it again and see the step pass

6. Repeat step 2 – 5 until green like a Cuke

Page 55: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Why

Communication

Regression testing

Living documentation

Page 56: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

What

Behaviour

Page 57: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

How

Features

Scenarios

Glue code

System under test

Page 58: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Continuous Integration

Preparation for fast deployment

It works as we have specified

Page 59: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Declarative

Don't write scripts

Define what should work

No implementation details

Page 60: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Cucumber is a good hammer

Page 61: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

All problems are not nails

Page 62: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Right tool for the right problem

Page 63: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Do not focus on the tools

Tools will never solve the problem

A fool with a tool is still a fool

Page 64: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Resources

Cucumber – http://cukes.info/

Cucumber – https://github.com/cucumber/cucumber/wiki

Gherkin – https://github.com/cucumber/cucumber/wiki/Gherkin

Selenium – http://seleniumhq.org/

Maven – http://maven.apache.org/

Jenkins – http://jenkins-ci.org/

Blog – http://thomassundberg.wordpress.com/

Page 65: Behaviour Driven Development with Cucumber for Java … · Behaviour Driven Development with Cucumber for Java Thomas Sundberg ... Selenium –

Behaviour Driven Development with Cucumber for Java

Thomas Sundberg

Developer 20+ yearsMaster Degree in Computer Science

I write computer programs.

@[email protected]

http://thomassundberg.wordpress.com