cucumber & cuke4duke with groovy and grails - lightning talk

20
/ cuke4duke lightning talk Aaron Murray [email protected]

Upload: wearefractal

Post on 19-Jun-2015

1.025 views

Category:

Technology


2 download

DESCRIPTION

Overview of using Cucumber via cuke4duke with Groovy and Grails

TRANSCRIPT

Page 1: Cucumber & cuke4duke with groovy and grails - lightning talk

/ cuke4dukelightning talk

Aaron [email protected]

Page 2: Cucumber & cuke4duke with groovy and grails - lightning talk

TDD, ATDD, BDD, DDD?That’s a lot of DD’s

• TDD = Test Driven Development – Development Methodology, write tests first, test using xUnit – make sure the code works

• ATDD = Acceptance Test Driven Development – business-readable, Team-driven, tests owned by client – make sure the features are correct

• BDD = Behavior Driven Development – Team-driven, “Should”, “Given, When, Then” – extends TDD and incorporates ATDD

• DDD = Domain Driven Design – outlines the idea of the Ubiquitous Language used between non-technical stakeholders and developers

Page 3: Cucumber & cuke4duke with groovy and grails - lightning talk

BDD Breakdown

Business-readable output– Replacement/Extension for TDD at the Unit-testing level– Artifacts owned by developers (code)– Provides other stakeholders with reports after developers have done the work– Gspec, Spock, EasyB

Business-readable input– Based on Acceptance Testing – coarse-grained features – Artifacts owned by client– Stakeholder involvement before developers have done any work– Cucumber, FitNesse

Not exclusive – can and probably need to be combined

Fundamentally Two Types of BDD Tools

Page 4: Cucumber & cuke4duke with groovy and grails - lightning talk

• Story-level BDD Tool written for RSpec by Aslak Hellesøy• Executes plain-text functional descriptions as automated tests• Gherkin is the language of Cucumber. It’s a Business

Readable DSL (structured native language input)• Consists of “feature” definitions written in Gherkin and “step”

definitions written in the language you are testing• “Tests” written first, and verified by non-technical

stakeholders at the story level before any code is written• Code then written to make the stories pass• Written in Ruby but available for the JVM, .NET, python etc.• Rich ecosystem & strong community

http://cukes.info/

Page 5: Cucumber & cuke4duke with groovy and grails - lightning talk

EasyB business-readable output

inputscenario "Two amounts with the same currencies are

added", { given "Two different amounts with the same

currencies", { money1 = new Money(12, "CHF") money2 = new Money(14, "CHF") expected = new Money(26, "CHF") } when "Add given amounts" , { result = money1.add(money2) } then "New amount is sum of two given ones", { result.equals(expected).shouldBe true }}… <<truncated>>

output

2 scenarios executed successfully.

Story: money

scenario Two amounts with the same currencies are added given Two different amounts with the same currencies when Add given amounts then New amount is sum of two given ones

scenario Two amounts with different currencies are added given Two amounts with different currencies when Add given amounts then Operation should fail

Page 6: Cucumber & cuke4duke with groovy and grails - lightning talk

Feature : Book search In order to find books I might buy As a potential customer I want to search for books by different criteria Background: Given the following books

 | Author  | Title | Year | Publisher   | Eric Evans | Domain Driven Design  | 2003 | Addison Wesley  | Gerard Meszaros | xUnit Test Patterns | 2007 | Addison Wesley    

Scenario: Search for title When I search for title 'Patterns' Then the result list should contain 1 books

business-readable input

Gherkin input:

Page 7: Cucumber & cuke4duke with groovy and grails - lightning talk

business-readable input

Output: something like this

Page 8: Cucumber & cuke4duke with groovy and grails - lightning talk

• Also created by Aslak Hellesøy• Runs on JRuby• Run Cucumber features from Ant, Maven, Command Line• Write step definitions in Java, Groovy, Scala, Clojure, Javascript, loke,

JRuby, Spring etc.• Test with WebDriver (or Selenium 2.0)• Debug cuke4duke steps with the remote debug functionality of Java

cuke4dukehttps://github.com/aslakhellesoy/cuke4duke/wiki

Page 9: Cucumber & cuke4duke with groovy and grails - lightning talk

3 ways: Ant, Maven, CLI

Easiest way to get started: Dan Lynn’s unpublished cucumber plugin for grails

$ git clone https://github.com/danklynn/grails-cucumber.git$ cd grails-cucumber$ grails install-plugin grails-cucumber-0.2.zip$ grails upgrade$ grails cucumber

+

Page 10: Cucumber & cuke4duke with groovy and grails - lightning talk

1. Write features in Business Readable plain text (*.feature)

2. Write step definition in language you’re testing + DSL (mystep.groovy)

3. Run & Fail4. Write code to make step pass5. Run and pass (winning)6. Repeat 2-5 for the rest of your feature

development cycle

Page 11: Cucumber & cuke4duke with groovy and grails - lightning talk

Step 1: write your featureAddition.feature

Feature: Addition In order to avoid silly mistakes As a math idiot I want to be told the sum of two numbers

Scenario: Add two numbers Given I have entered <input_1> into the calculator And I have entered <input_2> into the calculator When I press <button> Then the result should be <output> on the screen

Examples: | input_1 | input_2 | button | output | | 20 | 30 | add | 50 | | 2 | 5 | add | 7 | | 0 | 40 | add | 40 |

Page 12: Cucumber & cuke4duke with groovy and grails - lightning talk

Step 2: step definitionCalculatorSteps.groovy

this.metaClass.mixin(cuke4duke.GroovyDsl)

Before() {    calc = new Calculator()}

Given(~"I have entered (\\d+) into the calculator") { int n ->    calc.push n}

When(~"I press (\\w+)") {op ->    result = calc.send(op)}

Then(~"the result should be (.*) on the screen") { int r ->    assert r == result}

Page 13: Cucumber & cuke4duke with groovy and grails - lightning talk

Step 3: FAIL

Page 14: Cucumber & cuke4duke with groovy and grails - lightning talk

Step 4: write code to make it passCalculator.groovy

class Calculator {    def numbers = []

    void push(number) {        numbers << number    }

    def send(operation) {        def result        switch (operation) {            case "add":                result = add()        }        numbers.clear()        result    }

    private def add() {        numbers.sum()    }}

Page 15: Cucumber & cuke4duke with groovy and grails - lightning talk

Step 5: PASS

Page 16: Cucumber & cuke4duke with groovy and grails - lightning talk

Winning.

Page 17: Cucumber & cuke4duke with groovy and grails - lightning talk
Page 18: Cucumber & cuke4duke with groovy and grails - lightning talk
Page 19: Cucumber & cuke4duke with groovy and grails - lightning talk

https://github.com/nodejitsu/kyuri

via cuke4duke

via kyuri

via cuke4duke?

Page 20: Cucumber & cuke4duke with groovy and grails - lightning talk

• http://cukes.info/• https://github.com/aslakhellesoy/cuke4duke/wiki• https://github.com/danklynn/grails-cucumber• http://www.methodsandtools.com/archive/archive.

php?id=72• http://blog.jonasbandi.net/2010/03/classifying-bdd

-tools-unit-test-driven.html• http://www.slideshare.net/cebartling/cucumber-cu

ke4duke-and-groovy

references