Do you think IIT Guwahati certified course can help you in your career?
No
Introduction 📃
API stands for an application programming interface. It acts as a mediator between two applications allowing them to communicate. An existing API can be integrated with your application to provide new features.
This article will discuss validating APIs in Ready API. We will also discuss available assertions you can use. To learn more, visit Ready API.
What is Ready API?
Ready API is an API test automation tool. It is used to test the working and functionality of APIs. It is done by using the assertions on virtual services. You can scale and automate the testing process for API with the help of Ready API. You do not have to write code to perform tests on Ready API.
A virtual service is an abstraction of the actual API that imitates the behavior of an existing API. It is also called API Virtual. When working with a virtual service, it becomes crucial to validate the request received and responses sent by the virtual service. To do so, we can use assertions or scripts.
Verifying Incoming Requests
The incoming requests to a virtual service can be validated with the help of assertions. Assertions do not replace the validation performed by your web application, but they make sure that the requests received by the web service from the client code are valid.
Verifying Virtual Responses
A virtual service is also responsible for sending responses, so you must also verify the responses. You can create functional tests to check whether the virtual service returns the correct responses.
For instance, you can create a functional test to do the following.
Send requests to your virtual service.
Validate the responses using assertions.
Why do we use Assertions?
In Ready API, virtual services validate the input data received by the client code using assertions. The server code should also run a check and return an error in case of invalid input data. Assertions are extra quick check-ups to validate the input data sent by the client code before it reaches the server code.
Service-Level and Operation-Level Assertions
There are two levels to applying assertions. You can apply assertions on separate operations or to the entire virtual service.
Operation Level Assertions: works for the incoming requests of a specific operation.
Credit: support.smartbear.com
Service Level Assertions: works for all incoming requests to a virtual service.
Credit: support.smartbear.com
Note: The requests that do not have defined responses are not validated. The virtual service will report the 500 Internal Server Error code if such an operation is simulated. However, the assertion will not fail.
Note: You can create any number of assertions. They will be executed according to the order on the assertions list.
How to Create an Assertion
You can easily create assertions by following the steps below.
Create a Service-Level Assertion
To create a service-level assertion, follow these steps.
From the Navigator Panel, select your virtual service.
Click + in Assertions from the editor.
Fill in the details about your assertions type and properties in the corresponding dialogues.
Credit: support.smartbear.com
Create an Operation-Level Assertion
Follow the below steps to create operation-level assertions.
From the Navigator Panel, select your virtual service.
From the editor of the virtual service, switch to the Setup Page.
Select the desired operation from the Setup page.
Click + in Assertions from the editor.
Fill in the details about your assertions type and properties in the corresponding dialogues.
Credit: support.smartbear.com
Preparing Sample Data
The special toolbar commands and conditions make it easier to create validation conditions for assertions. These are present in the Assertions properties dialogue. You can use these commands to create conditions to verify the working of your virtual service.
You will need sample data to run your assertions. To get the sample data, you must send a request to the virtual service.
Run your virtual service.
Simulate the given request to the service. This can be done using client nodes, API nodes, browsers, or functional tests.
Available Assertions
Add the necessary assertions in the Add Assertions dialogue. Your virtual service will be validated on the assertions selected by you.
The following are the available assertions you can use to validate the working of SOAP, JMS, and REST virtual services.
Property Content
Following are some of the common assertions with their description.
Assertions
Description
Contains
This assertion checks whether specific data is present in the incoming request body. If the data is not present, the assertion fails.
Not Contains
Checks whether specific data is not present in the incoming request body. If the data is present, the assertion fails.
Following are the assertions available forXML request bodies.
Assertions
Description
Message Content Assertion
Let you create complex checks made up of smaller checks for XML bodies. Each smaller check may have its own condition and expected outcome.
XPath Match
Checks whether a specific node(element or attribute) exists, and if yes, then with the expected value or not. XPath expression is used to extract the data from the request body.
XQuery Match
Checks whether specific data is present or not in the incoming request body. XQuery code is used to extract the data from the request body.
Following are the assertions available forJSON request bodies.
Assertions
Description
JsonPath Count
Matches the number of times a particular element occurred in the incoming request body with expected value. The assertion fails if the count does not match the expected value.
JsonPath Existence Match
Checks whether a particular element is present or not in the incoming request body.
JsonPath Match
Extract the data from the incoming request body and check if the value matches the expected value.
JsonPath RegEx Match
Extract data and applies a regular expression to it. Check if the result matches the expected value.
Note: JsonPath expressions are used to extract the data from the request body.
Compliance, Status, and Standards
Assertions
Description
HTTP Header Exists
Checks whether a specific HTTP header exists or not. Fails if not exist.
HTTP Header Equals
Checks whether a specific HTTP header exists with an expected value or not. Fails if not exist or have unexpected value.
Swagger Compliance
Checks whether the request complies with a particular swagger definition.
Script
Assertions
Description
Script
random checks are performed on the incoming request body using a Groovy script. The script returns true or false to specify whether the assertion passes or fails.
Viewing Assertion Results
The assertion results are stored in the Transaction Log. You can follow the below steps to view the transaction log.
Open the Transaction Log page from the Service editor.
You can see the failed assertions in the Assertions section. You can also see the cause of failure. (failed assertions are marked with red dots.)
To get details on the failed assertions, click on the operation and see the editor on the right.
Credit: support.smartbear.com
Frequently Asked Questions❔
Why do we use assertions in Ready API?
In Ready API, virtual services validate the input data received by the client code using assertions. Assertions are extra quick check-ups to validate the input data sent by the client code before it reaches the server code.
What are the different levels of assertion in Ready API?
There are two levels of assertions in Ready API.
Operation-level Assertions: Works only for incoming requests with specific operations.
Service-level Assertions: Works for all incoming requests.
What is virtual service in Ready API?
A virtual service is an abstraction of the actual API that imitates the behavior of an existing API. It is also called API Virtual. You can use the virtual service to validate the working of an actual API.
What does the Script assertion do in Ready API?
Script assertion performs random checks on the incoming request body using a Groovy script. The script returns true or false to specify whether the assertion passes or fails.
How can you verify the working of a virtual service in Ready API?
You can use assertions. Assertions have some conditions and expected outcomes. You can use them to verify the working of a virtual service in Ready API.