testing con spock
TRANSCRIPT
TESTING CON SPOCK
http://bitbucket.org/salenda/robot-cocina
fatimacasau
TDD
AGILISMO
¡ESTÁN DE MODA!
ScrumKanban
XP
¿Test difíciles de comprender?
¿Test poco documentados?
¿Si los test fallan tardas en averiguar por qué?
¡Da pereza hacer tests!
¡¡Spock y Groovy son tus aliados!!
SPOCK• Framework para testing
• Altamente expresivo
• Groovy como lenguaje
• jUnit Runner
• Aplicaciones Java y Groovy
• Mock de objetos
Getting Started
• Spock Web Console - http://webconsole.spockframework.org
• Eclipse, IDEA, NetBeans
• Ant, Maven,Gradle
import spock.lang.*
class MyFirstSpecification extends Specification {
// fields
// fixture methods
// feature methods
// helper methods
}
FIELDS
def obj = new ClassUnderSpecification() def coll = new Collaborator()
@Shared res = new VeryExpensiveResource()
static final PI = 3.141592654
FIXTURE METHODS
def setup() {} // run before every feature method def cleanup() {} // run after every feature method def setupSpec() {} // run before the first feature method def cleanupSpec() {} // run after the last feature method
FEATURE METHODS
def "pushing an element on the stack"() { // blocks go here }
• Blocks
• Specification as Documentation
• Historias de usuario
BLOCKS
HELPER METHODS
• Otros métodos
• Llamados desde tests
• Reutilizar código
ETIQUETAS IMPORTANTES
• @Ignore
• @Unroll
@Unroll class DataDriven extends Specification { def "maximum of #a and #b is #c"() { expect: Math.max(a, b) == c where: a | b || c 3 | 5 || 5 7 | 0 || 7 0 | 0 || 0 } }
LOGS
maximum of 7 and 0 is 7 FAILED Math.max(a, b) == c | | | | | | 7 0 | 7 42 false
Contenido ExtraGrails y Test funcionales con Geb
GRAILS• Plugin Spock
• Tests unitarios (mocks)
• Dominio, Controllers, Constraints, Taglibs, Services...
• Tests integración
• Funcionales con apoyo de plugins
• Plugin Geb
GEB• Browser automation solution
• WebDriver
• jQuery content selection
• Page Object modelling
• Groovy language
• Spock testing
• The Book of Geb - http://www.gebish.org/manual/current/
• http://www.gebish.org/
import Geb.Browser Browser.drive { go "http://google.com/ncr" // make sure we actually got to the page assert title == "Google" // enter wikipedia into the search field $("input", name: "q").value("wikipedia") // wait for the change to results page to happen // (google updates the page dynamically without a new request) waitFor { title.endsWith("Google Search") } // is the first link to wikipedia? def firstLink = $("li.g", 0).find("a.l") assert firstLink.text() == "Wikipedia" // click the link firstLink.click() // wait for Google's javascript to redirect to Wikipedia waitFor { title == "Wikipedia" } }
GEB