Fail a test with an if case

Hello, I’m trying to figure out how to fail a test when something happens:

So a have a string/text that I’m searching through with a for loop, using value from an array:

for ( let i = 0; i < fullNames.length; i ++) {
    var n = listedNames.search(fullNames[i])
    if ( n === -1) {
         pm.test("This name is missing: " + fullNames[i] , function () {

n != -1; the n != -1 does not seem to return a failed test…
});
}
}
However, I want to make it fail when n is equal to -1 - when what was searched has not been found. This for loop usually runs 30-40 times, can I make it that it logs all the failed searches as failed tests? Or simply just fail it once when the n is -1, I can probably log the rest in the console for further investigation if it does fail.

1 Like

I can be wrong but you can insert an else!

if (something) {
 pm.test("Foo")
} else {
 pm.test("Bar")
}

:vulcan_salute:

 pm.test("This name is missing: " + fullNames[i] , function () {

n != -1; **the n != -1 does not seem to return a failed test…**
}

I’m trying to make it fail the test, with n != -1, in this case n should be -1, so I’m trying to get it to fail if n isnt -1

Hey @theToncheff :wave:

Could you share the full response body and the full code please, not just the tests, the tiny snippets of information are just going to lead to more questions :slight_smile:

What are you looking to test for? A missing name, an empty string, some other condition?

Is there a reason you chose the .search() method over something else?

I’m just trying to establish some context :slight_smile:

I chose search because I don’t know a better way right now, however that isn’t my issue.

I’m just trying to figure out, if it is possible to get a test to fail if it does not meet the expected value for N, the pm.test will only be logged when n is -1, so I know it has already failed, I’m just looking for a way to manually make it fail:

 pm.test("RandomText" , function() {
         if I understand correctly, if something isnt right here, it should fail.

    }

I’m not sure what -1 represents in your context, as I can’t see the code you’re running or the details about what is being looped through to get that value as the output. So I wouldn’t be able to suggest a valid solution that covers your use case, i would just be guessing. :slight_smile:

There is a .fail(<optional message string>) chai method that you could use to fail the test if the condition is met.

Basic example:

pm.test("Value equals -1", function () {
    let num = -1
    if(num === -1) {
        pm.expect.fail("Value equals -1")
    } 
});

It’s way to fail a test but again, I don’t 100% understand what it is you’re trying to do so this is just a suggestion :slight_smile:

1 Like

Hey, so N stands for the index of the array, so when it searches the string, it returns N with the index value of what was searched. This does not matter, as I’m only looking for missing names in this case.

N is -1 when the value from the array (fullnames[i]) has not been found, so I’m trying to make it that each time n returns as -1, to have a failed test, right now if I have missing names, I receive a passed test that prints the name, however I want that test to be a failed one.

I tried using .fail, however I may have not used it correctly as it breaks my collection when it fails the first time while I’m trying to have it to log each fail as failed test in the runner as many times as required.

Thank you though! I will try to see if I can get it to work with .fail

Sharing all your code would probably help get you to a solution quicker. :grin:

Trying to explain what it is, what’s it’s doing or even that it’s failing somewhere, is never going to be as good as actually seeing it and walking through it. :grin:

If you wanted to do that in a private message, feel free to ping me.

So, this is what I ended up with:

 if ( n === -1) {
        pm.test("This participant is missing: ",function() { throw new Error( fullNames[i]); });
    }

Seems to be doing the job

1 Like

@theToncheff you can also use:

pm.expect.fail('This failed because ...');

3 Likes