Top Banner
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
65
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Goal

Introduce Cucumber for Java

Page 3: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Tools are useless

If you don't know how to use them

Page 4: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Outline

Why

What

How

Page 5: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Why

Communication

Page 6: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Bandwidth

Low High

Page 7: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

A specification

Page 8: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

A specification

Page 9: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Examples

Create concrete examples

Mark Twain:

There is nothing so annoying as a good example

Page 10: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Executable

How should the examples be expressed?

Page 11: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

@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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf
Page 15: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Regression testing

Can we deliver?

Page 16: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Living documentation

These examples are actually working

Page 17: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

What

Page 18: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Behaviour Driven Development

Page 19: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Behaviour Driven Development

Page 20: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Black box

Page 21: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

High level

Not always end to end

Not always through the GUI

Page 22: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Common language

Used by all involved

Customer

Developers

Testers

Page 23: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

How

Page 25: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Tools

Technical Non technical

JBehaveTumbler

JUnit robotframework

FitNesse

Page 26: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

The right tool for the job

Page 27: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Audience

Readers

Customers

Developers

Maintainers

Product owner

Developers

Page 28: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Pattern

Specification

System under test, SUT

Steps – Glue (Native code)

Execute SUT Assert SUT

ExecuteSpecification

Page 30: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Gherkin

Small API

Features

Scenarios

Background

Scenario outlines

Translated to 47 languages

Page 31: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Keywords

Given – setup SUT

When – execute SUT

Then – verify SUT

And, But – flow to the language

Page 32: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Advantages

Easy to read

Easy to understand

Easy to discuss

Easy to parse

Page 34: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Features

Order not relevant

When

Given

Then

Page 35: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Backgrounds

Executed before each scenario

Powerful

Page 36: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Scenario outline

Substitute keywords with values from a table

Each row in the table makes up one scenario

Page 37: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Hooks

@Before

@After

Executed before or after each step

Very often

Page 40: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Drivers

JUnit

Command line

Page 42: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Continuous Integration

Maven

Ant

Page 43: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Example

A Maven project

Simple model

Use a Continuous Integration server

Extend to a web application

Page 44: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Live coding

Page 45: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Workflow

Page 47: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

1. Describe the behaviour in plain text

Page 48: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

2. Write a step definition in Ruby

Page 49: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

2. Write a step definition in Ruby Java

Page 50: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

3. Run it and watch it fail

Page 51: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

4. Write code to make the step pass

Page 52: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

5. Run it again and see the step pass

Page 53: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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

Page 54: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Why

Communication

Regression testing

Living documentation

Page 56: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

What

Behaviour

Page 57: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

How

Features

Scenarios

Glue code

System under test

Page 58: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Continuous Integration

Preparation for fast deployment

It works as we have specified

Page 59: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Declarative

Don't write scripts

Define what should work

No implementation details

Page 60: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Cucumber is a good hammer

Page 61: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

All problems are not nails

Page 62: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Right tool for the right problem

Page 63: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

Do not focus on the tools

Tools will never solve the problem

A fool with a tool is still a fool

Page 64: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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: jf13_BehaviourDrivenDevelopmentWithCucumber.pdf

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