Showing posts with label REST. Show all posts
Showing posts with label REST. Show all posts

Thursday, 10 April 2014

restIT: Testing Framework for REST based web services

I would like to call this testing framework restIT (REST Integration Test).

This test framework will give a simple way to verify that REST webservice is responding with right JSON responses. All you need to provide is a couple of yml config files declaring the REST urls and JSON schema files for the expected JSON responses.

Adding new tests should not take more than 10 mins of your time. Framework is flexible enough for you to add more advanced features such as Authentication headers and complex JSON response validation which cannot be done via JSON schema files.

Project is hosted on github at: https://github.com/njaiswal/restITframework

Steps to make use of this framework.

  • Git clone the project.

  • Import the project in eclipse or your fav ID as existing maven project.
  • Replace src/test/resources/testcases.yml file with your own.
    • Make sure you change baseurl to map to base url of your rest service.
    • Add services which you want to test with/out prameters, this depends on your rest service. See examples in the file.

  • For each of the test case you have added in the above step, you need to add a schema map. Replace file src/main/resources/schemaMap.yml with your own.

Each schema map in this yml file has 3 fields:
    • id: This is just a name.Appears in logs/reports.
    • file: This is the JSON schema file path
    • regex: If the URL matches this regex, response of this URL will be validated againts above JSON schema file
  • In src/test/resoruces/schemas directory add all JSON schema files defined in schemaMap.yml file.
    • See examples in the git project.
    • Find more details on how to write schema files at: http://json-schema.org/
    • You can add as much or as little JSON response validation as you want depending on what json-schema provides out of the box. Think of this like xml schema for xml responses.
    • If you want to do more complicated response validation, extended the response validation code, or if you have specific requests let me know and I might accommodate it in my free time.

Steps to run the test framework and view reports.

Easy: 

View reports at: target/surefire-reports/index.html

Make sure to check the 'Reporter Output' tab for any JSON schema validation error reports. It shows all json schema vaildation errors pretty printed.

Enjoy. Let me know if you have any questions/suggestions.


Saturday, 20 April 2013

Cucumber-JVM BDD for RESTful API

In this post I will like to work out a example in Cucumber which takes on a very familiar setup these days in any kind of software organisation: Acceptance testing of a RESTful web service.

For anyone who has been following my posts, I tend to make use of online services in my testing examples. This time is no different and the goal is to:

Write set of acceptance tests for http://imdbapi.org's RESTful API

And I am going to use Cucumber-JVM achieve the objective. As the name suggest imdbapi.org exposes IMDB data via a RESTful api and we are going to search for movies! Try out following to understand the scope of this testing:



Setup Java Project with Maven


Put all dependencies in pom.xml 
Since I have already coded up the test case my directory structure is looking like following:


All or most of the fun in Cucumber is in writing the .feature file which is in Gherkin which is the language which many BDD tools like Cucumber understands.

Lets start looking my .feature file. It contains 2 Scenarios. First one is a simple scenario which represents 1 test case, while the second one is actually a set of test cases with different input and expected output. There are a lot of other ways which one can represent their set of acceptance test cases in Gherkin but for sake of example I have used the following and this file should be self explanatory which is what Gherkin is good at.



Now create a simple RunImdbTest.java file which will be used to run test cases in .features file via JUnit.



At this point you can run 'mvn test' and let Cucumber print out the snippet of code that you need to include in your Step Definition file where basically the conversion between plain english test statements happen to actual java code behind. In our case this file is ImdbSteps.java

ImdbStep.java


This Step Definition file contains a set of annotations with capture regular expressions that drive the whole automation between plain english statements and java code behind to do the job.

JsonReader.java



This helper class has to logic to http get the movie titles and convert the response in JSON objects for comparison in Step Definition class.

Finally our test cases are ready and we can run them using 'mvn test' command. This run will also create the green cucumber test report which all product managers like to see.



All project files are uploaded on github.