Ggug spock
-
Upload
skills-matter -
Category
Documents
-
view
3.776 -
download
0
description
Transcript of Ggug spock
Rob Fletchergit 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.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…
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 "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]" }
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!")}
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") }}
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()}
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" }}
spockframework.org
blog.spockframework.org
grails.org/plugin/spock
Energized Work: energizedwork.com
Blog: adhockery.blogspot.com
Twitter: @rfletcherEW