JAVA结合testng断言verify(断言失败不中断继续执行)



原理:

1.自已构造一个断言类,把Assert.assertEquals给try catch住。

2.利用testng的监听类在测试方法运行结束后进行分析。

代码:

断言类:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.demo;   import java.util.ArrayList; import java.util.List;   import org.testng.Assert;   public class Assertion {            public static boolean flag = true ;            public static ListError errors = new ArrayListError();            public static void verifyEquals(Object actual, Object expected){          try {              Assert.assertEquals(actual, expected);          } catch (Error e){              errors.add(e);              flag = false ;          }      }            public static void verifyEquals(Object actual, Object expected, String message){          try {              Assert.assertEquals(actual, expected, message);          } catch (Error e){              errors.add(e);              flag = false ;          }      }   }

 监听类:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 package com.demo;   import java.util.ArrayList; import java.util.List;   import org.testng.ITestResu< ; import org.testng.TestListenerAdapter;   public class AssertionListener extends TestListenerAdapter {        @Override      public void onTestStart(ITestResu< resu< ) {          Assertion.flag = true ;                 Assertion.errors.clear();      }            @Override      public void onTestFailure(ITestResu< tr) {          this .handleAssertion(tr);      }            @Override      public void onTestSkipped(ITestResu< tr) {          this .handleAssertion(tr);      }            @Override      public void onTestSuccess(ITestResu< tr) {          this .handleAssertion(tr);      }            private int index = 0 ;            private void handleAssertion(ITestResu< tr){          if (!Assertion.flag){              Throwable throwable = tr.getThrowable();                         if (throwable== null ){                  throwable = new Throwable();              }                         StackTraceElement[] traces = throwable.getStackTrace();              StackTraceElement[] al< race = new StackTraceElement[ 0 ];                         for (Error e : Assertion.errors) {                  StackTraceElement[] errorTraces = e.getStackTrace();                  StackTraceElement[] et = this .getKeyStackTrace(tr, errorTraces);                  StackTraceElement[] message = new StackTraceElement[]{ new StackTraceElement( "message : " +e.getMessage()+ " in method : " , tr.getMethod().getMethodName(), tr.getTestClass().getRealClass().getSimpleName(), index)};                  index = 0 ;                  al< race = this .merge(al< race, message);                  al< race = this .merge(al< race, et);              }              if (traces!= null ){                  traces = this .getKeyStackTrace(tr, traces);                  al< race = this .merge(al< race, traces);              }                         throwable.setStackTrace(al< race);              tr.setThrowable(throwable);              Assertion.flag = true ;                 Assertion.errors.clear();              tr.setStatus(ITestResu< .FAILURE);                     }      }            private StackTraceElement[] getKeyStackTrace(ITestResu< tr, StackTraceElement[] stackTraceElements){          ListStackTraceElement ets = new ArrayListStackTraceElement();          for (StackTraceElement stackTraceElement : stackTraceElements) {                         if (stackTraceElement.getClassName().equals(tr.getTestClass().getName())){                                 ets.add(stackTraceElement);                  index = stackTraceElement.getLineNumber();              }          }          StackTraceElement[] et = new StackTraceElement[ets.size()];          for ( int i = 0 ; i et.len >h; i++) {              et[i] = ets.get(i);          }          return et;      }            private StackTraceElement[] merge(StackTraceElement[] traces1, StackTraceElement[] traces2){          StackTraceElement[] ste = new StackTraceElement[traces1.len >h+traces2.len >h];          for ( int i = 0 ; i traces1.len >h; i++) {              ste[i] = traces1[i];          }          for ( int i = 0 ; i traces2.len >h; i++) {              ste[traces1.len >h+i] = traces2[i];          }          return ste;      } }

 

?
1 br

 测试类:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.demo;   import org.testng.annotations.Listeners; import org.testng.annotations.Test;   @Listeners ({com.demo.AssertionListener. class }) public class Test14 {              @Test      public void testAssert3(){           Assertion.verifyEquals( 2 , 3 , "比较两个数是否相等:" );          Assertion.verifyEquals( 1 , 2 , "比较两个数是否相等:" );          }            @Test      public void testAssert4(){           Assertion.verifyEquals( 4 , 3 , "比较两个数是否相等:" );          Assertion.verifyEquals( 2 , 2 , "比较两个数是否相等:" );          }   }
最新回复(0)
/jishugmnOTJRfvx0T8_2F9pSP02Eld5tTTMkH4jO1j1zc8kmQI_3D4858691
8 简首页