API Testing Primer: An Introduction

A word about API applications

A simple API system diagram. In this example the API application is installed on a server with an in-memory cache and database and also integrates with a third party API. Each component in this system diagram is a point of integration that requires testing. More on this when we discuss integration testing.


API Testing Tools

  • Postman
    • Postman is a browser-based UI platform for testing APIs. Users can enter the endpoints, define the headers, provide parameters, and payloads to execute an API request and see the response. User can organize their API requests into collections and share them with other users.
  • Hoppscotch
    • Hoppscotch is similar to Postman but can be installed locally or on your own server for browser-based access. This gives you all the benefits of Postman without all the disadvantages of cloud based products you have no control over.
  • Swagger
    • Swagger is another browser-based API testing solution. The difference with Swagger is that the API requests are mostly set up automatically using a JSON definition of the API. Development teams may provide Swagger endpoints which list all the available API methods that can easily be accessed.
  • Insomnia
    • Insomnia is another browser-based API testing solution that offers a more simplistic and easy to use design.
  • SoapUI
    • SoapUI is a free desktop API testing tool that has a long history in the software QA community. SOAP based APIs can be tested with this tool.
  • ReadyAPI
    • Ready API is a commercial tool for API testing offering functional testing, performance testing, and other tools.
  • RestAssured
    • RestAssured is a Java library for testing APIs making this a code-based solution to API testing without a UI. Code-based API solutions may appeal more to those looking for a build runtime or CI/CD testing solution.
  • Karate DSL
    • Karate DSL is another code-based API testing solution built on the Cucumber library with test cases defined in Javascript. Code-based API solutions may appeal more to those looking for a build runtime or CI/CD testing solution.
  • JMeter
    • JMeter is a desktop/server application for API functional and load testing. JMeter provides a UI for creating tests which can then be run from either the UI or command line. JMeter tests can be set up to access external data sources, implement flow controls and logic, and run custom coded assertions.
  • K6
    • K6 is a code-based API load testing application. Scripts are written in Javascript.
  • Selenium, Cypress, Playwright
    • Selenium, Cypress, Playwright are popular UI test automation tools that can also be used to code-based API testing. If you have an existing UI test framework using the same solution for API testing may provide an advantage.

API Testing Types

Integration Testing

  • Databases
  • Authorization systems
  • Third party data providers
  • Email Handlers
  • Payment Providers
  • Logging and Error Handling Systems
  • Caching Schemes
  • Scheduled Job Execution
  • Queuing Services

Contract Testing

  • HTTP methods (POST, GET, PATCH, DELETE)
    • A definition of which HTTP methods are supported by the API.
  • Endpoint URLs and paths
    • The domain the API can be accessed at as well as the path to access the API
  • Headers
    • The HTTP headers that are required by the API for the request to be served. This can include authentication headers, content-type, and user agent data.
  • Parameters
    • The listing of parameters and data types that the API requires for requests to be processed.
  • Post body format
    • The specification for the data that is being sent to the API such as if it requires XML or JSON and the schema of the data with respect to parent and child objects.

  • HTTP status codes
    • Numeric codes that will be included in the HTTP Status header that define the response contents. Some examples include 200 for a successful response or 500 for a server error.
  • Headers
    • A listing of response headers such as content-type and cache status information
  • Response Body
    • The specification for the data that is being sent by the API such as if it is XML or JSON and the schema of the data with respect to parent and child objects.

Functional Testing

  • Parameter testing
    • Testing each request parameter and parameter value. For instance if a parameter supports an enumerated list of values then each values will be tested.
  • Response testing
    • Similar to the request parameter testing, each response field will be tested including verifying that each request value is included in the response.
  • Data Validation
    • During this testing we check to verify that the data that is returned in a response is correct. This may include comparing the API response data with the source data such as the database that the data is sourced from. The objective of this verification is to confirm that the data in the database and the application of any business rules produces the same data that is in the API response.
  • Error Handling
    • Error handling testing involves sending invalid requests to the API to verify that it properly handles the requests. The types of invalid requests may include unauthorized, improperly formatted, improperly encoded, or requests that violate business rules.
  • Boundary Testing
    • Boundary testing is testing to verify that parameter values near or over a limit are properly handled by the API. For instance if an API parameter takes a value of 0 – 100 we will test that the correct response is provided when value ‘0’ and ‘100’ are sent. If an API accepts a CSV file as input with a limit of 50,000 records then we test with a CSV file with 50,000 records in it. We also test response fields with regard to boundary testing; verifying that given response field will return values at the minimum and maximum scopes.
  • Cache Policy Testing
    • Cache testing may or may not be required depending on if the API is caching any API responses on the server. An API may cache responses to increase performance and reduce the load on the database. API responses that retrieve data that does not frequently change are good candidates for caching. Testing involves making a request to an API (HTTP GET method) that caches the response and then repeating the request to verify that the data is cached and retrieved from the cache correctly. Further testing would involve updating or deleting the data that is served in the response and then making the GET request again to verify that the API serves the updated data or does not respond with the record if it was deleted.
  • Performance Testing
    • During functional testing we will get our first look at performance and load testing. Together with the product owners and development team a standard response time will need to be defined. Then during functional testing we will evaluate the API calls with different parameter values, responses, and business rules to verify that the API meets the response time requirements. The APIs must meet their performance requirements as a prerequisite for load testing to be performed.

Load Testing

  • The user or business flows to be included in the load test are determine and set. These can be flows such as first time user experience (FTUE), user registration, general search, navigation, and usage, checkout, and payment flows.
  • Discover which APIs are called during each of these flows.
  • Determine how many API requests per second are generated by a typical user executing these flows.
  • Determine the the expected amount of simultaneous users that are expected to execute these flows at peak usage periods.
  • Once the JMeter script has been created that simulates the API usage from the flows that we defined in the first set, use JMeter to simulate the number of users (called ‘threads’ in JMeter) that generate the expected number of API requests per second by the number of simultaneous users making API requests at peak usage

Security Testing

  • Authentication and Authorization Testing
    • Verification of the authentication process for API access. This can include the testing of authentication tokens, session tokens, and expiration of tokens.
  • Input Validation
    • Input validation testing may focus on verifying that the API is resistant to SQL injection and other injection attacks.
  • Rate Limiting
    • Verifying that the API detects excessive requests coming from a single source and appropriately limits the sender from making excessive requests to the API.
  • Logging and monitoring
    • Investigating any logging and monitoring policies that are put in place to ensure that they can detect and report attacks on the API.

What’s Next?