Monday, October 28, 2013

API (Application Programming Interface) and API Testing

 
API (Application Programming Interface)

 
 
 
 
 
 
 
 


An API (Application Programming Interface) is a collection of software functions and procedures, called API calls that can be executed by other software applications.
An API (Application Programming Interface) is a collection of software functions and procedures, called API calls, that can be executed by other software applications. API testing is mostly used for the system which has collection of API that needs to be tested. The system could be system software, application software or libraries.
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.
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 (1234, 5656)
Exceptions have to be handled if number is exceeding the integer limit.
Consider the below API function -
·         Lock()
·         Unlock()
·         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.
 
API Testing

 
 

API testing is different from other testing types as GUI is rarely involved in API Testing. Even if GUI is not involved in API testing, you still need to setup initial environment, invoke API with required set of parameters and then finally analyse the result.
API testing is mostly used for the system which has collection of API that needs to be tested. The system could be system software, application software or libraries. API testing is different from other testing types as GUI is rarely involved in API Testing. Even if GUI is not involved in API testing, you still need to setup initial environment, invoke API with required set of parameters and then finally analyse the result.
Setting initial environment become complex because GUI is not involved. It is very easy to setup initial condition in GUI, In most cases you can find out in a glance whether system is ready or not. In case of API this is not the case, you need to have some way to make sure that system is ready for testing.
This can be divided further in test environment setup and application setup. Things like database should be configured, server should be started are related to test environment setup. On the other hand object should be created before calling non static member of the class falls under application specific setup. Initial condition in API testing also involves creating conditions under which API will be called. Probably, API can be called directly or it can be called because of some event or in response of some exception.
Or
Setting initial environment become complex because GUI is not involved. In case of API, you need to have some way to make sure that system is ready for testing. This can be divided further in test environment setup and application setup. Things like database should be configured, server should be started are related to test environment setup. On the other hand object should be created before calling non static member of the class falls under application specific setup. Initial condition in API testing also involves creating conditions under which API will be called. Probably, API can be called directly or it can be called because of some event or in response of some exception.
Output of API could be some data or status or it can just wait for some other call to complete in a-synchronized environment. Most of the test cases of API will be based on the output, if API Return value based on input condition
This is relatively simple to test as input can be defined and results can be validated against expected return value. For example, It is very easy to write test cases for int add(int a, int b) kind of API. You can pass different combinations of int a and int b and can validate these against known results.
Return value based on Input Condition
Relatively simple to test as input can be defined and results can be validated.
Example: It is very easy to write test cases for int add(int a, int b) kind of API. You can pass different combinations of int a and int b and can validate these against known results.
Does not Return Anything
Behavior of API on the system to be checked when there is no return value.
For example, if you need to write test cases for delete(ListElement) function you will probably validate size of the list, absence of list element in the list.
Trigger some other API/event/interrupt
If API is triggering some event or raising some interrupt, then you need to listen for those events and interrupt listener. Your test suite should call appropriate API and asserts should be on the interrupts and listener.
Update Data Structure
This category is also similar to the API category which does not return anything. Updating data structure will have some effect on the system and that should be validated. If you have other means of accessing the data structure, it should be used to validate that data structure is updated.
                                         
Modify Certain Resources
If API call is modifying some resources, for example updating some database, changing registry, killing some process etc., then it should be validated by accessing those resources.
You should not get confused with API Testing and Unit Testing. API testing is not Unit testing. Unit testing is owned by dev team and API by QE team. API is mostly black box testing whereas unit testing is essentially white box testing. Unit test cases are typically designed by the developers and there scope is limited to the unit under test. In API testing, test cases are designed by the QE team and there scope is not limited to any specific unit, but it normally cover complete system.
Main Challenges of API Testing can be divided into following categories.
·         Parameter Selection
·         Parameter Combination
·         Call Sequencing
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
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
  
 

No comments:

Post a Comment