groovier testing with spock

14
Rob Fletcher git clone http://github.com/robfletcher/ggug-spock-examples

Upload: robert-fletcher

Post on 06-May-2015

10.708 views

Category:

Documents


4 download

DESCRIPTION

Talk given at the London Groovy & Grails User Group by Rob Fletcher of Energized Work on 2010-08-16

TRANSCRIPT

Page 1: Groovier testing with Spock

Rob Fletchergit clone http://github.com/robfletcher/ggug-spock-examples

Page 2: Groovier testing with Spock

• Expressive testing language

• Easy to learn

• Usable from unit to end-to-end level

• Leverages Groovy language features

• Runs with JUnit: compatible with IDEs, build tools & CI

• Extensible for specialized testing scenarios

"Everything is questioned, and only the essential is kept"

Page 3: Groovier testing with Spock

Classes extend spock.lang.Specification Feature methods:

def "name in quotes"() separated into labelled blocks

Lifecycle: setup cleanup setupSpec cleanupSpec

Page 4: Groovier testing with Spock

given: preconditions, data fixtures, etc.

when: actions that trigger some outcome

then: makes assertions about outcome

expect: short alternative to when & then

where: applies varied inputs

and: sub-divides other blocks

setup: alias for given

cleanup:post-conditions, housekeeping, etc.

Page 5: Groovier testing with Spock

import spock.lang.*

class TotalizerSpec extends Specification {

  def totalizer = new Totalizer()

  def "adding an item increments the total"() {    when: "a product is added"    totalizer.add(WIDGET)

    then: "the total reflects the product's price"    totalizer.total == WIDGET.price  }}

Page 6: Groovier testing with Spock

assertEquals "Expo '86", album.titleassertFalse album.isCompilation()assertNotNull album.tracks.find { it == "Palm Road" }

JUnit 3…

assertThat album.title, equalTo("Expo '86")assertThat album.isCompilation(), is(false)assertThat album.tracks, hasItem("Palm Road")

JUnit 4…

then:album.title == "Expo '86"!album.isCompilation()album.tracks.find { it == "Palm Road" }

Spock…

Page 7: Groovier testing with Spock

Condition not satisfied:

starship.speed == old(starship.speed) + warpFactor| | | | | || 8 | 5 7 2| false[USS Enterprise NCC-1701]

at StarshipSpec.starship accelerates predictably(StarshipSpec.groovy:44)

Page 8: Groovier testing with Spock

@Unroll("adding a #product increments the total")def "adding a product increments the total"() {  when: totalizer.add(product)

  then: totalizer.total == product.price

  where: product << [WIDGET, GIZMO, THINGY]}

Page 9: Groovier testing with Spock

def "a user can be retrieved using their id"() {  when: def user = registry.findUser(id)

  then: user.name == name user.email == email

  where: id | name | email 1 | "Rob Fletcher" | "[email protected]" 2 | "Enoch Root" | "[email protected]" 3 | "Edward Teach" | "[email protected]" }

Page 10: Groovier testing with Spock

def "a message is sent when someone registers"() {  given:  def registry = new Registry()  def mailer = Mock(Mailer)  registry.mailer = mailer

  when:  registry.addPerson("Rob Fletcher", "[email protected]")

  then:  1 * mailer.send("[email protected]", "Welcome Rob!")}

Page 11: Groovier testing with Spock

import grails.plugin.spock.*

class UserSpec extends UnitSpec {

def "users can be saved and retrieved"() { setup: mockDomain(User)

when: def user = new User(name: "Rob").save()

then: !user.hasErrors()

and: User.findByName("Rob") }}

Page 12: Groovier testing with Spock

def "Login fails if incorrect details submitted"() { when: "a user visits the login page" def loginPage = LoginPage.open()

and: "enters their details incorrectly" loginPage.loginWith("blackbeard", "badpassword")

then: "they should see an error message" loginPage.loginError == LoginPage.LOGIN_FAILED_MSG

and: "they should not be logged in" !HomePage.open().isLoggedIn()}

Page 13: Groovier testing with Spock

import geb.spock.*

class GoogleSpockSpec extends GebSpec {

def "perform search"() { when: to GoogleHomePage search.search("spock framework")

then: at GoogleResultsPage resultLink(0).text() == "spock - Project Hosting

on Google Code" }}

Page 14: Groovier testing with Spock

spockframework.org

blog.spockframework.org

grails.org/plugin/spock

Energized Work: energizedwork.com

Blog: adhockery.blogspot.com

Twitter: @rfletcherEW