Automated tests for API and services are very important so there are lots of solutions for API test automation in the market. These solutions can be
commercial-off-the-shelf (COT) or opensource libraries also, there are many
frameworks created around these opensource libraries. Testing APIs with tools
that have IDE can be easy to start and learn but it can be challenging to
customize for your unique problems or integration to CI/CD tools can be hard.
Therefore I can suggest you use one of the opensource libraries by implementing it with your favorite language. In this way, it can run faster, easily extendable, and configurable. I have created a
framework with Airborne and Rspec for automating API tests, you can check it from Github. In this post, I want to explain what is Airborne and how I implemented a framework around it. You can simply update the environment variables and write your specs file in the `specs` folder.
What is API Testing
API stands for an application programming interface. APIs are providing an interface of your system to the world. API's endpoints can be called by requests and these calls return relevant data. An API can be considered as a source for clients such as web, android, ios, desktop applications, or even other APIs. Testing APIs from these clients will be difficult, expensive, and slow, therefore it is very important to test APIs as soon as any changes are introduced in the codebase.
API testing includes calling endpoints of APIs with relevant data and credentials. Each call should be asserted in terms of validity and verification. Some of the scenarios can be end-to-end tests, which means that you can not test these scenarios in one single call, instead, you need to call several API calls in accordance with your user-scenarios. All these works called API testing. API testing can be performed manually with some tools or you can write your automation project and run them automatically. In terms of `exploratory testing`, you should adapt your testing tools to perform manual testing for exploration.
What is Airborne
Airborne is a GEM created to test APIs on top of the Rspec and RestClient. Airborne provides a framework to write automated tests for APIs. There are many tools and libraries created for API test but Airbone is Ruby equivalent and it provides a simple response check so that you can whole JSON response at on with expected data or type of data. It also uses the RestClient for handling HTTP requests, which is very mature and reliable. If you have a struggle using Airborne you can call RestClient directly in your test either. The bad part is that Airborne doesn't have good documentation but once you start writing simple tests it becomes easier.
How to Write Tests
Airborne is an abstraction layer on top of rest_client, it makes testing easier with providing simple syntax. Airborne provides basic HTTP methods such as get, post, put, patch, delete, head, and options requests. You can use any of these methods to write your tests, see the example below:
Testing needs assertion, for API testing assertions are written to check the object types, object values in the JSON. Airborne has built-in assertions to check the JSON effectively. These are the Rspec based asserting functions:
expect_json_types
- Tests the types of JSON property values returnedexpect_json
- Tests the values of the JSON property values returnedexpect_json_keys
- Tests the existence of the specified keys in the JSON objectexpect_json_sizes
- Tests the sizes of the JSON property values returned, also test if the values are arraysexpect_status
- Tests the HTTP status code returnedexpect_header
- Tests for a specified header in the responseexpect_header_contains
- Partial match test on a specified header