Spock - WordPress.com · • Spock is a “data driven” test tool • The idea is that the test...

14
SPOCK Data driven testing

Transcript of Spock - WordPress.com · • Spock is a “data driven” test tool • The idea is that the test...

Page 1: Spock - WordPress.com · • 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

SPOCK

Data driven testing

Page 2: Spock - WordPress.com · • 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

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

Page 3: Spock - WordPress.com · • 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

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.

Page 4: Spock - WordPress.com · • 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

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” }

Page 5: Spock - WordPress.com · • 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

TESTING OF A REST SERVICE

• What is required ?

• End point of the service

• Method Type

• Body

• Request payload

• Request headers

Page 6: Spock - WordPress.com · • 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

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

}

Page 7: Spock - WordPress.com · • 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

TOOLS /FRAMEWORK FOR TESTING RESTFULL API

• SoapUI

• Postman

• Advance Rest Client

• Spock

• Cocumber

• Protractor

• etc

Page 8: Spock - WordPress.com · • 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

SPOCK VS SOAPUI

Description SoapUI Spock

Merging No Yes

Page 9: Spock - WordPress.com · • 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

SPOCK VS SOAPUI

Description SoapUI Spock

x: Tests are hard to

build up

Yes No

Page 10: Spock - WordPress.com · • 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

SPOCK VS SOAPUI

Description SoapUI Spock

No steep learning

curve

Yes No

Page 11: Spock - WordPress.com · • 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

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

Page 12: Spock - WordPress.com · • 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

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

Page 13: Spock - WordPress.com · • 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

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”

Page 14: Spock - WordPress.com · • 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

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