How to mark a testng test fail at a specific step without throwing an error

390 views
Skip to first unread message

mycheap...@gmail.com

unread,
Dec 21, 2021, 5:39:48 PM12/21/21
to Selenium Users
Hello,

@TestNG
public void test(){

   // other code
    if(true){
       System.out.println("pass");
    }
    else{ 
        System.out.println("fail");
    }

}

I did not use assert in my test. Right now if false, I am just printing out "Fail". In this case, testng still mark it as passed. 

Instead, what I would like to do is, when it reaches this line

        System.out.println("fail");

I like to mark the testNG test as failed without throwing any exception because I like my tests to continue running. 

I used this  org.testng.Assert.fail("you wandered onto the wrong path"); as following:

@TestNG
public void test(){

   // other code

    if(true){
       System.out.println("pass");
    }
    else{ 
        System.out.println("fail");
org.testng.Assert.fail("you wandered onto the wrong path");
    }

// code

}

Problem is that it throws an exception and my test does not continue with the later steps. How can I mark the test failed without throwing an error?


⇜Krishnan Mahadevan⇝

unread,
Dec 22, 2021, 2:09:17 AM12/22/21
to Selenium Users
You are contradicting yourself.

If a test fails, it aborts right at that point. If you would like the test to continue on failure but eventually have the test failed, then you should be using SoftAsserts. There are a lot of tutorials on the net that will explain this for you.


PS: This is really a TestNG question and not related to Selenium. Please help keep this forum relevant by posting questions that are ONLY related to Selenium. 

For TestNG queries, you can always post them on the TestNG google forums https://groups.google.com/g/testng-users



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/c1a9667d-97d8-4329-86ba-7e2374c7876en%40googlegroups.com.

Adrian

unread,
Dec 22, 2021, 3:38:52 PM12/22/21
to Selenium Users
Hi,
What you would achieve by allowing the test to continue?  Just more failure at every other step.
I cannot see the value in continuing a test that has failed.

I can guess that part of the test or a previous test has created something that causes your test to fail if it exists and you wan to ignore this and continue.
This was an issue I faced where I uploaded a file, but the next time the test run the file already existed so my test failed.
For this situation I improved my tests to upload the same file, but rename it to a unique name before uploading.

So this is what you should be doing, writing tests in a way so that can be run twice in a row without issue.


Cheers,
Adrian.

Reply all
Reply to author
Forward
0 new messages