May 17, 2023
API Integration,Performance
0
1524
API testing with Postman and How to Automate Tests in Postman
What is API Testing?
- API testing is a type of software testing where application programming interfaces (APIs) are tested to determine whether they meet expectations for functionality, reliability, performance, and security. In simple terms, the purpose of API testing is to reveal bugs, anomalies, or deviations from the expected behavior of the API.
- API testing does not add to the look and feel of the software product. As an alternative to using standard input devices (such as a keyboard) or output devices (such as a monitor), API testing uses software to send calls to your API that you use to receive output during API testing and ultimately to your will do to document the response product.
What is postman and why do we need this tool?
- Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.
- Start using Postman to interact with Apis and write API tests.
- Send request:
- Always need to read the API documentation first. Check all status and which status code to use for particular request.
- Status code 200: Indicates that the request was successful.
API Documentation:
- View API from http://192.168.0.1:8033/swagger/index.html
- The API we are using uses the HTTPS protocol. HTTPS ensures the connection is encrypted.
- HTTP request message will consist of URL (address), request method (GET, POST, PUT, ...), header and body
- The HTTP response message will contain a status code (200, 404, 500, ...), a header, and a body.
- Collections and variables in Postman
- You can save requests so that you can use them again. All requests need to be added to the postman collection.
- Using collections, we will have postman collection for each api. We are storing the base address of the API in a collection variable called baseUrl. Our saved baseUrl will be displayed as {{baseUrl}} in the address bar. Variables allow us to avoid repeating the same information and save time. Variables allow us to make changes easily. A variable has two states such as INITIAL VALUE and CURRENT VALUE. If the variable adds to the INITIAL VALUE, then it will be available to others if you share the collection. If the variable adds to the CURRENT VALUE, then it will be private, and this is the value that Postman uses.
How to Pass the parameters in postman:
- Query parameters start after the? in the URL. For ex: {{baseUrl}}/ GetIndustryList?sEcho=1&iDisplayStart=0
- Multiple query parameters in the URL with an &. For ex: Echo=1&iDisplayStart=0
- In an API, some query parameters may be optional or mandatory. We can enable and disable the parameter by clicking on the checkbox.
POST request
- A POST request allows you to send data in the request body. Submitting requires industry certification.
- Some APIs are public and do not require authentication and some APIs are private and require authentication.
- An access token is a temporary password generated by the API. Valid JSON needs to be specified, otherwise the server will not understand the request.
- Use double-quotes for strings, separating key-value pairs with commas. Numbers and Booleans are not allowed between quotes. Postman will warn if the JSON is invalid.
How to Update the request:
- We need to use PATCH request. It is used to update existing data.
How to DELETE request:
- DELETE request is used to delete the data. If you try to retrieve the same data with a GET request, you will receive a 404-status code. it will indicate "not found"
Automation scripts (Test)with Postman
- Write Test automation
- We have done manual testing, but if writing API tests, avoid re-testing the API manually.
- We need to check the response to understand whether the API is working properly or not. Tests written in JavaScript.
- Testing the response status code is one of the easiest tests we can write.
- When writing tests, we want to make sure that the tests fail. We can use console.log() to view the contents of a JavaScript variable
Variables
- Postman variables are fundamental to automated testing of APIs. For variables we use postman environment (Environment Variables) if we are working in multiple test environments. Variables saved in the collection. Global variables are useful for all collections in scope.
How to get data from the response
- Having hardcoded values in requests can cause API tests to fail if the data changes. Always we need to use console.log() to see the data we are trying to set as variable.
How to execute the Request
- If you run the Postman collection, the default API is what you have in the collection. You can change the API we use postman.setNextRequest and specify the name of the next request If we want to stop the execution, we can do so by running postman.setNextRequest(null)
How to use the Collection runner
- Collection runner is a built-in functionality of Postman. It allows you to execute the entire collection with just one click.
- Make sure to check (v) the "Save response" box as this will allow you to inspect the response body.
- If any API response is going wrong, then the collection runner will indicate FAILED [red color] with a count. If the API response is correct, then the collection runner will indicate passed [green color] with a count.
How to use Postman monitors
- Postman monitoring lets you run a collection periodically to check for its performance and response. It will give continuous visibility into the health and performance of APIs.
- First create [configure] monitor settings.
- Select the collection to monitor.
- Schedule when to monitor the collection.
- Enter the name of the monitor.
- Select the corresponding environment.
- Select the region from which you want to test.
- Now click on Run then we can see test results for team and private collections.
Test results include the name of collection, status (healthy or failure), the percentage of average success rate, and the average response time.
*** Reference URL:
Postman API Platform | Workspaces
Writing tests | Postman Learning Center
Posted by: Smruti Burman
Sr Quality Analyst
Comments