Groovier testing with Spock
Upcoming SlideShare
Loading in...5
×
 

Groovier testing with Spock

on

  • 11,345 views

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

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

Statistics

Views

Total Views
11,345
Slideshare-icon Views on SlideShare
10,581
Embed Views
764

Actions

Likes
15
Downloads
91
Comments
0

8 Embeds 764

http://d.hatena.ne.jp 662
http://projects.semanticbits.com 89
https://twitter.com 6
https://duckduckgo.com 2
https://www.google.co.jp 2
http://ecampustraining.dcccd.edu 1
http://universegroovygrails.blogspot.com 1
http://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • HAI

Groovier testing with Spock Groovier testing with Spock Presentation Transcript

  • Rob Fletcher git clone http://github.com/robfletcher/ggug-spock-examples
    • 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"
    • Classes extend spock.lang.Specification
    • Feature methods:
      • def "name in quotes"()
      • separated into labelled blocks
    • Lifecycle:
      • setup
      • cleanup
      • setupSpec
      • cleanupSpec
    • 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.
    • 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
    •    }
    • }
  • assertEquals "Expo '86" , album.title assertFalse 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…
    • 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)
    • @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 ]
    • }
    • def &quot;a user can be retrieved using their id&quot; () {
    •    when:
    • def user = registry.findUser(id)
    •    then:
    • user.name == name
    • user.email == email
    •    where:
    • id | name | email
    • 1 | &quot;Rob Fletcher&quot; | &quot;rob@energizedwork.com&quot;
    • 2 | &quot;Enoch Root&quot; | &quot;root@eruditorum.org&quot;
    • 3 | &quot;Edward Teach&quot; | &quot;blackbeard@providence.bs&quot;
    • }
    • def &quot;a message is sent when someone registers&quot; () {
    •    given:
    •    def registry = new Registry()
    •    def mailer = Mock(Mailer)
    •    registry.mailer = mailer
    •    when:
    •    registry.addPerson( &quot;Rob Fletcher&quot; , &quot;rob@ew.com&quot; )
    •    then:
    •    1 * mailer.send( &quot;rob@ew.com&quot; , &quot;Welcome Rob!&quot; )
    • }
    • import grails.plugin.spock.*
    • class UserSpec extends UnitSpec {
    • def &quot;users can be saved and retrieved&quot; () {
    • setup: mockDomain(User)
    • when:
    • def user = new User(name: &quot;Rob&quot; ).save()
    • then: !user.hasErrors()
    • and: User.findByName( &quot;Rob&quot; )
    • }
    • }
    • def &quot;Login fails if incorrect details submitted&quot; () {
    • when: &quot;a user visits the login page&quot;
    • def loginPage = LoginPage.open()
    • and: &quot;enters their details incorrectly&quot;
    • loginPage.loginWith( &quot;blackbeard&quot; , &quot;badpassword&quot; )
    • then: &quot;they should see an error message&quot;
    • loginPage.loginError == LoginPage. LOGIN_FAILED_MSG
    • and: &quot;they should not be logged in&quot;
    • !HomePage.open().isLoggedIn()
    • }
    • import geb.spock.*
    • class GoogleSpockSpec extends GebSpec {
    • def &quot;perform search&quot; () {
    • when:
    • to GoogleHomePage
    • search.search( &quot;spock framework&quot; )
    • then:
    • at GoogleResultsPage
    • resultLink(0).text() == &quot;spock - Project Hosting on Google Code&quot;
    • }
    • }
    • spockframework.org
    • blog.spockframework.org
    • grails.org/plugin/spock
    Energized Work: energizedwork.com Blog: adhockery.blogspot.com Twitter: @rfletcherEW