Logging variables differences

I have a test that is using isEqual to check if two variables that are strings (larger csv converted to string with JSON.stringify) are equal to each other. This works fine. However, if the test fails I’d like to log the differences between the two variables.

Here is my test data:

pm.test("Baseline and Updated CSVs Match!", () => {
    const firstResponse = (pm.environment.get('balance_by_event_original'));
    const secondResponse = (pm.environment.get('balance_by_event_new'));
    const isEqual = _.isEqual(firstResponse, secondResponse);


Can you provide an example for one of those strings?

What do you mean by difference? and log where?

Do you just want to show both strings in the failing test, or an actual DIFF between two objects.

Not sure why you need isEqual in this scenario when you can use an “to.eql” assertion instead.

This normally tells you the difference between the two in the failing test.

let foo = "oranges";
let bar = "apples";

pm.test("variables match!", () => {


EDIT: See update below for solution and question

Hi Mike,

So I want to find out what the difference is when two variables don’t match, kind of like a diff checker.

Here is one of the variables:

B[“Report: Balance by Event,”,“Group: UX3 Dylan,”,“Event: Dup Test,”,“Dates: 11/2/2021 to 11/2/2025,”,“,”,“Event ID,Event Name,Event Date,USD Sales,USD Discount,USD Refund,USD Canceled,USD Forfeited,USD Adjusted Sales,USD Processing Fee,USD Processing Fee to Client,USD Taxes,USD Delivery Client Fee,USD Delivery Fee,USD Fee,USD Payment Plan Fee,USD Payment Plan Client Fee,USD Net Sales,USD Disputed,USD Dispute Fee,USD Collected by ,USD Recoupment Amount,USD Stripe Processing Fee,USD Balance”,“"6,904",Dup Test,2/24/2024,238.80,0.00,-33.80,0.00,0.00,205.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,205.00,0.00,0.00,205.00,-114.40,0.00,90.60”,“Total,238.80,0.00,-33.80,0.00,0.00,205.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,205.00,0.00,0.00,205.00,-114.40,0.00,90.60”,“,”,null,“v4.0,”,“”]

And if the second variable I am using, if there’s a number that is different than what is in the above, I want the console (or the test results, whatever is easiest) to tell me what the discrepency is

Wow, okay so incredibly ChatGPT was able to do this for me ha! However, I’m not clear as to exactly how its doing it, so maybe you could help me understand?

Since this is a string and not an array, I had it treat it as such.

if (firstResponse === secondResponse) {
    console.log("Variables are identical");
} else {
    var positionThreshold = 10; // NNumber of characters before and affer diff
    // Find the position where the strings differ
    for (var i = 0; i < Math.min(firstResponse.length, secondResponse.length); i++) {
        if (firstResponse[i] !== secondResponse[i]) {
            var start = Math.max(0, i - positionThreshold);
            var end = Math.min(firstResponse.length, i + positionThreshold + 1);
            console.log("Difference found at position " + i + " in line 1: " + firstResponse.substring(start, end));
            console.log("Difference found at position " + i + " in line 2: " + secondResponse.substring(start, end));

I don’t understand what the “i” and “Math.min/max” is doing.

i is just the iteration count, its the internal counter used in the loop.

This is looping through the string one character at a time, and its also used to tell you where in the string the differences are.

Math min returns the smallest number from whatever you throw at the function.

This is used to control the loop, because you can’t compare if the string is a different size, its its going to use the smallest string to control the loop.

This will probably work ok for you, as you appear to be comparining a line in a CSV file. But probably wouldn’t be very good for comparing a JSON object.

Doing a DIFF properly can be complicated, and most of the libraries out there are based on node.JS. I could never find a library in pure JavaScript (that you could EVAL in Postman) that catered for all of the possibilities when running a DIFF between two objects.

On a side note, although Postman can be used for automating certain tasks. It’s not really built for working with CSV files. Personally, I think you should be looking at PowerBI if you want to compare CSV files and data sets.

thanks so much. i will look into PowerBI!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.