Creating Test Cases

In vREST NG,

  • A group of HTTP requests (or APIs) can be organised to be executed in sequence
  • and each HTTP request is represented as a test case
  • and each test case can be validated with the help of assertions and response validators.

Now, we will walk through step by step, how you can validate a REST API in vREST NG.

Suppose we want to validate the Create Contact API of our sample test application. The API has the following structure:

API Endpoint

http://example.vrest.io/contacts/v1/test/contacts

Request Method:

POST

Request Body:

{
  "name": "<String>",
  "email": "<String>",
  "designation": "<String>",
  "organization": "<String>",
  "country": "<String>",
  "aboutMe": "<String>",
  "twitterId": "<String>",
  "facebookId": "<String>",
  "githubId": "<String>"
}

Request Headers:

{
  "Content-Type": "application/json"
}

Note: This API doesn't need any authentication / authorization. Because that will make this guide more complex. For more information on this, read authentication / authorization.

We request you to please follow the below steps in your vREST NG application also as described.

Now, let us take step by step approach on, how we can validate this API in vREST NG.

To create a test case, you need to open your project. This will land you on the Test Cases tab (located in the left sidebar)

Creating Test Case

Once you are in the Test Cases tab,

  • Click on the button on the left pane.
  • A window will pop up saying "Add New Test Case".
  • Select the Test Suite
  • Select your API's request Method.
  • Enter your API URL in the Test Case URL field.
  • Provide a summary describing your test case, in the Test Case Summary field.
  • Click

Voila! You have created your test case.

To open this test case, click on the test case created from the list of test cases on the left pane. Test case will open on the right side of this pane.

Now is the time to define the request body for your API.

Defining Request Body

To define the request body of your API

  • Go to Request Tab in your test case
  • Under the Body Sub-Tab, you will see the section to enter your request body in JSON format.
  • (You can enter the request body in JSON, XML, Text, or Form URL Encoded form).
  • After you have entered the request body information, click on button.

Now, you need to define the request headers.

Defining Request Headers

To define request headers,

  • Go to Headers Sub-Tab under Request Tab.
  • Click on (A new row will be added to your list of headers).
  • Click on Enter Value for Header Name and enter the header name.
  • Then provide the Header Value.

If you are willing to test your APIs over certain conditions, then you need to create Assertions in vREST

Define assertions for Validation and Execute the test case

In Validation tab, define the assertions for your test case response. It totally depends on you, how you want to validate your response. There is a default assertion already defined for status code verification. Since, our sample API returns 200 as the status code.

Let us try to execute this test case. To execute this test case, simply click on

We validated the test case with simple status code verification. Let us try to add some more assertions.

  • Assertion to verify header 'Content-Type'
  • Assertion to verify response content. We are verifying the response via invoking Default Validator. Default Validator simply matches the expected response body with actual response body received. This feature is available only in Pro version. In community edition, you may add JSON Body assertions to validate the API response.

Define the above assertions in Validation tab as shown in below image:

Now, when we execute this test case, the test case will fail. Now go to "Results" tab to see the failure reason. The test case has failed because we have not set any Expected Body for the test case, as you can see in the Diff Report. Default Validator needs this expected body to compare with the actual body.

Click on to copy this actual body to expected body for the test case. You can see that our test case's expected body has been automatically set to actual body.

Now run the test case again. This time, the test case has failed again.

This time, the test case has failed due to some dynamic properties "_id" and "createdOn" in the API response. We can ignore these dynamic properties by using a special variable {{*}}. We can modify our expected body as below:

Now, run the test case again. This time, the test case passes.

In this manner, you can create a suite of positive and negative test cases by copying and pasting the above test case and modifying particular request details.