Skip to content

How To Use Math.Round With Postman

API Testing Blog

Using Math.round() in Postman for API Testing

Postman is a powerful tool for testing APIs, but sometimes you need to manipulate data before assertions. A common scenario is rounding numbers to the nearest integer. This is where the Math.round() JavaScript function comes in handy.

Understanding Math.round()

Math.round() is a built-in JavaScript function that rounds a number to the nearest integer. Here’s how it works:

  • If the decimal part of the number is 0.5 or greater, it rounds up to the next integer.
  • If the decimal part is less than 0.5, it rounds down to the previous integer.

Using Math.round() in Postman Requests

Postman allows you to write JavaScript code in its scripting environment. This enables you to use Math.round() directly within your test scripts.

Step 1: Set up your Request

  • Create a new Postman request or use an existing one that returns numeric data.
  • Send the request and capture the response data.

Step 2: Access the Response Data

  • Using pm.response.json(): If the response is in JSON format, use pm.response.json() to access the data as a JavaScript object.
const response = pm.response.json();
  • Using pm.response.text(): If the response is text, use pm.response.text() to retrieve the data as a string.
const responseText = pm.response.text();

Step 3: Use Math.round() on the Data

  • For JSON data: Extract the specific numerical value you want to round using dot notation. Then apply Math.round() to the value.
const roundedValue = Math.round(response.price);
  • For text data: You’ll need to parse the numerical value from the text string before rounding. Use parseFloat() to convert the string to a number.
const responseText = pm.response.text();
const priceFromString = parseFloat(responseText.split(",")[1]); // Example: Assuming price is after comma
const roundedValue = Math.round(priceFromString);

Step 4: Use the Rounded Value in Tests

Use the roundedValue variable in your assertions or further calculations within your Postman test scripts.

pm.test("Round price correctly", () => {
pm.expect(roundedValue).to.be.equal(10); // Example assertion
});

Example: Rounding a Price Value

Let’s say you’re testing an API that returns product information. You want to verify that the price is rounded correctly.

Request:

GET /products/1

Response:

{
"id": 1,
"name": "Product A",
"price": 9.99
}

Postman Test Script:

const response = pm.response.json();
const roundedPrice = Math.round(response.price);
pm.test("Price rounded correctly", () => {
pm.expect(roundedPrice).to.be.equal(10);
});

Variations of Math.round()

JavaScript offers additional rounding functions:

  • Math.ceil(): Rounds a number up to the nearest integer.
  • Math.floor(): Rounds a number down to the nearest integer.

You can use these functions in Postman test scripts in the same way as Math.round() to achieve different rounding behavior. For instance, you might use Math.ceil() to calculate the minimum shipping cost based on a price.

Conclusion

Math.round() is a powerful tool for rounding numeric values within Postman test scripts. It allows you to perform assertions on rounded data, enhance your API testing workflow, and ensure the accuracy of your validations. Remember to choose the appropriate rounding function based on your testing requirements.

API Testing Blog