Automation testingQa

How to write tests in spock a groovy — part I

2 Mins read

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.


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

  1. Fetch data from the above API
  2. Test that hex color #ffaabb returns RGB(255, 170, 187)
  3. 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)”

// 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.

Don’t miss amazing tips!

Related posts
Automation testingQaRuby

Selenium Ruby Example And Deployment To The Cloud Grid

13 Mins read
Table of contents0.1 What is Selenium?0.2 How does Selenium WebDriver in Ruby work?0.3 How to use Selenium with Ruby?0.4 How to run…
General testingInterestingQa

Available Career Growth Paths In Software Testing

3 Mins read
Table of contents0.1 What Are The Available Career Growth Paths In Software Testing?0.2 Junior Level Possible careers:0.3 Mid Level Possible careers:0.4 Senior…
General testingQa

If You Want To Find Bugs - Do More Exploratory Testing

3 Mins read
Table of contents0.1 Exploratory testing basics0.2 Tips to get started with exploratory testing0.3 1) Set the purpose of your exploratory testing0.4 2)…

Leave a Reply

Your email address will not be published. Required fields are marked *

− 3 = 1

Automation testingGeneral testingQa

What Is Regression Testing and why do we need it?