Test ScriptsTest scripts execute after the server receives the response. In postman, we can write test scripts to test API requests in Javascript. Test script is used to test whether your API is working accordingly or not, to establish that integrations between the services are functioning properly, and to check that new developments have not affected any functionality of existing requests. Test Scripts are also called Post-Request script. We can use the test code to debug the process of your API project. Such as, you may use the test script to verify error handling for your API by submitting an incomplete data request. We can run the test scripts to a single request, folders, or collections. Let's see a simple example to add the test script to a request:
Clear the old logs from the console.
Here, you can see that the test script is running after the request execution. Creating Variables using Tests ScriptHere, we will use the environment variable.
This will create a variable inside the "Development" environment with the name "u" and value "Hello".
Here, we can see the created "u" variable is available in the environment. Writing Test ScriptsWe can enter the script manually or use the Snippets, which is available on the right side of the code editor. We can also write our custom test script in Postman. Postman offers a 'pm' API (called as pm.* API), which is used to write the tests. pm.test()This function writes the conditions of the test in the postman test sandbox. Writing tests within this feature helps you to correctly name the test and ensure that the rest of the script is not interrupted in case of any errors. This function has two arguments, first is the test name (as a string), and second is a function which returns a Boolean value. Let's see an example:
Click on the Send button to test your request and select Test Results in the response section. This will show how many tests passed and how many ran in total. In the above example, if the request returned a 200 status code, the test will pass; otherwise, it will fail. Try to change the status code of your tests and run the same request again. You will get the following response: pm.except()The pm.except() function is used to print the test result messages in a different format. This function makes the test readable, and even we can manage data assertions from a variable or response. Click on the send button and see the Test Results from the response section: Through this function we can test the request environment, as given below: Another example is: pm.response.to.be.*This is an object, offers shorthand for the tests based on commonly used response. Test ResultsTo test written tests are passing or failing; we use Test Results. Once you run a request with tests, then select the Test Results tab from the response window. Here, you will get a list of your test results, whether the test has passed or failed. A test with Boolean value true means a passing test, and false means test is failing. Next TopicTest Examples |