Testing the API using Postman

Postman also provides us with a helpful tool/framework that we can use to test our APIs automatically. The test runtime is based on Node.js; for that reason, we should write a test assertion using JavaScript. The test framework of Postman uses the following workflow to test our code:

The scripts will execute as follows for every request in a collection:

  • A pre-request script will run before every request in the collection. This is considered the setup part of a test case.
  • A test script will run after every request in the collection; this is the core part of our test. Furthermore, it provides the core assertions for our test.

Let's use this procedure to implement a simple test for our API:

pm.test("response is ok", function () {
pm.response.to.have.status(200);
});

As you can see, the test uses the pm global object to describe a test, and it adds a callback function to make some assertions. In this case, it checks whether the API has a status of 200 OK. The following implementation can be added to a request by typing the preceding code into the Tests tab of the request, as shown in the following screenshot:

After that, we will be able to check the result in the response part of Postman:

The test results section that's shown in the preceding screenshot contains four different subtabs, all of which group the tests by the results. If an error occurs, the UI provides details of the error.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.249.105