API Connect

 View Only



LinkedIn Share on LinkedIn

API Testing : How to test dependent APIs

By Donal Joyce posted Mon December 16, 2024 12:21 PM

  

Why do we need API Testing

We need to be sure that:

  • APIs are fully tested before they are promoted to production
  • Ensure API calls are getting completed successfully
  • APIs are returning valid results
  • Any issues in API calls (performance, quality, etc.) are known to the development team quickly .
  • To debug and trace API logs in case of any issues.

Why API Connect Testing Tool

How can it help your API development Journey

  1.           Zero code – Test cases can be built without any code needing to be written
  2.          Automate all tests – Once a test is built it can be put into an automation bucket to be run.

To allow for faster delivery and support these quality requirements we need to automate the process for testing of APIs as much as possible.

API Testing Tool

IBM is providing an API Testing solution to address these needs.

To ensure quality with API testing, the API definition (e.g. OpenAPI) is used to generate test cases.  This supports the need to execute comprehensive tests quickly.  Rather than the API developer manually creating a set of test situations, the automatic generation of tests based on the API interface can provide a more comprehensive test suite.  And, because this is generated automatically with no code required, the time and effort to generate the tests is minimal.

How to use API testing in your API development journey with API Connect

Suppose you have a published API in API Manager and you like to test it using API Testing tool

We can start test development with creating a test suite , test suite is an organised collection of related test cases

Once a test suite has been created we can add multiple test cases to it .
There are four ways to create a test case with in a test suite

  1.           From spec (build with smart generation)

  2.           From Specification

  3. From API Endpoint 
  4. From Scratch

1. From spec (build with smart generation)

Smart generation feature in Testing tool analyses the openAPI spec and then assertions for multi-step calls are automatically created to save time and reduce errors.

For example, when testing a GET call, it is necessary to first perform a CREATE call (POST call) to generate a resource. The GET call can then be tested on the created resource, and afterward, the resource should be deleted. The smart generation feature handles this task for you.

Steps to generate a test caseSteps to generate a test case 

In the above provided example, the Petstore API is utilised to generate test cases. This test case targets a GET call for the endpoint store/order/{orderId}. The assertion test for GET call includes automated dependency setup (via a POST request) and teardown actions (via a DELETE request).

1.1 Using response value in multi-step calls

If we need to use the response from a previous call in the next subsequent call we can achieve it using var field

 
Suppose that if we need to use the token value obtained from a login call in the subsequent calls , 

For example , if https://example.com/login api call give response as

{
token: <token_value>
}

and we need to use this token value in the next call , we can follow the given example model.

- type: request

      method: post

      url: https://example.com/login

      var: payload

      mode: json

      body:

        user: abc

        id: abc

    - type: request

      method: get

      url: https://example.com/examples

      var: examplesData

      headers:

        Authorization: Bearer {{.payload.token}}

Here response data from https://example.com/login is stored in variable “payload”  as per the line var: payload
So this response value can be used in subsequent calls whenever needed , in the given example we are using the token value from the first call in the header values of next get call as  
Authorization: Bearer {{.payload.token}}

The response value from previous calls can also be used in url field of subsequent calls.

Note : whenever trying to use the response value from previous calls in next call’s header values use this format of .<variable_name>.<key>

If the dependent calls are not auto generated in the test assertions, we are expected to include them manually along with the generated assertions.
For example as shown in the below code , the order id is required for the get call made in line number 31. So in order to meet the dependent parameter a POST call is made on line number 16 prior to the GET call and the order id received as a response from the POST call is used as a path parameter in the GET call

test for dependent call

 

2. From Specification

This method is similar to  From spec (build with smart generation) . The key difference is that, for multi-step calls, all dependency setup and teardown calls must be added manually, as they are not automatically included.

3. From API Endpoint

If we wants to create a test case corresponding to an API response , From API Endpoint option can be used

Test from endpoint

Here we have to make an API call by providing the API details such as method , endpoint , request headers and body. Then assertions can be generated against the response we get for the request

4. From Scratch

If we want to create every assertions and calls from scratch we can make use of this option

Run Testcase

Once testcase has been created  we can start running our generated tests. We can see the detailed test report after running the testcase as mentioned in Run test case

ATM Webhooks

Once tests has been generated they can be invoked from outside API Connect Test Interface using the webhook functionality provided by ATM

0 comments
125 views

Permalink