Is there a way to mark a test as failed in the TestNG framework if a certain path is followed?

I know I can use system.exit(1); or something similar to mark an abnormal termination but it doesn't seem to actually mark the test as failed.

2 Answers 2

org.testng.Assert.fail("you wandered onto the wrong path");
  • this did not click on my mind
    – Tarun
    Oct 25, 2011 at 5:10
  • Two roads diverge in a wood... and I, I Assert.failed...
    – corsiKa
    Aug 29, 2012 at 14:36
  • I used it, It help me to fail test case in result but also getting org.testng.Assert.fail(Assert.java:94) at org.testng.Assert.fail(Assert.java:101) at Testcases.SignUpTest.Company_Invalid_Check(SignUpTest.java:277) 34 lines not shown , Can you please tell me how can I avoid it? May 2, 2016 at 8:38
  • Helping Hands, please create a new question for that. In the question, include your stack trace and the corresponding code fragment. It may also help to summarize what you are trying to do.
    – user246
    May 2, 2016 at 13:37

You can always throw exception -

if("this path is followed") {
throw new Exception("was bound to fail!!!")
  • An exception does not seem to fail a test either. I might just put in some code which could never be executed. In my situation this is using some robot code inside of a webdriver test.
    – Brett K
    Oct 24, 2011 at 22:23
  • Are you handling exception in your test?
    – Tarun
    Oct 25, 2011 at 5:10
  • Throwing AssertionError instead of Exception (which is what Assert.fail() does under the covers) would be better and would prevent the error from being swallowed by a catch (Exception) block. Of course, catch (Exception) is generally a bad idea anyways...
    – dimo414
    Apr 6, 2016 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.