Sunday 4 November 2012

API Testing



API Testing

What is an API?

API is an acronym for Application Programming Interface.
It enables communication between two separate software system. A software system implementing an API contains functions/sub-routines which can be executed by another software system.

What is API Testing?

API testing is checking Application Programming Interface of a Software System.
In order to check API , it needs to be called from the calling software . Necessary test environment, database, server, application setup needs to be prepared before doing API Testing.
 API testing is focused on the functionality of the software’s business logic and it is entirely different from GUI testing. It mainly concentrates on the business logic layer of the software architecture. This testing won’t concentrate on the look and feel of an application.
The interesting problems for testers are: 
1. Ensuring that the test harness varies parameters of the API calls in ways that verify functionality and expose failures. This includes assigning common parameter values as well as exploring boundary conditions. 
2. Generating interesting parameter value combinations for calls with two or more parameters. 
3. Determining the content under which an API call is made. This might include setting external environment conditions (files, peripheral devices, and so forth) and also internal stored data that affect the API. 
4. Sequencing API calls to vary the order in which the functionality is exercised and to make the API produce useful results from successive calls.

Set-up of API environment and different types of test cases:

Normally, setting up the API environment is very complex as it doesn’t involve user interface. Database and server should be configured as per the application requirements. API Function should be called to check whether that API is working.
Output of API could be
  • Any type of data
  • Status (say Pass or Fail)
  • Call to another API function.
The output is compared with the expected outcome. API test cases are written based on the input data that users are expected to key-in.
For example –
There is an API function which should add two integer numbers .
1.Long add(int a, int b)
The numbers have to be given as input parameters. Output should be summation of two integer numbers . This output needs to be verified with expected outcome.
Calling needs to be done such as
1.add (12345656)

Exceptions have to be handled if number is exceeding the integer limit.

Consider the below API function -
  1. Lock()
  2. Unlock()
  3. Delete()

They return any value such as True (in case of success) or false (In case of error) as an output .
A more accurate test case would be , can call the functions in any of the script and later check for changes either in the database or the Application GUI.

Calling of another API / Event

In this case , we call one of the API function which in turn will call another function.
For example – First API function can be used for deleting a specified record in the table and this function in turn call another function to REFRESH the database.


Approach of API Testing:


Following points helps the user to do API Testing approach:
  1. Understanding the functionality of the API program and clearly define the scope of the program
  2. Apply testing techniques such as equivalence classes, boundary value analysis and error guessing and write test cases for the API
  3. Input Parameters for the API need to be planned and defined appropriately
  4. Execute the test cases and compare expected and actual results.

 

Difference between Unit and API Testing:

Following are the differences between unit testing and API testing

Unit Testing
API Testing
Performed by developers
Performed by testers
Separate functionality is tested
End to End functionality has been tested
Developer can access the source code
Testers usually cannot access the code
UI testing is also involved
Only API functions are tested
Only Basic functionalities are tested
All functional issues are tested

API Test Automation:


Since API and unit testing both target source code, similar tools can be used for testing both.
If an API method is using .NET code, then the tool which is supporting should have .NET
Example for Automation tools are
  • NUnit for .Net
  • JUnit for Java

There are two distinct typesof API testing:
API Functional Testing:
Functional testing takes an external perspective of the API, using the API Specification and published Use Cases to determine the valid inputs and expected outputs.This is classic "black box" testing, without an intimate knowledge of the API's internal structure. A typical API Test developed by INNOMINDS covers the following areas:
  • Baseline Tests
  • Task-Oriented Tests
  • Forced Error Tests
API Performance Testing:
Performance API testing encompasses a wide range of activities, including creating a test environment, setting realistic performance targets, developing test scenarios, generating high quality test input data, test execution, and root cause analysis. Innominds with years of experience in performing Performance and Load Testing of disparate application and systems is well positioned to serve your API performance testing needs. Our services in this spectrum includes
  • Load Testing
  • Stress Testing
  • Endurance Testing and
  • Scalability Testing
Innominds API testing methodology includes the following steps:
  • Detailed review of the API specification and any related use case documentation.
  • Test Specification Development
  • Test Framework Development
  • Test Case Development
  • Test Execution and Reporting

Best Practices of API Testing:

There are some best practices for effective API testing:
  • Planning the test cases – What and How to test the API functions.
  • Important to organize the test properly so that those API can be organized properly.
  • Parameters selection should be explicitly mentioned in the test case itself.
  • Call sequencing should be performed and well planned.
  • Prioritize API function calls so that it will be easy for testers to test.
  • Proper testing and test cases should be done while handling one time call functions like – Delete, CloseWindow, etc….

Challenges of API testing:

There are various challenges when we are doing API testing:
  • No GUI available to test the application which is difficult to give input values.
  • Verifying and validating the output in different system is little difficult for testers.
  • Parameters selection and categorization needs to be known to the testers.
  • Exception handling function needs to utilized and used properly.
  • Testers should be knowledgeable in coding.
  • Parameter selection.
  • Parameter combination.
  • Call sequencing.

 

Conclusion:

API consists of set of classes / functions / procedures which represent the business logic layer. If API is not tested properly, it may cause problems not only the API application but also in the calling application.

I am very thankfull to all the groups who shared this valuable API Testing information.  Thank you.