赞
踩
package ListenerPackage; import java.util.ArrayList; import java.util.List; import org.testng.ITestResult; import org.testng.TestListenerAdapter; import Assert.Assertion; public class AssertListener extends TestListenerAdapter { @Override public void onTestStart(ITestResult result) { Assertion.flag = true; Assertion.errors.clear(); } @Override public void onTestFailure(ITestResult tr) { this.handleAssertion(tr); } @Override public void onTestSkipped(ITestResult tr) { this.handleAssertion(tr); } @Override public void onTestSuccess(ITestResult tr) { this.handleAssertion(tr); } private int index = 0; private void handleAssertion(ITestResult tr){ if(!Assertion.flag){ Throwable throwable = tr.getThrowable(); if(throwable==null){ throwable = new Throwable(); } StackTraceElement[] traces = throwable.getStackTrace(); StackTraceElement[] alltrace = 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; alltrace = this.merge(alltrace, message); alltrace = this.merge(alltrace, et); } if(traces!=null){ traces = this.getKeyStackTrace(tr, traces); alltrace = this.merge(alltrace, traces); } throwable.setStackTrace(alltrace); tr.setThrowable(throwable); Assertion.flag = true; Assertion.errors.clear(); tr.setStatus(ITestResult.FAILURE); } } private StackTraceElement[] getKeyStackTrace(ITestResult tr, StackTraceElement[] stackTraceElements){ List<StackTraceElement> ets = new ArrayList<StackTraceElement>(); 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.length; i++) { et[i] = ets.get(i); } return et; } private StackTraceElement[] merge(StackTraceElement[] traces1, StackTraceElement[] traces2){ StackTraceElement[] ste = new StackTraceElement[traces1.length+traces2.length]; for (int i = 0; i < traces1.length; i++) { ste[i] = traces1[i]; } for (int i = 0; i < traces2.length; i++) { ste[traces1.length+i] = traces2[i]; } return ste; } }
package Assert; import java.util.ArrayList; import java.util.List; import org.testng.Assert; public class Assertion { public static boolean flag = true; public static List<Error> errors = new ArrayList<Error>(); 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; } } public static void verifyTrue(Boolean actual ){ try{ Assert.assertTrue(actual); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyTrue(Boolean actual,String message ){ try{ Assert.assertTrue(actual,message); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyFalse(Boolean actual ){ try{ Assert.assertFalse(actual); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyFalse(Boolean actual ,String message ){ try{ Assert.assertFalse(actual,message ); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyNotNull(Object actual ){ try{ Assert.assertNotNull(actual); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyNotNull(Object actual ,String message){ try{ Assert.assertNotNull(actual,message); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyNull(Object actual ){ try{ Assert.assertNull(actual); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyNull(Object actual,String message ){ try{ Assert.assertNull(actual,message); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(String actual,String expected ){ try{ Assert.assertEquals(actual,expected); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(String actual,String expected,String message ){ try{ Assert.assertEquals(actual,expected,message); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(int actual,int expected ){ try{ Assert.assertEquals(actual,expected); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(int actual,int expected,String message ){ try{ Assert.assertEquals(actual,expected,message); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(long actual,long expected ){ try{ Assert.assertEquals(actual,expected); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(long actual,long expected,String message ){ try{ Assert.assertEquals(actual,expected,message); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(boolean actual,boolean expected ){ try{ Assert.assertEquals(actual,expected); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(boolean actual,boolean expected ,String message){ try{ Assert.assertEquals(actual,expected,message); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(short actual,short expected ){ try{ Assert.assertEquals(actual,expected); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(short actual,short expected,String message ){ try{ Assert.assertEquals(actual,expected,message); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(byte actual,byte expected ){ try{ Assert.assertEquals(actual,expected); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(byte actual,byte expected ,String message){ try{ Assert.assertEquals(actual,expected,message); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(char actual,char expected ){ try{ Assert.assertEquals(actual,expected); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyEquals(char actual,char expected,String message ){ try{ Assert.assertEquals(actual,expected,message); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyNotEquals(String actual,String expected ){ try{ Assert.assertNotEquals(actual,expected); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyNotEquals(String actual,String expected,String message ){ try{ Assert.assertNotEquals(actual,expected,message); }catch(Error e){ errors.add(e); flag = false; } } public static void verifyfail(String message ){ try{ Assert.fail(message); }catch(Error e){ errors.add(e); flag = false; } } }
package Case; import org.testng.annotations.Listeners; import org.testng.annotations.Test; import org.testng.AssertJUnit; import Assert.Assertion; @Listeners(ListenerPackage.AssertListener.class) public class TestCase { @Test public void Case1(){ AssertJUnit.assertEquals(1, 1); System.out.println("=========Case1======="); } @Test public void Case2(){ Assertion.verifyEquals(1, 2); System.out.println("=========Case2======="); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。