RestFul API Testing with Cucumber and Rack Test

API Testing & BDD

API Testing is a nice way to conduct end to end test on your application by communicating with another modules. You can read more about it here

When it comes to Behaviour Driven Development [BDD], API testing become more interesting. I am having great fun experimenting with Cucumber while reading Cucumber Book by Matt Wynne & Aslak Hellesoy. I already tried API Testing with Behat by following Keith Loy ‘s blog ‘Testing Rest API with Behat’ which you can read here.

In this short, tutorial, we will see in-process API Testing with Cucumber and Rack Test. We have assumed that Cucumber and Application sits in the same Ruby process. We will be dealing with simple API which stores and returns name of sports and players.

Start with Feature File

We will create simple web Service by using Sinatra and JSON which has /sports route and returns data in JSON format.  Lets Create Project and within that project we will create ‘sports_list.feature’

Let’s add following code to the ‘sport_app.rb’ file

In here, we are storing some sports and players, then make GET request /sports which should return 200 response. We are also checking number of elements and response body.

Now we will install necessary Gems. Create ‘Gemfile’ with following gems

Install Gems by running command

Now we can run ‘cucumber’ and you will see following

In order to make them pass, we need to

  • Create Simple Web service which returns JSON 
  • Organize our Project Code with Step Definitions & Support directory
  • Implement Step Definitions to make them pass

Create a Project Structure by creating files and directory which will look like this :



We have source code available on the GitHub in the repository ‘REST-Cucumber-RackTest‘. You can clone source code and execute following commands to see all the scenarios passing:

 What We have Done?

Let’s try to understand what we have done in each of these files in order to make all steps passed.


In this file, we are creating simple web service with route /sports which will return JSON response. We are using Sinatra for creating the web service.


As we are calling Rack-Test’s ‘get’ method, we need to setup ‘environment for the Rack-Test. The ‘get’ method is defined in the in a module called “Rack::Test::Methods”.

Last line of the code in this file

This will register two ruby module within Cucumber one for HTTP Simulation and other for own helper module. We have defined AppHelper to communicate between modules.


In here, we are clearing data in the ‘before’ hook. We have stored data in the class variable that needs to be cleaned up every time.


This is the place where we have implemented all the REST related steps. We have parsed JSON response also we have checked status of the message and number of elements.


In this step, we have stored data in the class variable.

At this point, if you run ‘cucumber’ you will see all steps passed.



Using Cucumber and Rack Test, we can perform API testing within te same Ruby process. We will see API testing in the ‘Out-of-Process’ in the next post. Stay tuned !!