Behaviour-driven development and acceptance tests on Java web applications
-
Upload
francisco-souza -
Category
Technology
-
view
3.731 -
download
0
description
Transcript of Behaviour-driven development and acceptance tests on Java web applications
and acceptance tests on Java web applications
Behaviour-driven development...
Francisco [email protected]
Tests...
Hey, developer!
how did we work?
Manual testing
http://www.flickr.com/photos/seandreilinger/2154168478/
http://www.flickr.com/photos/pusspaw/745074637/
Annoying
http://www.flickr.com/photos/40045029@N07/4641262811/
Mecanic
Don't you like it?
Don't you like it?
Do it better!
Selenium
http://www.flickr.com/photos/statusfrustration/143040265/
Automatization
Selenium IDE
Selenium RC
TDD
test
test
first?
how did we work?
As a user I want to see
the page title“Google”
on www.google.com.br.
Specification
Tests?
a test is a runnable specification
You can leave your post-it and use case specification in a outdated box
You can leave your post-it and use case specification in a outdated box
but you have to update your tests!
You can leave your post-it and use case specification in a outdated box
Agreed!
but you have to update your tests!
show me the code!
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()); }}
http://www.flickr.com/photos/zanini/4924308668/
Ugly!
and the customer?
As a user I want to see
the page title“Google”
on www.google.com.br.
Given
Given
that I am on the browser start page
When
Given
that I am on the browser start page
WhenI go to the website www.google.com.br
Given
that I am on the browser start page
Then
WhenI go to the website www.google.com.br
that I am on the browser start page
Given
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
is it possible?
http://www.flickr.com/photos/nocas/12555343/
JBehave
01. Write the story
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"
02. Define the steps
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()); } }
03. Step runner
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(); } }
04. Run Forest run!
04. Run Forest run!
Didn't work =/
What is the problem with JBehave?
What is the problem with JBehave?
Dependencies
i18n
What is the problem with JBehave?
Dependencies
i18n
I couldn't get this on my machine =/
What is the problem with JBehave?
Dependencies
Boring
http://www.flickr.com/photos/mein_arkengel/3728122696/
Have fun?
http://www.flickr.com/photos/torimercedes/2837826814/
You can have fun with BDD :)
You can have fun with BDD :)
And you should!
Gherkin Syntax
Cucumber
Lettuce
Ruby?
Ruby?
Python?
Ruby?
Python?
I am a Java developer!
Ruby?
Python?
I am a Java developer!
J
Ruby?
Jython?
I am a Java developer!
J
is it possible?
Cucumber
01. Install JRuby
$ rvm install jruby$ rvm jruby
02. Install Cucumber and Selenium
$ gem install cucumber$ gem install selenium-client
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"
Understand:
A feature has many scenarios.
A scenario has many steps.
04. Define the steps using Ruby and Selenium
05. Run the tests
$ cucumber
http://www.flickr.com/photos/smull/356355686/
Couldn't I write step definitions
in Java?
You can! ;)
Cuke4Duke
http://www.flickr.com/photos/sharynmorrow/4860775922/
You can use Maven, Ant or command line
You can use Maven, Ant or command line
I like command line :)
01. Install cuke4duke gem
$ gem install cuke4duke
02. Add the JARs to your project
http://cukes.info/maven/cuke4duke/cuke4duke/
cuke4duke.jar
picocontainer.jar
Advertise:
Maven is the best way to add and manage cuke4duke in your project ;)
http://cukes.info/maven/
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"
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"); } ...
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(); } }
05. Run Forest run!$ cuke4duke --jars lib --require bin/ features
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
http://www.flickr.com/photos/78617484@N00/2656184369/
i18n
# 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 ;)
Cucumber actually (2010 Sep 11) spoken
languages
45
Stop the manual testing right now!
Stop the manual testing right now!
Enjoy Cucumber :)
Francisco Souza@franciscosouza
www.franciscosouza.net