Code Snippets #4 The if() function

posted in: Software Testing | 0

The if function allows you to specify some arguments before performing specific statements dependant on the outcome of those arguments. 

For example, I want to log a Pass for a response code of 200 OK, but if it is 409 conflict, I want to log a Fail.

ìf(responseCode.code === 200)
{
console.log("Pass");
}
else
{
console.log("Fail");
}

The if function is very versatile in that you can have multiple if functions within an if function!

For example:

if(responseCode.code === 200 || responseCode.code === 409)
{
if(responseCode.code === 200)
{
console.log("Pass");
}
if(responseCode.code === 409)
{
console.log("Something is conflicting);
}
else
{
console.log("Fail");
}

N.B. The two tubes || represent OR. So you can also have multiple arguments within one if function.

As this is postman, we want to see some postman tests in there too. I have added the console.log() for debugging. You don’t have to have these when writing your postman tests. 

if(responseCode.code === 200 || responseCode.code === 409)
{
if(responseCode.code === 200)
{
tests["Response code is 200 OK"] = responseCode.code === 200;
console.log("Pass");
}
if(responseCode.code === 409)
{
tests["Response code is 409 Conflict"] = responseCode.code === 409;
console.log("Something is conflicting);
}
else
{
tests["Response code is not 200 or 409"] = responseCode.code === 200 || responseCode.code === 409;
console.log("Fail");
}

The if function is probably going to be one of your most used functions for testing, seeing as we are not always just looking for one outcome, but often, multiple outcomes. 

Back

Leave a Reply