Behaviour-driven development and acceptance tests on Java web applications

90
and acceptance tests on Java web applications Behaviour-driven development... Francisco Souza @franciscosouza franciscosouza.net giran.com.br

description

Internal BDD talk at Giran (http://www.giran.com.br). Source code at: http://github.com/franciscosouza/java-bdd-talk

Transcript of Behaviour-driven development and acceptance tests on Java web applications

Page 1: Behaviour-driven development and acceptance tests on Java web applications

and acceptance tests on Java web applications

Behaviour-driven development...

Francisco [email protected]

Page 2: Behaviour-driven development and acceptance tests on Java web applications

Tests...

Hey, developer!

Page 3: Behaviour-driven development and acceptance tests on Java web applications

how did we work?

Page 4: Behaviour-driven development and acceptance tests on Java web applications

Manual testing

http://www.flickr.com/photos/seandreilinger/2154168478/

Page 5: Behaviour-driven development and acceptance tests on Java web applications

http://www.flickr.com/photos/pusspaw/745074637/

Annoying

Page 6: Behaviour-driven development and acceptance tests on Java web applications

http://www.flickr.com/photos/40045029@N07/4641262811/

Mecanic

Page 7: Behaviour-driven development and acceptance tests on Java web applications
Page 8: Behaviour-driven development and acceptance tests on Java web applications
Page 9: Behaviour-driven development and acceptance tests on Java web applications
Page 10: Behaviour-driven development and acceptance tests on Java web applications
Page 11: Behaviour-driven development and acceptance tests on Java web applications

Don't you like it?

Page 12: Behaviour-driven development and acceptance tests on Java web applications

Don't you like it?

Do it better!

Page 13: Behaviour-driven development and acceptance tests on Java web applications

Selenium

http://www.flickr.com/photos/statusfrustration/143040265/

Page 14: Behaviour-driven development and acceptance tests on Java web applications

Automatization

Page 15: Behaviour-driven development and acceptance tests on Java web applications

Selenium IDE

Page 16: Behaviour-driven development and acceptance tests on Java web applications

Selenium RC

Page 17: Behaviour-driven development and acceptance tests on Java web applications

TDD

Page 18: Behaviour-driven development and acceptance tests on Java web applications

test

Page 19: Behaviour-driven development and acceptance tests on Java web applications

test

first?

Page 20: Behaviour-driven development and acceptance tests on Java web applications

how did we work?

Page 21: Behaviour-driven development and acceptance tests on Java web applications

As a user I want to see

the page title“Google”

on www.google.com.br.

Specification

Page 22: Behaviour-driven development and acceptance tests on Java web applications

Tests?

Page 23: Behaviour-driven development and acceptance tests on Java web applications

a test is a runnable specification

Page 24: Behaviour-driven development and acceptance tests on Java web applications

You can leave your post-it and use case specification in a outdated box

Page 25: Behaviour-driven development and acceptance tests on Java web applications

You can leave your post-it and use case specification in a outdated box

but you have to update your tests!

Page 26: Behaviour-driven development and acceptance tests on Java web applications

You can leave your post-it and use case specification in a outdated box

Agreed!

but you have to update your tests!

Page 27: Behaviour-driven development and acceptance tests on Java web applications

show me the code!

Page 28: Behaviour-driven development and acceptance tests on Java web applications

show me the code!

package net.franciscosouza.selenium;

import com.thoughtworks.selenium.*;

public class TestGoogleHomePage extends SeleneseTestCase { public void setUp() throws Exception { setUp("http://www.google.com.br/", "*chrome"); } public void testGoogleTitle() throws Exception { selenium.open("http://www.google.com.br"); assertEquals("Google", selenium.getTitle()); }}

Page 29: Behaviour-driven development and acceptance tests on Java web applications

http://www.flickr.com/photos/zanini/4924308668/

Ugly!

Page 30: Behaviour-driven development and acceptance tests on Java web applications

and the customer?

Page 31: Behaviour-driven development and acceptance tests on Java web applications

As a user I want to see

the page title“Google”

on www.google.com.br.

Page 32: Behaviour-driven development and acceptance tests on Java web applications

Given

Page 33: Behaviour-driven development and acceptance tests on Java web applications

Given

that I am on the browser start page

Page 34: Behaviour-driven development and acceptance tests on Java web applications

When

Given

that I am on the browser start page

Page 35: Behaviour-driven development and acceptance tests on Java web applications

WhenI go to the website www.google.com.br

Given

that I am on the browser start page

Page 36: Behaviour-driven development and acceptance tests on Java web applications

Then

WhenI go to the website www.google.com.br

that I am on the browser start page

Given

Page 37: Behaviour-driven development and acceptance tests on Java web applications

the title of page should be “Google”

Then

WhenI go to the website www.google.com.br

that I am on the browser start page

Given

Page 38: Behaviour-driven development and acceptance tests on Java web applications

is it possible?

Page 39: Behaviour-driven development and acceptance tests on Java web applications

http://www.flickr.com/photos/nocas/12555343/

JBehave

Page 40: Behaviour-driven development and acceptance tests on Java web applications

01. Write the story

Page 41: Behaviour-driven development and acceptance tests on Java web applications

01. Write the story

Given that I am on the browser start page

When I go to the website www.google.com.br

Then the title of page should be "Google"

Page 42: Behaviour-driven development and acceptance tests on Java web applications

02. Define the steps

Page 43: Behaviour-driven development and acceptance tests on Java web applications

02. Define the stepspublic class GoogleSteps extends SeleneseTestCase {

@Given("that I am on the browser start page") public void openTheBrowser() throws Exception { this.setUp("http://www.google.com.br", "*chrome"); } @When("I go to the website www.google.com.br") public void goToGoogleHomePage() { this.selenium.open("/"); } @Then("the window title should be Google") public void testTitle() { assertEquals("Google", this.selenium.getTitle()); } }

Page 44: Behaviour-driven development and acceptance tests on Java web applications

03. Step runner

Page 45: Behaviour-driven development and acceptance tests on Java web applications

03. Step runnerpublic class GoogleTitle extends JUnitStory {

@Override public Configuration configuration() { return new MostUsefulConfiguration() .useStoryLoader(new LoadFromClasspath(this.getClass().getClassLoader())) .useStoryReporterBuilder(new StoryReporterBuilder().withDefaultFormats().withFormats(Format.CONSOLE, Format.TXT)); } @Override public List<CandidateSteps> candidateSteps() { return new InstanceStepsFactory(this.configuration(), new GoogleSteps()).createCandidateSteps(); } }

Page 46: Behaviour-driven development and acceptance tests on Java web applications

04. Run Forest run!

Page 47: Behaviour-driven development and acceptance tests on Java web applications

04. Run Forest run!

Didn't work =/

Page 48: Behaviour-driven development and acceptance tests on Java web applications

What is the problem with JBehave?

Page 49: Behaviour-driven development and acceptance tests on Java web applications

What is the problem with JBehave?

Dependencies

Page 50: Behaviour-driven development and acceptance tests on Java web applications

i18n

What is the problem with JBehave?

Dependencies

Page 51: Behaviour-driven development and acceptance tests on Java web applications

i18n

I couldn't get this on my machine =/

What is the problem with JBehave?

Dependencies

Page 52: Behaviour-driven development and acceptance tests on Java web applications

Boring

http://www.flickr.com/photos/mein_arkengel/3728122696/

Page 53: Behaviour-driven development and acceptance tests on Java web applications

Have fun?

http://www.flickr.com/photos/torimercedes/2837826814/

Page 54: Behaviour-driven development and acceptance tests on Java web applications

You can have fun with BDD :)

Page 55: Behaviour-driven development and acceptance tests on Java web applications

You can have fun with BDD :)

And you should!

Page 56: Behaviour-driven development and acceptance tests on Java web applications

Gherkin Syntax

Page 57: Behaviour-driven development and acceptance tests on Java web applications

Cucumber

Page 58: Behaviour-driven development and acceptance tests on Java web applications

Lettuce

Page 59: Behaviour-driven development and acceptance tests on Java web applications

Ruby?

Page 60: Behaviour-driven development and acceptance tests on Java web applications

Ruby?

Python?

Page 61: Behaviour-driven development and acceptance tests on Java web applications

Ruby?

Python?

I am a Java developer!

Page 62: Behaviour-driven development and acceptance tests on Java web applications

Ruby?

Python?

I am a Java developer!

J

Page 63: Behaviour-driven development and acceptance tests on Java web applications

Ruby?

Jython?

I am a Java developer!

J

Page 64: Behaviour-driven development and acceptance tests on Java web applications

is it possible?

Page 65: Behaviour-driven development and acceptance tests on Java web applications

Cucumber

Page 66: Behaviour-driven development and acceptance tests on Java web applications

01. Install JRuby

$ rvm install jruby$ rvm jruby

Page 67: Behaviour-driven development and acceptance tests on Java web applications

02. Install Cucumber and Selenium

$ gem install cucumber$ gem install selenium-client

Page 68: Behaviour-driven development and acceptance tests on Java web applications

03. Write a feature

Feature: Google homepage testing

Scenario: Title of Google homepageGiven that I am on the browser start pageWhen I go to the website www.google.com.brThen the title of page should be "Google"

Page 69: Behaviour-driven development and acceptance tests on Java web applications

Understand:

A feature has many scenarios.

A scenario has many steps.

Page 70: Behaviour-driven development and acceptance tests on Java web applications

04. Define the steps using Ruby and Selenium

Page 71: Behaviour-driven development and acceptance tests on Java web applications

05. Run the tests

$ cucumber

Page 72: Behaviour-driven development and acceptance tests on Java web applications

http://www.flickr.com/photos/smull/356355686/

Couldn't I write step definitions

in Java?

Page 73: Behaviour-driven development and acceptance tests on Java web applications

You can! ;)

Page 74: Behaviour-driven development and acceptance tests on Java web applications

Cuke4Duke

http://www.flickr.com/photos/sharynmorrow/4860775922/

Page 75: Behaviour-driven development and acceptance tests on Java web applications

You can use Maven, Ant or command line

Page 76: Behaviour-driven development and acceptance tests on Java web applications

You can use Maven, Ant or command line

I like command line :)

Page 77: Behaviour-driven development and acceptance tests on Java web applications

01. Install cuke4duke gem

$ gem install cuke4duke

Page 78: Behaviour-driven development and acceptance tests on Java web applications

02. Add the JARs to your project

http://cukes.info/maven/cuke4duke/cuke4duke/

cuke4duke.jar

picocontainer.jar

Page 79: Behaviour-driven development and acceptance tests on Java web applications

Advertise:

Maven is the best way to add and manage cuke4duke in your project ;)

http://cukes.info/maven/

Page 80: Behaviour-driven development and acceptance tests on Java web applications

03. Write copy the feature

Feature: Google homepage testing

Scenario: Title of Google homepageGiven that I am on the browser start pageWhen I go to the website www.google.com.brThen the title of page should be "Google"

Page 81: Behaviour-driven development and acceptance tests on Java web applications

04. Define the steps using Java and Selenium

public class GoogleHomePageSteps extends SeleneseTestCase { @Before() public void setUpSelenium() throws Exception { this.setUp("http://www.google.com.br", "*chrome"); } @Given("that I am on the browser start page") public void openBrowser() { this.selenium.open("/"); } @When("I go to the website www.google.com.br") public void goToGoogle() { this.selenium.open("http://www.google.com.br"); } ...

Page 82: Behaviour-driven development and acceptance tests on Java web applications

04. Define the steps using Java and Selenium

@Then("the title of page should be \"(.*)\"") public void checkTitle(String title) { assertEquals(title, this.selenium.getTitle()); } @After public void closeSelenium() { this.selenium.close(); } }

Page 83: Behaviour-driven development and acceptance tests on Java web applications

05. Run Forest run!$ cuke4duke --jars lib --require bin/ features

Page 84: Behaviour-driven development and acceptance tests on Java web applications

05. Run Forest run!$ cuke4duke --jars lib --require bin/ features

Folder containing your .jar files

Folder containing your .class files

Folder containing your .feature files

Page 85: Behaviour-driven development and acceptance tests on Java web applications

http://www.flickr.com/photos/78617484@N00/2656184369/

i18n

Page 86: Behaviour-driven development and acceptance tests on Java web applications

# language: ptFuncionalidade: Teste da página inicial do Google

Cenário: Título da página inicial do GoogleDado que estou na página inicial do navegadorQuando eu vou para a o site www.google.com.brEntão o título da página deve ser "Google"

Portuguese ;)

Page 87: Behaviour-driven development and acceptance tests on Java web applications

Cucumber actually (2010 Sep 11) spoken

languages

45

Page 88: Behaviour-driven development and acceptance tests on Java web applications

Stop the manual testing right now!

Page 89: Behaviour-driven development and acceptance tests on Java web applications

Stop the manual testing right now!

Enjoy Cucumber :)

Page 90: Behaviour-driven development and acceptance tests on Java web applications

Francisco Souza@franciscosouza

www.franciscosouza.net