Table of contents
What is spock?
Spock is a framework that was developed with a purpose of writing automated tests for software applications.
What is groovy?
Groovy is a lightweight version of Java programming language that rans on the JVM. It employs feature of both a static and dynamic programming language. To get more information about groovy, visit the official site here.
Prerequisites
Introduction to spock.
Let’s quickly scheme through the basic composition of a spock test method. We are going to use a real free to use color API (an api that converts different colors to different color formats). To test the API, we hit the API endpoint with a defined request and get the response with returned data. After this, we will write assertions against the results, we will then check whether — our tests pass or fail. Before jumping straight to the code, let’s look at the test method below to understand what a spock method consists of.
In our quest to learn how spock and groovy work, we will use an hex-rgb color converter API to confirm what we expect as a valid response. On a very basic level, we shall cover the following test cases.
Test overview
- Fetch data from the above API
- Test that hex color #ffaabb returns RGB(255, 170, 187)
- Write assertions to verify the responses
Getting Started
To start, create a test class with the following code.
def “test that hex color #ffaabb returns RGB(255, 170, 187)” given: “” when: “the get color endpoint is invoked” then: “RGB colors should match (255, 170, 187)” end
// this is the part where the test description is given.
//this sets up preconditions for the test to be run.
//this is a section where we hit the actual endpoint of the system under test
Exploring spock annotations.
That’s because in spite of its iffy earnings reports over the last five years, Synergy Research reported that IBM had 7 percent of the cloud infrastructure market in its most recent report, which it defines as Infrastructure.
@Title — gives a natural-language name to a spec
–@Narrative — natural-language descrition to a spec
–@Unroll — enables iterations on a test method to be reported independently
-@See — References to external information related to a specification or feature.
-@Stepwise —Declares that the spec’s feature methods should be run sequentially in their declared order
- For a detailed list of the available annotations see: here.
Thanks for reading!
Please check out part II — where we write and run the first test!
See this article on regression testing.