it will pass here in eclipse. what ErrorUtil java file does is it changes test in reports.
As per Mr.Ashish's video the ErrorUtil has to fail the test case in eclipse as well. I have attached a screenshot from Mr.Ashish's video where the same scenario which i have explained in my previous message fails in eclipse. My report also shows that test case is passed.
Thanks
Archana
Yes, I do have a testng_Listeners.xml(its just naming different) in my project with the listener tag pointing to CustomListeners java file. Attached the screenshot for your reference.
Thank you!
I am also facing the same issue. I am running from testng.xml and result passes in eclipse.
Test Case code :
public class ListenerOverirdeMethodsUse1 {
@Test
public void testA(){
System.out.println("testA starts");
System.out.println("testA ends");
}
@Test
public void testB(){
System.out.println("testB starts");
try{
Assert.fail("TestB failed");
}catch(Throwable t){
System.out.println("Error");
ErrorUtil.addVerificationFailure(t);
}
System.out.println("testB ends");
}
@Test//(enabled=false)
public void testC(){
System.out.println("testC starts");
throw new SkipException("TestC Skipped");
}
}
Results are:
[TestNG] Running:
D:\Programming\JavaLearning\SeleniumLearningQTPSelenium\testng.xml
testA starts
testA ends
Passed TC Name is - testA
testB starts
Error
testB ends
Passed TC Name is - testB
testC starts
Skipped TC Name is - testC
===============================================
LearningTestNG
Total tests run: 3, Failures: 0, Skips: 1
===============================================
Listener Class code is as follows:
public class CustomListener extends TestListenerAdapter implements IInvokedMethodListener{
void onTestFailed(ITestResult tr){
System.out.println("Failed TC Name is - "+tr.getName());
}
public void onTestSkipped(ITestResult tr){
System.out.println("Skipped TC Name is - "+tr.getName());
}
public void onTestSuccess(ITestResult tr){
System.out.println("Passed TC Name is - "+tr.getName());
}
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
// TODO Auto-generated method stub
}
public void afterInvocation(IInvokedMethod method, ITestResult result) {
Reporter.setCurrentTestResult(result);
if (method.isTestMethod()) {
List<Throwable> verificationFailures = ErrorUtil.getVerificationFailures();
//if there are verification failures...
if (verificationFailures.size() != 0) {
//set the test to failed
result.setStatus(ITestResult.FAILURE);
//if there is an assertion failure add it to verificationFailures
if (result.getThrowable() != null) {
verificationFailures.add(result.getThrowable());
}
int size = verificationFailures.size();
//if there's only one failure just set that
if (size == 1) {
result.setThrowable(verificationFailures.get(0));
} else {
//create a failure message with all failures and stack traces (except last failure)
StringBuffer failureMessage = new StringBuffer("Multiple failures (").append(size).append("):nn");
for (int i = 0; i < size-1; i++) {
failureMessage.append("Failure ").append(i+1).append(" of ").append(size).append(":n");
Throwable t = verificationFailures.get(i);
String fullStackTrace = Utils.stackTrace(t, false)[1];
failureMessage.append(fullStackTrace).append("nn");
}
//final failure
Throwable last = verificationFailures.get(size-1);
failureMessage.append("Failure ").append(size).append(" of ").append(size).append(":n");
failureMessage.append(last.toString());
//set merged throwable
Throwable merged = new Throwable(failureMessage.toString());
merged.setStackTrace(last.getStackTrace());
result.setThrowable(merged);
}
}
}
}
}
Hello Admin,
After posting my reply here, i took seriously over your suggestion "check id error utils file is called properly." and found that i have one more ErrorUtil file in different package, which i deleted and than came to know that i was imorting and pointing to wrong ErrorUtil class file, which i deleted.
Pointing to correct file did the trick, the Issue is solved now. Thanks and sorry for spamming on this post.
But I still need to understand the logic/implementation of ErrorUtil's methods and afterInvocation method
Regards,
Sanjay
you want to learn how is ErrorUtil works? if, so you can read the code or debug the code go to errorutil line by line but I would suggest start using extent reports they're much better no need for errorutil file.