Automated your API testing with Postman and Jenkins
We’re currently live in an exciting age of intelligence, our digital world is connected stronger than ever. Many applications and IT services can integrate and create a huge data exchange network. And the unsung hero that help connected our world is API. No matter what you’re using or working on like ordering grocery on your phone, paying for it from a smartwatch, working on a website: you’re consuming an API. More and more APIs are being used for all variety of applications and devices.
Consider API is in the center of every connection in today world. If an API does not work correctly, it can cause chaos and suboptimal experience for end users. Also, poorly designed and poorly implemented APIs may lead to security vulnerabilities, small things like wrongly formatted JSON have the potential t cause hacks, data leaks and erosion of the company’s goodwill. Therefore, API testing needs to be performed thoroughly to ensure functionality, performance, and security.
Since Agile practices are being adopted by most of IT companies nowadays, the software product can be released at high speed and changed regularly. Therefore, we need to create an increased demand for new ways to quickly test them. This makes automation test become a critical factor to maintain agility. And luckily, API testing is one of the best candidates for test automation as API design can be controlled through a central definition and used throughout the software lifecycle and the automation pipeline. Test automation is achieved be integrating created test with automation tools. As for API testing, these tests can be created by SoapUI or Postman …, then plugged into a CI system like Jenkins, Travis, TeamCity, CircleCI, Bamboo and Go are all favored tools for Continuous Integration… to be executed or triggered automatically. These automation tools provide scheduling services and triggers for events like code commit. Whenever there is any change, QA team can bring in the change with very little effort. Also, these tools enable easy generation of reports from the text execution data.
When you perform API testing manually, you might find out that your test needs to be repeated over and over again. Have you ever think executing the same test cases manually whenever there is any change in code or a new version the product is released is a waste of time and budget? Also, your tester might get bored and lose motivation on their testing which can lead to careless and then some bugs might get escaped. Applying test automation to your testing process, then multiple iterations of your tests can be created easily. And more tests can be executed in a short period of time, regression test on your application will no longer be a problem because the API test mainly focuses on testing if your API can work with another. The automated test can also be run on multiple environments and should be dynamic enough that you can switch execution environments fast, hence ensuring that the code works fine irrespective of the underlying scenario. Automated test execution is good as manual execution and is compatible with test environments and reporting.
Just like some others type of automated test, automate API testing biggest strength are time saving and reliable. In real life projects, times are very precious, executed all test cases must be fast and accurate. Automated tools perform the same steps precisely every time they executed and always recorded the results. Thanks to that, testers freed from repetitive manual tests have more time to create new automated software tests and deal with complex features. And, if anyone has known about API, they should have known that API return value can be thousand line of data, can you properly check and analyze all of them carefully. Therefore, you need a tool to do that for you. Having a tool to run all of your tests all the time for continual coverage can help us get closer to bug-free deployments catching the error before your product are released, also by using the same test to monitor your API can actually give you a detailed insight into your functional uptime. The fact that a computer is always faster and more accurate than human will never change. Also, automation can do what manual testing cannot. Automated testing can simulate tens, hundreds or thousands of virtual users, which mean, thousand of the request can be sent to the API to see if your API can handle all of them.
But why we really need it?
There is a report that said API testing is much more suitable for test automation and continuous testing than UI testing – particularly with Agile and DevOps – because:
- API tests are less brittle and easier to maintain
- API tests can be implemented and executed earlier in each sprint
- API tests can often verify detailed “under-the-hood” functionality that lies beyond the scope of UI tests
- API tests are much faster to execute and are thus more suitable for checking whether each new build impacts the existing user experience
So, when and how can we automate it?
Your API tests are ripe for automation once you find that you must execute these tests multiple times within a short period of time. Automate API testing is simple in theory, you create the test then have it run automatically. A simple practice for automated your API testing:
First, call a Single Endpoint and analyze that payload. After you’ve done analyzed it, take a look at the HTTP header, such as: Response Code, Content-Type, then validate the entire payload object by object (special care to any critical objects) and making sure the schema is correct which mean the layout of the object has to be consistent every time based on our expectations, but also remember to confirm the data being transferred by API is as expected, too.
In advanced, the test you created for API testing must confirm the Logic Business, the logic is different in each industry and you will have to make sure the API can work well based on the logic business of each industry. Since API was born to work with others API, we need to use Dynamic Data in our testing. APIs are specifically meant to work together, one API output can actually use to feed that data in a subsequent API call. A long line of API calling each other really like randomize and use dynamic data to test.
Finally, Integration Test. Integration test means you have to be a normal end user of the product reproducing all the normal steps to get the information you want, it’s actually to validate that multiple steps our customers might go through.
Let’s see how it works!
In this demo we will quickly demonstrate how to write REST API tests with Postman and how to use Newman (the CLI runner for Postman) in Jenkins, using the Pipeline DSL. Here we go, we are going to use the following tools & technologies:
- Postman, as our API automation scripting tool.
- Newman, to make it possible to run the tests via simple command line.
- Jenkins, as our continuous integration tool.
Postman is also a popular tool for testing API services. It’s a plug-in within Google Chrome that acts as a powerful HTTP client to test web services. It’s especially popular within Agile teams as you can easily build a collection or library of REST calls, which can allow for more collaborative working between testing and development.
Jenkins – A Continuous Integration is a development practice that goes hand-in-hand with automation testing. In CI, Developers integrate code into a shared repository, which is verified by an automated build, which in turn allows problems to be detected early. It’s here that we integrate automated API tests in order to identify and fix issues as quickly as possible, lending to the efficiency of Agile delivery.
Our demo objectives:
- Create a simple test in Postman
- Create a project which stores the test collections
- Additionally, the project will define Newman as an NPM dependency
- Use Jenkins pipeline DSL to run the tests
- Configure the reports to show pass/fail tests
Enjoy the video!
API testing automation strategy is a major key to delivering faster. Compared to manual testing, automation testing leverages a tool to help speed up the process and remove human error. With the fact that many IT companies are getting to adopt API testing, applying automation test in their API test will become their strength in the testing product: time-saving, cost-saving and reliable results.
You can read more about API Testing in the article: Getting Started with API Testing