Hi Team,
I am trying to verify null value in test script as compare to the .csv file.
Step to reproduce:
- Create a .csv file with header and value e.g. Header-orderType and value-null
- Create a test script
pm.test(ânull valueverificationâ, function () {
var jsonData = pm.response.json();
pm.expect(jsonData[0].nextScheduledPremierOrder.orderType).to.eql(String(data.orderType ));
});
- Hit the url and get the response and verify the response.
[{
âidâ: â2002â,
ânextScheduledPremierOrderâ: {
âqvâ: â286.38000000â,
âorderTypeâ: null
}}]
4.Getting the error as AssertionError: expected null to deeply equal ânullâ
Note:Tried with chai assertion but no luck, Any solution?
Before we head down a similar road as we did a few days ago.
Have you opened up that csv file in a text editor to check the formatting? Atom, notepad++, sublime, VSCode, etc are all good options.
My fear is that using excel to create these csv files is adding these invisible characters that are not showing in the views that you have.
Yes, I already checked the formatting via notepad++ and Atom.No invisible character hidden there.
The value in your csv is a string whose contents is the word ânullâ.
You can do a little bit of preprocessing before your assertion:
pm.test(ânull valueverificationâ, function () {
let jsonData = pm.response.json();
let orderType = data.orderType;
if(orderType === "null"){
orderType = null;
}
pm.expect(jsonData[0].nextScheduledPremierOrder.orderType).to.eql(String(data.orderType ));
});
Another option could be to just try not casting data.orderType as a string in the first place.
No luck, with the both way tried and not working.
cc @allenheltondev
it looks like it is still comparing null as a string, I see there was a mistake in my script
pm.test(ânull valueverificationâ, function () {
let jsonData = pm.response.json();
let orderType = data.orderType;
if(orderType === "null"){
orderType = null;
}
pm.expect(jsonData[0].nextScheduledPremierOrder.orderType).to.eql(orderType);
});
Thanks!! Even I havenât noticed the repetition of data.orderType when already assigned to the orderType variable. It"s working fine but looks like not feasible in long term solution.
Scenario:- We are following data driven approach(.csv file) where as all values maintained in .csv file with respect to the objects/header.
Basically values change based on the run time database response,Each time any of the objects can have null values so practically not possible to change script based on the runtime null values.
Looking forward to hear any suggestions.
Solution:
A. I tried to put ânullâ in .csv file but it"s not working.
B. Yes, I can make above script for all of the objects but again it will be very long script and not in optimize way.
You could make a function that does this check/coalesce so you donât have to keep writing it over and over. I just donât see another way to do this if all your values are nullable.
pm.test(ânull valueverificationâ, function () {
let jsonData = pm.response.json();
pm.expect(jsonData[0].nextScheduledPremierOrder.orderType).to.eql(replaceNull(data.orderType));
pm.expect(jsonData[0].nextScId).to.eql(replaceNull(data.nextScId));
});
function replaceNull(value) {
if(value === "null") {
value = null;
}
return value;
}