How can I fail a test in TestNG in an AfterMethod?

380 views
Skip to first unread message

Srikanth Pulletikurthi

unread,
Aug 28, 2015, 2:22:42 AM8/28/15
to testng-users
Hi,

I want to check some external log files after each test, if there were ary errors during execution.
I want my test case to be failed if there are any core dumps found.

As you can see from the attachment, there is a coredump found during tearDown() but the actual
test case is still passed.

Setting the current test result to failure in onConfigurationFailure() using below two lines of code didn't work. Still only the
configuration method (tearDown()) is failing but not the actual test.

result.setStatus(ITestResult.FAILURE);
Reporter.setCurrentTestResult(result);

How can I fail the actual test along the configuration method ?


TC.JPG

Kaleem Uddin Mohammed Abdul

unread,
Aug 28, 2015, 4:37:19 AM8/28/15
to testng-users
 @AfterMethod(alwaysRun=true)
       public void afterMethod() throws Exception {
          System.out.println("in afterMethod");
          throw new Exception("Hello!!");
       }
It worked for me. in Hello string you can specify the error message which is their in your log.

The skip list is because my test depends on test case 2.

The below is the output
[TestNG] Running:
  C:\Kaleem\EclipseWorkSpace\TestNGTesting\src\testng\com\testng.xml

in beforeSuite
in beforeTest
in beforeClass
in beforeGroup
in beforeMethod
in test case 2
in afterMethod
in beforeMethod
in afterMethod
in afterClass
in afterTest
in afterSuite

===============================================
Suite
Total tests run: 3, Failures: 0, Skips: 2
Configuration Failures: 2, Skips: 0
===============================================
Reply all
Reply to author
Forward
0 new messages