Getting Started with REST Testing in SoapUI

Getting Started with REST APIs

 

When it comes to testing REST APIs, having the right tools can make all the difference. Just because our name is SoapUI, doesn’t mean we don’t know what we are talking about when it comes to REST testing. SoapUI is built specifically for testers and developers to improve API quality and speed time to delivery. This guide will walk you through the basics of REST testing and how you can easily test your REST APIs with SoapUI.

 

What is a RESTful API?

 

REST stands for Representational State Transfer. It is a software architecture style that relies on a stateless communications protocol, most commonly, HTTP. REST structures data in XML, YAML, or any other format that is machine-readable, but usually JSON is most widely used. REST follows the object-oriented programming paradigm of noun-verb. REST is very data-driven, compared to SOAP, which is strongly function-driven. You may see people refer to them as RESTful APIs or RESTful web services. They mean the same thing and can be interchangable. 

 

Your basic REST HTTP requests are: POST, GET, PUT, and DELETE. Let’s look at an example from the Swagger Pet Store API:

 

  • Sending a GET request to /pet/{petId} would retrieve pets with a specified ID from the database.
  • Sending a POST request to /pet/{petId}/uploadImage would add a new image of the pet.
  • Sending a PUT request to /pet/{petId} would update the attributes of an existing pet, identified by a specified id.
  • Sending a DELETE request to /pet/{petId} would delete a specified pet.

 

So in a nutshell here is what each of these request types map to:

 

GET

Read or retrive data

POST

Add new data

PUT

Update data that already exists

DELETE

Remove data

 

To learn more about REST requests and how to do them in SoapUI, please visit our Working with REST Requests page.

 

REST API Testing with SoapUI

 

SoapUI supports extensive testing of RESTful web services and their resources, representations, etc. Follow along below with a quick demo using the Google Maps API to get you going. In just a few minutes, you'll know how to create your first functional test of a RESTful web service — and it only takes a few clicks! Once you get the hang of it, start testing your own APIs!

 

If you’d like a more detailed guide to testing RESTful APIs, download our E-book: REST 101: The Beginner's Guide to Using and Testing RESTful APIs

 

To get started with this tutorial download SoapUI Pro now!

 

 

REST Testing Video Tutorial

 

 

The way to create REST testing requests has been greatly simplified in SoapUI 4.6 – fewer steps are needed.

Start by creating a new REST project from the File menu by choosing the "New REST Project" option in the File menu:

Dialog to create a new REST project

Specify the following Google Map API URL in the Service Endpoint Field:

http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false

Here you can just press OK, and SoapUI creates the project complete with a Service, Resource, Method and the actual Request and opens the Request editor.

In the "Parameters" table, you can see that SoapUI has automatically extracted the different query-arguments from the path.

Press the green arrow at the top left in the Request editor and you can see the XML output returned by the service:

Here you can just press OK, which finally creates the actual request and opens its editor. Press the green arrow at the top left in the Request editor and you can see the XML output returned by the service:

rest_places_resource_get_method_request_1

The request seems to be working fine, so we're all set to create an actual functional test for this resource. Click the "Add to TestCase" button at the top left, which prompts for the names of an initial TestSuite and TestCase, then it shows the following dialog:

add_rest_request_to_test_case

Just go with the default options for now and press OK; SoapUI generates a corresponding REST Request TestStep into the TestCase:

run_rest_test_case

Now double-click the resource icon in the navigator and change the Resource Path to "/maps/api/geocode/json":

change_resource_path

Now go back to the previous request and run it again:

json_rest_request

Now you can see a nicely formatted JSON response in the JSON view to the right instead of the previous XML result.

Ok! Time to add an actual assertion to validate the content of the response. In our case we are just going to check that we get 1 place back from the service, open the "Get places - Request 1" TestStep and submit it as usual giving the same JSON response as above. Then in the right response part of the window now select the "Outline" view and right-click on the first "e" item. Then in the popup menu, select the "Add Assertion -> for Count" option, which automatically generates an JSONPath Count assertion for you (this is a SoapUI Pro feature, in SoapUI open source you should create this assertion by hand):

add_count_assertion_to_rest_rest

Here you see the generated JSONPath statement at the top and its expected result below. All is fine, just Save the assertion and go back to the TestCase window:

jsonpath_count_match_rest_assertion

Run the TestCase with the green arrow at the top left which will result in the above output in the Log at the bottom; your functional test passed just fine!

rest_test_case_completed_successfully

Finally, if you are running SoapUI Pro, you can create a simple HTML report. Click the "Create Report" button in the menu at the top and select "JUnit-Syle HTML Report" in the opened dialog as follows:

rest_test_case_junit_resport_selected

Press OK and SoapUI will generate the report for and open it in the system browser:

rest_test_case_junit_report

Voila! Your first functional test of a REST service with SoapUI, just a couple of clicks away.