An
application programming interface (API) at
its core is a formal specification that acts as a guaranteed contract between
two separate pieces of software. We can also define API as an interface between two
software applications by allowing them to communicate with each other.
APIs
have become the focal point of programming improvement, interfacing and transferring
information and rationale across frameworks and applications. Fortunately,
testing them can enormously improve the productivity of your testing procedure
overall, helping you convey programming quicker than at any other time.
Applications often have three layers:
The API layer contains the business logic of an application – the rules of how users can interact with services, data, or functions of the app. Since the API or service layer directly touches both the data layer and the presentation layer, it presents the sweet spot of continuous testing for Testing and Development teams.
API testing involves testing the application programming interfaces (APIs) for functionality, security, Performance. Since APIs lack a GUI, API testing is performed at the Service layer.
1.Unit Testing: For testing the usefulness of individual activity.
2. UI Testing: For testing the usefulness of UI as a component of start to finish coordination tests to guarantee the UI capacities true to form.
3. Functional Testing: Functional testing is simply a test of specific functions within the code base. These functions represent specific scenarios (successful cases) and error handling.
Due to the nature of the test, we should expect a few stated responses. We should expect either an error (and thus, the appropriate error codes and handling instructions) or a corrected response that bears the material we’ve requested.
Functional testing should be done with the positive scenarios as well as both errata and edge cases.
4. Load Testing: For testing the Functionality and execution under Load conditions.
Load testing takes on a few different scenarios in order to ensure peak performance. The first of these scenarios is called the “baseline“, and tests the API against the theoretical regular traffic the API expects in normal, day-to-day usage.
A second load test is generally done with the theoretical maximum traffic. This is done to ensure that even during full load, methods are in place to safely throttle requests.
Finally, an overload test is performed to test the theoretical maximum and to add 10–20% additional traffic on the top. While this type of testing anticipates some sort of failure, it is more like a test of the API functionality to validate the error code generation and to ensure any failures are handled during high load.
5. Error Detection: For recognising any blunders, for example, special cases and asset spills.
This type of test is focused on the actual running of the API. Other tests are concerned with the result of implementing the API in a scenario, error detection is concerned with the universal results of utilizing the API codebase. These types of tests have one of the following focuses:
• Monitoring: Compiled code is tested for various implementation errors, handler failures to ensure there is no security issue in the code base.
• Execution Errors: The code should respond to valid requests in a predictable, known way, and should fail invalid requests.
• Resource Leaks: Invalid requests, purposefully overflowing commands, and other “illegal but common” types of requests are submitted to the API to test for memory, resource, data, or operational leaks and insecurities.
• Error Detection: The code is put through known failure scenarios to ensure that errors are identified and handled.
6. Usability Testing : It is easy to hit the APIs on different platforms and test the usability of a software.
7. Security Testing: Security testing also includes penetration testing, and fuzz testing.
Security Testing includes validation of encryption methodologies and validating the design of the access control through user rights management and validating authorisation checks.
In Penetration testing, API is attacked by someone with limited working knowledge of the API to assess the threat vector from outside. These attacks can be limited to specific functions, resources, or processes, or can target the entirety of the API.
In Fuzz testing, massive amounts of random data, known as “noise” or “fuzz,” is forced into the system in order to attempt a forced crash, overflow, or any other negative behaviour. This is done to test the API at its absolute limits, and serves somewhat as a “worst case scenario.”
8. Interoperability and WS Compliance testing: This sort of testing applies to SOAP APIs and checks for following two aspects:
Interoperability between SOAP APIs is checked by ensuring conformance to the Web Services Interoperability profiles. By conforming to these guidelines and utilising these tests, interoperability between SOAP APIs can be confirmed
Secondly, WS-* compliance is tested to ensure standards such as WS-Addressing, WS-Discovery, WS-Federation, WS-Policy, WS-Security, and WS-Trust are properly implemented and utilized.
9. Discovery Testing : API testing helps in identifying APIs where the resources are not exposed.
Infiltration Testing: For identifying any vulnerabilities of an application from aggressors.
1. Deciding What to Test and what not i.e Create test cases for all possible API input combinations to get complete test coverage
2. Use Realistic Data
3. Don’t Repeat Yourself.
4. Write clear tests that easily enable debugging.
5. Design the tests to run under different SUT configuration options.
6. Test Positive and Negative Outcomes
7. Use Data to Drive Dynamic Assertions
8. Track API Responses
9. Test the API for failures; until you get the output as failed.
10. Reuse test cases and monitor the API in production.
Step 1: Create API testing requirements. To be able to plan API tests, we need to answer following questions:
Step 2: Establish the API test environment. After the functional scope, the next step is setting up an API test environment. It requires the configuration of the servers, databases, and every resource the API interacts with, depending on the project requirements.
Step 3: Make a trial API call. Before starting the testing, perform a Sanity test i.e. make a test API call just to check the API is operational.
Step 4: Define the input parameters. Plan all possible input combinations to verify the results to determine whether the API performs as expected.
Step 5: Create API test cases. After all the preparations are done, it’s the time to write and execute test cases followed by compare actual results with the expected ones. A good practice is to group them by test category. Examples of API test cases may include:
JSON stands for JavaScript Object Notation and was designed to be a lightweight data interchange format. JSON is definitely becoming more popular and is now replacing XML in certain situations for API data exchanging. The site www.json.org describes how JSON is built on two structures:
1. “A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
2. “An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.”
1) SOAP Envelope – The SOAP Envelope is always the top element in the message.
2) Header – the Header is optional and is the first child element to appear after the envelope. Headers can contain different types of application-specific information like security authentication or session management info.
3) Body – Sometimes referred to as the payload, the Body contains the actual message that shows the information for the recipient of the message.
10.1 GET Method
10.2 POST Method
10.3 PUT Method
10.4 DELETE Method
The HTTP response codes are divided broadly in five categories :
• 1xx – Informational codes
• 2xx – Success codes
• 3xx – Redirect codes
• 4xx – Client error codes
• 5xx – Server error codes
Some of the most common HTTP response codes used with REST are as follows :
Having the right process, tool and solution for API test are critical for success. Below are the tools which are quite popular in API testing space:
I’ll give a list of API Testing tools in next article where we ‘ll see some different API Testing tools.