Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Smarter Testing With Spock

Smarter Testing With Spock

As was presented in Israeli Groovy and Grails User Group (ISRGUG)

Baruch Sadogursky

October 17, 2012
Tweet

More Decks by Baruch Sadogursky

Other Decks in Technology

Transcript

  1. Meta & Intro State Based Testing Data Driven Testing Interaction

    Based Testing Spock Extensions More Cool Stuff What we’ll talk about
  2. Meta & Intro State Based Testing Data Driven Testing Interaction

    Based Testing Spock Extensions More Cool Stuff What we’ll talk about
  3. The creator of Spock Groovy & Gradle committer Works for

    Gradleware @pniederw 4 Peter Niederwieser
  4. A developer testing framework... for Groovy and Java applications... based

    on Groovy... fully compatible with JUnit... Spock is...
  5. A developer testing framework... for Groovy and Java applications... based

    on Groovy... fully compatible with JUnit... but going beyond! Spock is...
  6. Reduce the lines of test code Make tests more readable

    Turn tests into specifications Spock Can...
  7. Reduce the lines of test code Make tests more readable

    Turn tests into specifications Be extended in powerful ways Spock Can...
  8. Reduce the lines of test code Make tests more readable

    Turn tests into specifications Be extended in powerful ways Bring back the fun to testing! Spock Can...
  9. Homepage http://spockframework.org Source Code http://github.spockframework.org/spock Documentation http://docs.spockframework.org/en/latest Spock Web Console

    http://webconsole.spockframework.org Spock Example Project http://github.spockframework.org/spock-example Slides and Code for this Presentation http://github.spockframework.org/smarter-testing-with-spock Getting Started
  10. Meta & Intro State Based Testing Data Driven Testing Interaction

    Based Testing Spock Extensions More Cool Stuff What we’ll talk about
  11. Unit State Testing Vocabulary 21 Given some state of some

    objects… We write We mean Setup Given
  12. Unit State Testing Vocabulary 22 Given some state of some

    objects… When we change that state… We write We mean Setup Given Change state When
  13. Unit State Testing Vocabulary 23 Given some state of some

    objects… When we change that state… Then the results should be… We write We mean Setup Given Change state When Assert Then
  14. Unit State Testing Vocabulary 24 We write We mean Setup

    Given Change state When Assert Then
  15.  Blocks setup: cleanup: expect: given: when: then: where: and:

     Fixture Methods setup() cleanup() setupSpec() cleanupSpec()  Instance and @Shared fields  old() and thrown()  Forces developers to think like users  Nice looking reports (that no one reads) Recap: State Based Testing
  16. Meta & Intro State Based Testing Data Driven Testing Interaction

    Based Testing Spock Extensions More Cool Stuff What we’ll talk about
  17. Meta & Intro State Based Testing Data Driven Testing Interaction

    Based Testing Spock Extensions More Cool Stuff What we’ll talk about
  18. How objects communicate Mocking frameworks simplify the job Spock has

    its own mocking framework Interaction Based Testing
  19. How objects communicate Mocking frameworks simplify the job Spock has

    its own mocking framework Google it: “Mocks aren't Stubs” Interaction Based Testing
  20. How objects communicate Mocking frameworks simplify the job Spock has

    its own mocking framework Interaction Based Testing
  21. Creating Mocking Recap: Interaction Based Testing def sub = Mock(Subscriber)

    Subscriber sub = Mock() 1 * sub.receive("msg") (1..3) * sub.receive(_) (1.._) * sub.receive(_ as String) 1 * sub.receive(!null) 1 * sub.receive({it.contains("m")}) 1 * _./rec.*/("msg")
  22. Stubbing Recap: Interaction Based Testing (2) // now returns status

    code String receive(String msg) { ... } sub.receive(_) >> "ok" sub.receive(_) >>> ["ok", "ok", "fail"] sub.receive(_) >> { msg -> msg.size() > 3 ? "ok" : "fail" }
  23. Stubbing Mocking and Stubbing Recap: Interaction Based Testing (2) //

    now returns status code String receive(String msg) { ... } sub.receive(_) >> "ok" sub.receive(_) >>> ["ok", "ok", "fail"] sub.receive(_) >> { msg -> msg.size() > 3 ? "ok" : "fail" } 3 * sub.receive(_) >>> ["ok", "ok", "fail"]
  24. Stubbing Mocking and Stubbing Impressing your friends Recap: Interaction Based

    Testing (2) // now returns status code String receive(String msg) { ... } sub.receive(_) >> "ok" sub.receive(_) >>> ["ok", "ok", "fail"] sub.receive(_) >> { msg -> msg.size() > 3 ? "ok" : "fail" } 3 * sub.receive(_) >>> ["ok", "ok", "fail"] (_.._) * _._(*_) >> _
  25. Meta & Intro State Based Testing Data Driven Testing Interaction

    Based Testing Spock Extensions More Cool Stuff What we’ll talk about
  26. External Extensions  spock-grails  spock-spring  spock-guice  spock-tapestry

     spock-unitils  spock-griffon  spock-arquillian  spock-extensions http://github.com/robfletcher/spock-extensions
  27. Meta & Intro State Based Testing Data Driven Testing Interaction

    Based Testing Spock Extensions More Cool Stuff What we’ll talk about
  28. ~/.spock/SpockConfig.groovy, or on class path, or with -Dspock.configuration Configuring Spock

    runner { filterStackTrace false include Fast exclude Slow optimizeRunOrder true } @Fast class MyFastSpec extends Specification { def "I’m fast as hell!"() { expect: true } @Slow def "sorry, can’t keep up..."() { expect: false } }
  29. Eclipse, IDEA (specially IDEA) Ant, Maven, Gradle Jenkins, Bamboo, TeamCity

    Spock runs everywhere JUnit and Groovy run! Tooling
  30. Eclipse, IDEA (specially IDEA) Ant, Maven, Gradle Jenkins, Bamboo, TeamCity

    Spock runs everywhere JUnit and Groovy run! Tooling
  31. You write... Spock Under The Hood a = 1; b

    = 2; c = 4 expect: sum(a, b) == c
  32. You write... Spock generates... Spock Under The Hood a =

    1; b = 2; c = 4 expect: sum(a, b) == c def rec = new ValueRecorder() SpockRuntime.verifyCondition( rec.record(rec.record(sum(rec.record(a), rec.record(b)) == rec.record(c)))
  33. You write... Spock generates... You see... Spock Under The Hood

    a = 1; b = 2; c = 4 expect: sum(a, b) == c def rec = new ValueRecorder() SpockRuntime.verifyCondition( rec.record(rec.record(sum(rec.record(a), rec.record(b)) == rec.record(c)))
  34. Homepage http://spockframework.org Source Code http://github.spockframework.org/spock Documentation http://docs.spockframework.org/en/latest Spock Web Console

    http://webconsole.spockframework.org Spock Example Project http://github.spockframework.org/spock-example Slides and Code for this Presentation http://github.spockframework.org/smarter-testing-with-spock Mocks Aren’t Stubs http://martinfowler.com/articles/mocksArentStubs.html Q&A