Open In App

How to add Postman test asset error or success message

Last Updated : 05 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Postman is a popular API testing tool that allows developers and testers to send requests to APIs and receive responses. One of the key features of Postman is its ability to perform tests on these responses, ensuring that they meet the expected criteria. This article will guide you through the process of adding custom error and success messages to your Postman tests.

What is Postman?

Postman is a widely used API development and testing tool that provides a user-friendly interface for developers and testers to interact with APIs. It allows users to send HTTP requests to a server, receive responses, and inspect the results.

Key features of Postman include:

  • Request Building: Postman allows users to easily construct HTTP requests, including methods (GET, POST, PUT, DELETE, etc.), headers, URL parameters, request bodies, and more.
  • Response Inspection: Users can view the details of the responses received from API requests, including status codes, headers, and response bodies.
  • Automation and Testing: Postman provides a testing environment where users can write automated tests to validate the responses from API calls. These tests can be written in JavaScript using the Chai assertion library.
  • Collections: Requests can be organized into collections, making it easy to group and execute a series of related API calls.
  • Environment Variables: Postman allows users to define environment variables, making it easy to switch between different environments (e.g., development, production) without modifying individual requests.
  • Mock Servers: Postman allows users to create mock servers, which simulate API responses. This is particularly useful for testing client applications before the actual API is fully developed.
  • Documentation Generation: Postman can automatically generate API documentation based on the requests and responses defined in a collection.
  • Collaboration and Sharing: Postman provides features for team collaboration, including the ability to share collections, workspaces, and environments with team members.
  • Monitoring and Debugging: Postman provides tools for monitoring API performance and debugging issues.

What are Test Assertions?

A test assertion, in the context of software testing, is a statement or condition that defines an expected behavior or outcome of a specific test case. It’s a way to validate whether the actual result of a test matches the expected result.

Assertions play a crucial role in the testing process because they provide clear pass/fail criteria for each test case. If an assertion evaluates to true, the test passes; if it evaluates to false, the test fails.

Key points about test assertions:

  • Purpose: Assertions are used to verify that a certain condition is true during the execution of a test. This condition could be related to the state of the application, the behaviour of a function, or the content of a response from an API, among other things.
  • Conditions: Assertions are typically written in the form of statements that express the expected outcome of a test based on specific conditions. For example, in a unit test for a function, an assertion might check if the function returns the correct value.
  • Failure Indication: If an assertion fails during the execution of a test, it indicates that there is a deviation from the expected behaviour. This is a signal that there might be a bug or an issue in the code being tested.
  • Assertion Libraries: In most programming languages and testing frameworks, there are libraries or built-in functions specifically designed for making assertions. These libraries provide a set of methods for common assertions (e.g., equality checks, and existence checks) and help streamline the testing process.
  • Feedback: Assertions provide feedback to developers and testers about the correctness of their code. They serve as a way to communicate whether the software is functioning as expected or if there are discrepancies that need to be addressed.

For example, in a JavaScript unit test, an assertion might look like this:

Javascript




// Check if a function returns the correct value
assert.equal(myFunction(2), 4, 'Expected the result to be 4');


In this example, the assertion checks if the result of myFunction(2) is equal to 4. If it is, the test passes. If not, it fails and the message “Expected the result to be 4” provides additional context about the expected outcome.

In summary, test assertions are fundamental testing elements that help ensure software’s correctness and reliability by comparing actual outcomes to expected outcomes. They play a crucial role in the process of building robust and bug-free software systems.

pm.test():

A Postman function used to define tests in the “Tests” tab. It takes a test name and a function containing the test logic.

Javascript




Syntax: pm.test(testName, testFunction);


What is Chai Assertion Library?

The Chai Assertion Library is a popular JavaScript library that provides a set of functions for making assertions in test cases. It is often used in conjunction with testing frameworks like Mocha, Jasmine, and Jest to write clear and expressive test cases in JavaScript applications.

Here are some key points about the Chai Assertion Library:

Chai is used to write assertions, which are statements that verify whether a certain condition is true during the execution of a test. These conditions could be related to the state of the application, the behaviour of a function, or the content of a response from an API, among other things.

Types of Assertions:

Chai supports three main styles of assertions:

  1. Should: This style uses should chains for making assertions.
  2. Expect: This style uses the expect function for making assertions.
  3. Assert: This style uses the assert function for making assertions.
  4. Readability and Expressiveness: Chai is designed to be highly readable and expressive. This makes it easy for developers and testers to understand the intent of the test cases.
  5. Flexibility: Chai provides a wide range of assertion functions to cover various scenarios, including equality checks, existence checks, type checks, and more. This flexibility allows developers to write detailed and specific tests.

Process of adding Postman test asset error or success message:

Step 1: Open Postman and Select a Request

Open the Postman application and select the request for which you want to add custom error and success messages.

Step 2: Navigate to the “Tests” Tab

Click on the “Tests” tab, located below the request details. This is where you’ll write the JavaScript code for your tests.

Step 3: Add a Success Message

To add a success message, you can use the pm.test() function along with Chai assertions. Here’s an example testing the status code:

Javascript




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


This will display a success message if the status code is 200.

Step 4: Add an Error Message

To add a custom error message, use pm.expect() in combination with an if statement. Here’s an example checking for an error field in the response JSON:

Javascript




pm.test("Custom Error Message", function() {
    let jsonData = pm.response.json();
    if (jsonData.error) {
        pm.expect(jsonData.error).to.equal(null, "Error message: " + jsonData.error.message);
    }
});} });


This will display a custom error message if an error is found in the response.

Conclusion:

With Postman’s testing capabilities, you can ensure that your APIs are functioning as expected. By adding custom error and success messages, you provide clear feedback on the results of your tests. This enhances the debugging process and improves the overall quality of your API development workflow. Remember to tailor your assertions to your specific API requirements for the most effective testing.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads