Month: April 2015

Behavior Driven Acceptance Testing for Web API

Posted on Updated on

Problem

In modern enterprise application which is based on SOA, we have multiple web APIs. In this case service provider needs to do functional and integration test for each Web API as per the given scenario and provide the documentation to service consumer. This will take  integration pain away from service consumer and also ensure the functional automation of web API. The problem is how to do functional automation for Web API using continuous integration ?

Solution

In  Agile software development  Behavior Driven Development (BDD) is the way to solve this problem but in this case developer is involved, need to follow this practice from beginning of the development.

But if instead of BDD you are using TDD and you have Testing team which is responsible for making releases of your web API. Then following approach will come into the rescue:

To do the functional and integration testing for web api as well as generating reports/document for stake holders like business analyst, product owner, service consumer etc … in continuous integration.

I have decided to use Cucumber which has Gherkin support, SoapUI is used to do the functional and integration testing of Web API and JUnit will act as bridge between Cucumber and SoapUI. Jenkins is used for continuous integration using Maven and cucumber reports are visualized using Cucumber test result plugin. The work flow is as below:

BDAT

To give an example, I want to test a scenario where user will be authenticated based on his credentials and after successfully authentication will receive access token. Using this access token there is another service will provide the user profile.

To support it my IdentityManagement.feature file will be as below:

  Feature: Identity Management
  In order to manage user identity
  As a service consumer
  I want to authenticate user and view his profile
 
  @soapui
  Scenario Outline: Autheticate user and Get user profile
  Given I have a <username> and <password>
  When I call authetication service
  Then I should get Access Token
  And I call user service to fetch the user profile
  
  Examples: Successfull Parameters
  | username | password |
  | vinayak | secretAgent |
  | john | secretAgent |

Using this scenario Cucumber will generate the JUnit Test cases. Cucumber and JUnit integration is provide by cucumber-jvm . From these unit test SoapUI test case/test step will be executed.Here SoapUI has provided the nice documentation for it.

In brief will show you what is the SoapUI project structure and how it relates to Cucumber feature. The following image is self explanatory.

SoapUI Project Structure

Terminology used in Cucumber is mapped with SoapUI building blocks as below:

Cucumber   SoapUI      
Feature        Test Suite  
Scenario     Test Case  
Given           Test Step  
When           Test Step  
Then           Test Step    

The source code for skeleton project is here.

Happy automation testing for Web API 🙂

Advertisements