Sunday, November 23, 2014

Error Collector in TestNg


  • Assert will stop its execution on test failure but it is must instead for better automation we have to use it as maximum as possible. 

                          If needs to pass the test 

                          try{ 
                                  Assert.assertequals(true,false); 
                              }
                           catch(Throwable ex){ 
                                   System.out.println("The case will pass"); 
                              } 

              but here u wont get the case marked failed



  • Use try catch, when test case fails, inside catch write "throw new RuntimeException();" so that it will capture in the TestNG report as fail
  • TestNG has support for custom listeners, which can run when tests pass/fail/skip, as well as before and after invocation. By adding a custom listener to check for verification failures after invocation, we can get the details of all verification failures that have occurred, and report them at the same time as we report our hard failure, or if there are no hard failures we can change the result to a failure and report the verification failures.

          This solution uses part of the TestNG soft failures patch by Dan Fabulich in order to combine the stack                 traces of multiple failures. Details of the patch are available here.

  • Class ErrorCollector :-The ErrorCollector rule allows execution of a test to continue after the first problem is found (for example, to collect _all_ the incorrect rows in a table, and report them all at once):

package myWorkjUnit; 
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
 
public class UsesErrorCollectorTwice {
 /*
 * The ErrorCollector Rule allows execution of a test to continue after the
 * first problem is found and report them all at once
 */
 @Rule
 public ErrorCollector collector= new ErrorCollector();
 
@Test
 public void example() {
 collector.addError(new Throwable("first thing went wrong"));
 collector.addError(new Throwable("second thing went wrong"));
 
 // all lines will run, and then a combined failure logged at the end.
 }
 
}


No comments:

Post a Comment