JAZOON'13 - Andres Almiray - Spock: boldly go where no test has gone before
Spock - WordPress.com · • Spock is a “data driven” test tool • The idea is that the test...
Transcript of Spock - WordPress.com · • Spock is a “data driven” test tool • The idea is that the test...
SPOCK
Data driven testing
AGENDA
1. What is a REST service?
2. Structure of Rest services
3. Testing of Rest services
4. What is Spock Framework?
5. Why Spock?
6. How do you use Spock ?
7. General Tips
RESTFUL API
• A RESTful API is an application program interface (API) that uses HTTP requests to GET,
PUT, POST and DELETE data. Representational state transfer (REST), which is used by
browsers, can be thought of as the language of the Internet.
STRUCTURE OF A REST SERVICE
1. End Point
/user/login
2. Method Type
GET/POST/PUT/DELETE…
3. Body (Request payload)
content type = application/JSON
{“email” : “[email protected]” , “password” : “test” }
TESTING OF A REST SERVICE
• What is required ?
• End point of the service
• Method Type
• Body
• Request payload
• Request headers
ASSERTIONS ON THE RESPONSE
• Comparisons :
• Expected output == actual output
Request body:
{“email” : “hi2344@com” , “password” : “test”} // Invalid email address – Login
Response body
{
error : Invalid email address
}
TOOLS /FRAMEWORK FOR TESTING RESTFULL API
• SoapUI
• Postman
• Advance Rest Client
• Spock
• Cocumber
• Protractor
• etc
SPOCK VS SOAPUI
Description SoapUI Spock
Merging No Yes
SPOCK VS SOAPUI
Description SoapUI Spock
x: Tests are hard to
build up
Yes No
SPOCK VS SOAPUI
Description SoapUI Spock
No steep learning
curve
Yes No
WHAT IS SPOCK?
• Spock is a “data driven” test tool
• The idea is that the test complexity should be in the data itself, not the test cases
• Rather than needing to write a new test (or conditional statement) for every test case, you just
need a new row in a table
• Spock is written in Groovy
• Expressive testing language
• Easy to Learn
• Reduces the line of test code
• Structural blocks – BDD style
WHY HAVE WE DECIDED TO USE SPOCK?
1. The tests can be run from command line, meaning we can also integrate them into
Bamboo or other automated deployments
2. Because the tests are in Groovy with Gradle, many IDEs allow the programmer to run them
without opening another window
3. Because the tests are written in a programming language, they are much more version
control friendly
4. Because the tests are written in a JVM language, we can avoid boilerplate by taking
advantage of our existing code and OO principles
5. There is no UI to deal with
HOW DO YOU USE SPOCK?
1. Make a Groovy class that extends the Spock “Specification” class
2. Make a method with “given” “expect”, and “where” blocks
a. “given” blocks blocks are for setting up the test
b. “expect” blocks contain your assertions
c. “where” blocks contain your data table
3. Run the test
a. Some IDEs have good JVM integration allowing for running in them
b. Otherwise you can use gradle: “./gradlew test”
GENERAL TIPS
1. Use the config.properties file in the resources to
change the endpoint URL
a. This will allow for quickly switching between the
various environments
2. Use conditional statement sparingly
a. If you are using a lot of conditional statements, there is
a good chance that you can create another table
column
3. Create builders for the data objects
a. Although it can be tedious, it will make your tests much
easier to read
4. Declare long strings at the top of the class to cut down
on table column size