Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Groovier testing with Spock

12.931 Aufrufe

Veröffentlicht am

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

  • Als Erste(r) kommentieren

Groovier testing with Spock

  1. 1. Rob Fletcher git clone http://github.com/robfletcher/ggug-spock-examples
  2. 2. <ul><li>Expressive testing language </li></ul><ul><li>Easy to learn </li></ul><ul><li>Usable from unit to end-to-end level </li></ul><ul><li>Leverages Groovy language features </li></ul><ul><li>Runs with JUnit: compatible with IDEs, build tools & CI </li></ul><ul><li>Extensible for specialized testing scenarios </li></ul>&quot;Everything is questioned, and only the essential is kept&quot;
  3. 3. <ul><li>Classes extend spock.lang.Specification </li></ul><ul><li>Feature methods: </li></ul><ul><ul><li>def &quot;name in quotes&quot;() </li></ul></ul><ul><ul><li>separated into labelled blocks </li></ul></ul><ul><li>Lifecycle: </li></ul><ul><ul><li>setup </li></ul></ul><ul><ul><li>cleanup </li></ul></ul><ul><ul><li>setupSpec </li></ul></ul><ul><ul><li>cleanupSpec </li></ul></ul>
  4. 4. <ul><li>given : preconditions, data fixtures, etc. </li></ul><ul><li>when : actions that trigger some outcome </li></ul><ul><li>then : makes assertions about outcome </li></ul><ul><li>expect : short alternative to when & then </li></ul><ul><li>where : applies varied inputs </li></ul><ul><li>and : sub-divides other blocks </li></ul><ul><li>setup : alias for given </li></ul><ul><li>cleanup : post-conditions, housekeeping, etc. </li></ul>
  5. 5. <ul><li>import spock.lang.* </li></ul><ul><li>class TotalizerSpec extends Specification { </li></ul><ul><li>   def totalizer = new Totalizer() </li></ul><ul><li>   def &quot;adding an item increments the total&quot; () { </li></ul><ul><li>     when: &quot;a product is added&quot; </li></ul><ul><li>     totalizer.add( WIDGET ) </li></ul><ul><li>     then: &quot;the total reflects the product's price&quot; </li></ul><ul><li>     totalizer.total == WIDGET .price </li></ul><ul><li>   } </li></ul><ul><li>} </li></ul>
  6. 6. assertEquals &quot;Expo '86&quot; , album.title assertFalse album.isCompilation() assertNotNull album.tracks.find { it == &quot;Palm Road&quot; } JUnit 3… assertThat album.title, equalTo( &quot;Expo '86&quot; ) assertThat album.isCompilation(), is(false) assertThat album.tracks, hasItem( &quot;Palm Road&quot; ) JUnit 4… then: album.title == &quot;Expo '86&quot; !album.isCompilation() album.tracks.find { it == &quot;Palm Road&quot; } Spock…
  7. 7. <ul><li>Condition not satisfied: </li></ul><ul><li>starship.speed == old(starship.speed) + warpFactor </li></ul><ul><li>| | | | | | </li></ul><ul><li>| 8 | 5 7 2 </li></ul><ul><li>| false </li></ul><ul><li>[USS Enterprise NCC-1701] </li></ul><ul><li>at StarshipSpec.starship accelerates predictably(StarshipSpec.groovy:44) </li></ul>
  8. 8. <ul><li>@Unroll( &quot;adding a #product increments the total&quot; ) </li></ul><ul><li>def &quot;adding a product increments the total&quot; () { </li></ul><ul><li>   when: </li></ul><ul><li>totalizer.add(product) </li></ul><ul><li>   then: </li></ul><ul><li>totalizer.total == product.price </li></ul><ul><li>   where: </li></ul><ul><li>product << [ WIDGET , GIZMO , THINGY ] </li></ul><ul><li>} </li></ul>
  9. 9. <ul><li>def &quot;a user can be retrieved using their id&quot; () { </li></ul><ul><li>   when: </li></ul><ul><li>def user = registry.findUser(id) </li></ul><ul><li>   then: </li></ul><ul><li>user.name == name </li></ul><ul><li>user.email == email </li></ul><ul><li>   where: </li></ul><ul><li>id | name | email </li></ul><ul><li>1 | &quot;Rob Fletcher&quot; | &quot;rob@energizedwork.com&quot; </li></ul><ul><li>2 | &quot;Enoch Root&quot; | &quot;root@eruditorum.org&quot; </li></ul><ul><li>3 | &quot;Edward Teach&quot; | &quot;blackbeard@providence.bs&quot; </li></ul><ul><li>} </li></ul>
  10. 10. <ul><li>def &quot;a message is sent when someone registers&quot; () { </li></ul><ul><li>   given: </li></ul><ul><li>   def registry = new Registry() </li></ul><ul><li>   def mailer = Mock(Mailer) </li></ul><ul><li>   registry.mailer = mailer </li></ul><ul><li>   when: </li></ul><ul><li>   registry.addPerson( &quot;Rob Fletcher&quot; , &quot;rob@ew.com&quot; ) </li></ul><ul><li>   then: </li></ul><ul><li>   1 * mailer.send( &quot;rob@ew.com&quot; , &quot;Welcome Rob!&quot; ) </li></ul><ul><li>} </li></ul>
  11. 11. <ul><li>import grails.plugin.spock.* </li></ul><ul><li>class UserSpec extends UnitSpec { </li></ul><ul><li>def &quot;users can be saved and retrieved&quot; () { </li></ul><ul><li>setup: mockDomain(User) </li></ul><ul><li>when: </li></ul><ul><li>def user = new User(name: &quot;Rob&quot; ).save() </li></ul><ul><li>then: !user.hasErrors() </li></ul><ul><li>and: User.findByName( &quot;Rob&quot; ) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  12. 12. <ul><li>def &quot;Login fails if incorrect details submitted&quot; () { </li></ul><ul><li>when: &quot;a user visits the login page&quot; </li></ul><ul><li>def loginPage = LoginPage.open() </li></ul><ul><li>and: &quot;enters their details incorrectly&quot; </li></ul><ul><li>loginPage.loginWith( &quot;blackbeard&quot; , &quot;badpassword&quot; ) </li></ul><ul><li>then: &quot;they should see an error message&quot; </li></ul><ul><li>loginPage.loginError == LoginPage. LOGIN_FAILED_MSG </li></ul><ul><li>and: &quot;they should not be logged in&quot; </li></ul><ul><li>!HomePage.open().isLoggedIn() </li></ul><ul><li>} </li></ul>
  13. 13. <ul><li>import geb.spock.* </li></ul><ul><li>class GoogleSpockSpec extends GebSpec { </li></ul><ul><li>def &quot;perform search&quot; () { </li></ul><ul><li>when: </li></ul><ul><li>to GoogleHomePage </li></ul><ul><li>search.search( &quot;spock framework&quot; ) </li></ul><ul><li>then: </li></ul><ul><li>at GoogleResultsPage </li></ul><ul><li>resultLink(0).text() == &quot;spock - Project Hosting on Google Code&quot; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  14. 14. <ul><li>spockframework.org </li></ul><ul><li>blog.spockframework.org </li></ul><ul><li>grails.org/plugin/spock </li></ul>Energized Work: energizedwork.com Blog: adhockery.blogspot.com Twitter: @rfletcherEW

×