Most important step in API development is to check its functionality whether it is constructed to simulate the use of the API by end-user applications and fulfill the provided business requirements. There are several API testing frameworks and tools for getting the job done. But comparing to other products, the postman is a tool that handy and easy to use and offering features like create and send any HTTP requests, write test cases to validate response data, response times, analyze the responses sent by the API, create integration test suites and integrate with build systems. Major characteristics of the postman is that it can dramatically cut down the time required to test and develop APIs adapts itself for individual developers, small teams or big organizations equally well. Here we will discuss the benefits of Test cases Postman for API Testing.
At our company, developers follow swagger to write documentation of APIs. So, it’s easy for a tester to get an idea what will be the response in the different scenarios.
In some cases, the response can be 200 status code or sometimes it can be 400, 404 or 500 status code with a proper message.
Postman Features
A few of the features using you can:-
- Simply test any API and see the response immediately.
- Create a collection of API endpoints and call them whenever you want to see the responses.
- Share these collections when you are logged in with team members.
- Use Postman’s CLI client, Newman, to write Postman tests with JavaScript.
- Write tests in JavaScript to test the response that comes when a Postman request is made.
- Finally, once you have all these endpoints created, you can export a JSON file, both for the endpoint tests and the environment variables, which again can be used by Postman as a source of for the tests.
What is a test in Postman?
With Postman, you can add scripts to your request to use dynamic variables, pass data between requests, and write tests. Code added under the Pre-request Scripttab will execute before your request is sent, and code added under the Tests tab will execute after your response is received.
Tests are scripts written in JavaScript that are executed after a response is received. Tests can be run as part of a single request or run with a collection of requests.
In the Postman app, the request builder at the top contains the Tests tab where you write your tests. The response viewer at the bottom contains a corresponding Test Results tab where you can view the results of your tests.
To start building test cases quickly, commonly-used snippets are listed next to the test editor. Select a snippet to append the code to the test editor. If needed, update the stub with assertions specific to your endpoint’s expected response. Then, send the request to view the test results at the bottom.
Test cases Postman
You can also write your own custom tests in JavaScript. In addition to supporting the older style of writing tests, Postman has a newer PM API (known as the pm.*
API) which is the more powerful way to write tests.
pm.test()
The pm.test()
function is used to write test specifications inside the Postman test sandbox. Writing tests inside this function allows you to name the test accurately, and ensures that the rest of the script is not blocked in case of any errors.
Some things to know about the pm.test()
function:
- The function accepts 2 parameters, the name of the test (as a string) and a function to return a boolean value.
- It can be used only in the Tests tab, after the primary Postman request has been sent.
Here are some examples: // example using pm.response.to.have pm.test("response is ok", function () { pm.response.to.have.status(200); }); // example using pm.expect() pm.test("environment to be production", function () { pm.expect(pm.environment.get("env")).to.equal("production"); }); // example using response assertions pm.test("response should be okay to process", function () { pm.response.to.not.be.error; pm.response.to.have.jsonBody(""); pm.response.to.not.have.jsonBody("error"); }); // example using pm.response.to.be* pm.test("response must be valid and have a body", function () { // assert that the status code is 200 pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants // assert that the response has a valid JSON body pm.response.to.be.withBody; pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed });
There are also other helpers to use in conjunction with pm.test()
.
pm.expect()
The pm.expect()
assertion function was built on the shoulders of the popular JavaScript test library ChaiJS BDD. Using a similar syntax, pm.expect()
makes it easy to write readable tests, and you can deal with assertions of data from a response or variables.
pm.response.to.be.*
The pm.resonse.to.be
object provides shorthands for frequently used response based checks. Using this family of assertions streamlines tests for response status types and body variations.
Check out these test examples and the Postman test sandbox to get started writing your own custom tests.
Test results
Now that you’ve written your tests, how do you know if they’re passing or failing?
After you run a request with tests, go to the Tests tab in the response viewer. You will see a list of your tests and whether the test has passed or failed. A boolean that evaluates to true is a passing test, and a boolean that evaluates to false is a failing test.
Running tests
When running a collection using the collection runner in the Postman app, you can view your tests running and the results in real time.
Testing automation
You can automate your tests by integrating Postman’s command line tool Newman with your favorite Continuous Integration or Continuous Delivery tool, like Jenkins or Travis CI.
You can also automate your tests by scheduling a collection run with a Postman monitor.
Conclusion
Writing automated test cases postman while testing can be expensive in the short-term, but they save you time in the long-term. They not only do more than a human can in a given amount of time, they also find defects quicker especially developing and testing Web services API’s and Connectors.
Happy Testing!!!!!
References
- Postman: Test cases Postman API Development.
- Postman API: Test cases Postman complete API development Environment.