7

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.

11
org.testng.Assert.fail("you wandered onto the wrong path");
4
  • this did not click on my mind
    – Tarun
    Oct 25 '11 at 5:10
  • Two roads diverge in a wood... and I, I Assert.failed...
    – corsiKa
    Aug 29 '12 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 '16 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 '16 at 13:37
0

You can always throw exception -

if("this path is followed") {
throw new Exception("was bound to fail!!!")
}
3
  • 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 '11 at 22:23
  • Are you handling exception in your test?
    – Tarun
    Oct 25 '11 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 '16 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.