Behaviour Driven
Developmentusing Cucumber JVM and Groovy
About Me
Marco Vermeulen
Java Dev for past 10 years
Groovy and Grails for 5
Worked for Shazam, Associated Newspapers, Burb...
About the Talk
BDD in a Nutshell
The Good and Bad of BDD
Cucumber as solution
Mini Cucumber Demo
Grails Cucumber Plugin
Ex...
BDD Defined
Straight from the horse's mouth:
#BDD in a tweet: Using examples at multiple levels to create a
shared underst...
BDD in a Nutshell
Using Examples
at multiple levels
to create a shared understanding
and surface uncertainty
to deliver so...
So???Collaborate on a Specification that all understand.
Write in light weight markup called Gherkin.
Make Executable Spec...
Bad Wrap
An Orphan
Marketing Hype
ATDD (Acceptance Test Driven Development)
Lost it's Zing!!!
Good Vibrations
TDD Evolved
Inspires Collaboration
Behaviour vs Implementation
Living Documentation
Abundance of Tools
Sim...
Abundance of Tools
JBehave
Concordian
Fitnesse
EasyB
Spock?
Jasmine (for JavaScript)
Cucumber
Cucumber and
Gradle
Cucumber and Gradle
An Anatomy
Dependencies
JUnit Test Runner
Gherkin
Step Definitions
Hooks
Tags
Cucumber Anatomy
Dependencies
build.gradle
repositories {
mavenCentral()
}
dependencies {
groovy 'org.codehaus.groovy:groo...
Cucumber Anatomy
Test Runner
src/test/groovy/RunCukeTests.groovy
import cucumber.api.junit.Cucumber
import org.junit.runne...
Cucumber Anatomy
Gherkin
src/test/cucumber/adding.feature
Feature: Calculate
Scenario: Add two numbers
Given the input "2+...
Cucumber Anatomy
Step Definitions
src/test/cucumber/steps/add_steps.groovy
import static cucumber.api.groovy.EN.*
Given(~'...
Cucumber Anatomy
Hooks
src/test/cucumber/support/env.groovy
Before and After each Scenario
import static cucumber.api.groo...
Mini Demo
Grails Cucumber Plugin
by Martin Hauner
Plugin Portal
https://github.com/hauner/grails-cucumber
Grails Cucumber Plugin
Features
Convention over Configuration
Easy Configuration
No Test Runner
Uses Functional Test phase...
Grails Cucumber
ExampleGVM admin console
Contrived example!!!
Walking skeleton
All moving parts of setup
Grails Cucumber Example
Configuration
grails-app/conf/BuildConfig.groovy
plugins {
...
test ":cucumber:0.8.0"
...
}
Grails Cucumber Example
Configuration
grails-app/conf/CucumberConfig.groovy
Replaces Test Runner .
cucumber {
tags = ["~@m...
Grails Cucumber Example
Test Folder Structure
Grails Cucumber Example
Gherkin Feature
test/cucumber/manage_candidates.feature
Feature: Manage Candidates
Scenario: List ...
Grails Cucumber Example
Step Definitions
test/cucumber/steps/manage_candidates.groovy
Gorm!
Geb!
Given(~'^the candidate "(...
Grails Cucumber Example
Domain Class
grails-app/domain/../Candidate.groovy
class Candidate {
String name
String defaultVer...
Grails Cucumber Example
Geb Configuration
test/functional/GebConfig.groovy
Remember to chromedriver!
Place it in root of p...
Grails Cucumber Example
Geb Page
test/functional/page/CandidatePage.groovy
class CandidatePage extends Page {
static url =...
Grails Cucumber Example
Geb Module
test/functional/modules/CandidatePage.groovy
class CandidateModule extends Module{
stat...
Grails Cucumber Example
Environment Hooks
test/cucumber/support/env.groovy
Use for any long running fixture
Hooks run befo...
Grails Cucumber Example
Hooks: Clean up GORM
test/cucumber/support/fixture.groovy
GORM's Hibernate session leaks across Sc...
Grails Cucumber Example
Spock Specification
@TestFor(CandidateController)
@Build([Candidate])
class CandidateControllerSpe...
Grails Cucumber Example
Controller
Controller
class CandidateController {
def list(Integer max) {
params.max = Math.min(ma...
Grails Cucumber Example
GSP
<g:each in="${candidateInstanceList}"
status="i" var="candidateInstance">
<tr>
<td id="${candi...
ConclusionBDD helps us Collaborate
BDD helps us make software that Matters!
Cucumber JVM and Gradle play nicely
Grails Cuc...
Thank You!!!
Q & A
greach 2014 marco vermeulen bdd using cucumber jvm and groovy
Upcoming SlideShare
Loading in...5
×

greach 2014 marco vermeulen bdd using cucumber jvm and groovy

675

Published on

greach 2014 marco vermeulen bdd using cucumber jvm and groovy slides

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
675
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

greach 2014 marco vermeulen bdd using cucumber jvm and groovy

  1. 1. Behaviour Driven Developmentusing Cucumber JVM and Groovy
  2. 2. About Me Marco Vermeulen Java Dev for past 10 years Groovy and Grails for 5 Worked for Shazam, Associated Newspapers, Burberry Currently: Consulting at Equal Experts Author of GVM (Groovy enVironment Manager) Twitter: http://wiredforcode.com @marcoVermeulen
  3. 3. About the Talk BDD in a Nutshell The Good and Bad of BDD Cucumber as solution Mini Cucumber Demo Grails Cucumber Plugin Example Application
  4. 4. BDD Defined Straight from the horse's mouth: #BDD in a tweet: Using examples at multiple levels to create a shared understanding and surface uncertainty to deliver software that matters 9:10 PM ­ 26 May 2013 Dan North  @tastapod FollowFollow 95 RETWEETS  54 FAVORITES    
  5. 5. BDD in a Nutshell Using Examples at multiple levels to create a shared understanding and surface uncertainty to deliver software that matters!
  6. 6. So???Collaborate on a Specification that all understand. Write in light weight markup called Gherkin. Make Executable Specifications by parsing the markup. Watch these new Pending unimplemented tests fail! Implement the tests, watching them fail. Write the Implementation, watch it pass!
  7. 7. Bad Wrap An Orphan Marketing Hype ATDD (Acceptance Test Driven Development) Lost it's Zing!!!
  8. 8. Good Vibrations TDD Evolved Inspires Collaboration Behaviour vs Implementation Living Documentation Abundance of Tools Simples!
  9. 9. Abundance of Tools JBehave Concordian Fitnesse EasyB Spock? Jasmine (for JavaScript) Cucumber
  10. 10. Cucumber and Gradle
  11. 11. Cucumber and Gradle An Anatomy Dependencies JUnit Test Runner Gherkin Step Definitions Hooks Tags
  12. 12. Cucumber Anatomy Dependencies build.gradle repositories { mavenCentral() } dependencies { groovy 'org.codehaus.groovy:groovy:2.1.5' testCompile 'junit:junit:4.11' testCompile 'info.cukes:cucumber-groovy:1.1.1' testCompile 'info.cukes:cucumber-junit:1.1.1' }
  13. 13. Cucumber Anatomy Test Runner src/test/groovy/RunCukeTests.groovy import cucumber.api.junit.Cucumber import org.junit.runner.RunWith @RunWith(Cucumber) @Cucumber.Options( format=["pretty", "html:build/reports/cucumber"], strict=true, features=["src/test/cucumber"], glue=["src/test/cucumber/steps", "src/test/cucumber/support"], tags=["~@manual", "~@review"] ) public class RunCukesTest { //leave me empty! }
  14. 14. Cucumber Anatomy Gherkin src/test/cucumber/adding.feature Feature: Calculate Scenario: Add two numbers Given the input "2+2" When the calculator is run Then the output should be "4" Scenario: Subtract two numbers Given the input "9-4" When the calculator is run Then the output should be "5"
  15. 15. Cucumber Anatomy Step Definitions src/test/cucumber/steps/add_steps.groovy import static cucumber.api.groovy.EN.* Given(~'^the input "([^"]*)"$') { String input -> //some groovy code } When(~'^the calculator is run$') { -> //some groovy code } Then(~'^the output should be "([^"]*)"$') { String output -> //some groovy code }
  16. 16. Cucumber Anatomy Hooks src/test/cucumber/support/env.groovy Before and After each Scenario import static cucumber.api.groovy.Hooks.* Before(){ //some groovy code } After(){ //some groovy code }
  17. 17. Mini Demo
  18. 18. Grails Cucumber Plugin by Martin Hauner Plugin Portal https://github.com/hauner/grails-cucumber
  19. 19. Grails Cucumber Plugin Features Convention over Configuration Easy Configuration No Test Runner Uses Functional Test phase Has Friends! (Geb, Spock, Build Test Data, Fixtures) Command Line integration Good IDE Support Under active development
  20. 20. Grails Cucumber ExampleGVM admin console Contrived example!!! Walking skeleton All moving parts of setup
  21. 21. Grails Cucumber Example Configuration grails-app/conf/BuildConfig.groovy plugins { ... test ":cucumber:0.8.0" ... }
  22. 22. Grails Cucumber Example Configuration grails-app/conf/CucumberConfig.groovy Replaces Test Runner . cucumber { tags = ["~@manual"] features = ["test/cucumber"] strict = true glue = ["test/cucumber/steps", "test/cucumber/support"] } configuration
  23. 23. Grails Cucumber Example Test Folder Structure
  24. 24. Grails Cucumber Example Gherkin Feature test/cucumber/manage_candidates.feature Feature: Manage Candidates Scenario: List Candidates Given the candidate "Grails" exists with default version "2.2.2" And the candidate "Groovy" exists with default version "2.1.4" When I visit the Candidate page Then I see "Grails" listed And I see "Groovy" listed
  25. 25. Grails Cucumber Example Step Definitions test/cucumber/steps/manage_candidates.groovy Gorm! Geb! Given(~'^the candidate "([^"]*)" exists with default version "([^"]*)"$'){ St def candidate = new Candidate( name: name, defaultVersion: defaultVersion ) assert candidate.save() } When(~'^I visit the Candidate page$') { -> to CandidatePage at CandidatePage } Then(~'^I see "([^"]*)" listed$') { String candidateName -> assert page.isCandidateInList(candidateName) }
  26. 26. Grails Cucumber Example Domain Class grails-app/domain/../Candidate.groovy class Candidate { String name String defaultVersion }
  27. 27. Grails Cucumber Example Geb Configuration test/functional/GebConfig.groovy Remember to chromedriver! Place it in root of project folder. ...or use any selenium driver you like. import org.openqa.selenium.chrome.ChromeDriver driver = { new ChromeDriver() } download
  28. 28. Grails Cucumber Example Geb Page test/functional/page/CandidatePage.groovy class CandidatePage extends Page { static url = "candidate/list" static at = { title ==~ /Candidate List/ } static content = { candidateList { $("div.list table", 0) } candidate { candidateName -> module CandidateModule, $("#$candidateName") } } def isCandidateInList(String candidateName){ def candidateRow = candidate(candidateName) return candidateRow ? true : false } }
  29. 29. Grails Cucumber Example Geb Module test/functional/modules/CandidatePage.groovy class CandidateModule extends Module{ static content = { name { $("#name") } defaultVersion { $("#default") } } }
  30. 30. Grails Cucumber Example Environment Hooks test/cucumber/support/env.groovy Use for any long running fixture Hooks run before and after each scenario Like @BeforeClass and @AfterClass in JUnit Before () { bindingUpdater = new BindingUpdater (binding, new Browser ()) bindingUpdater.initialize () } After () { bindingUpdater.remove () }
  31. 31. Grails Cucumber Example Hooks: Clean up GORM test/cucumber/support/fixture.groovy GORM's Hibernate session leaks across Scenarios. Feels sleezy :-P After () { def sessionFactory = appCtx.getBean("sessionFactory") sessionFactory.currentSession.flush() def dataSource = appCtx.getBean("dataSource") //clean fixtures println "Deleting the fixture..." def db = new Sql(dataSource) db.execute("DELETE FROM CANDIDATE;") sessionFactory.currentSession.clear() }
  32. 32. Grails Cucumber Example Spock Specification @TestFor(CandidateController) @Build([Candidate]) class CandidateControllerSpec extends Specification { void "should return a list of available candidates"(){ given: def grails = new Candidate(name:'grails',defaultVersion:'2.2.2').sav def groovy = new Candidate(name:'groovy',defaultVersion:'2.1.5').sav when: def result = controller.list() then: result.candidateInstanceList.contains grails result.candidateInstanceList.contains groovy } }
  33. 33. Grails Cucumber Example Controller Controller class CandidateController { def list(Integer max) { params.max = Math.min(max ?: 10, 100) [candidateInstanceList: Candidate.list(params), candidateInstanceTotal: Candidate.count()] } }
  34. 34. Grails Cucumber Example GSP <g:each in="${candidateInstanceList}" status="i" var="candidateInstance"> <tr> <td id="${candidateInstance.name}"> ${fieldValue(bean: candidateInstance, field: "name")} </td> <td> <g:link action="show" id="${candidateInstance.id}"> ${fieldValue(bean: candidateInstance, field: "defaultVersion")} </g:link> </td> </tr> </g:each>
  35. 35. ConclusionBDD helps us Collaborate BDD helps us make software that Matters! Cucumber JVM and Gradle play nicely Grails Cucumber plugin Rocks! BDD is lots of fun!
  36. 36. Thank You!!!
  37. 37. Q & A
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×