8

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.

Brett K
  • 315
  • 1
  • 3
  • 6

2 Answers2

13
org.testng.Assert.fail("you wandered onto the wrong path");
  • this did not click on my mind – Tarun Oct 25 '11 at 05: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? – Helping Hands May 02 '16 at 08: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. –  May 02 '16 at 13:37
0

You can always throw exception -

if("this path is followed") {
throw new Exception("was bound to fail!!!")
}
Tarun
  • 3,437
  • 2
  • 30
  • 43
  • 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 05: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 06 '16 at 04:25