Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 5 down vote accepted"you wandered onto the wrong path");
share|improve this answer
this did not click on my mind – Tarun Oct 25 '11 at 5:10
This is exactly what I was looking for. Thank you – Brett K Oct 26 '11 at 15:48
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 at at Testcases.SignUpTest.Company_Invalid_Check( 34 lines not shown , Can you please tell me how can I avoid it? – Helping Hands May 2 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 at 13:37

You can always throw exception -

if("this path is followed") {
throw new Exception("was bound to fail!!!")
share|improve this answer
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 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 at 4:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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