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


    原理:

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

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

    代码:

    断言类:

    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 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;
    		}
    	}
    
    }
    

     监听类:

    package com.demo;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.testng.ITestResult;
    import org.testng.TestListenerAdapter;
    
    public class AssertionListener 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 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, "比较两个数是否相等:");		
    	}
    
    }
    
  • 相关阅读:
    入门菜鸟
    FZU 1202
    XMU 1246
    Codeforces 294E Shaass the Great 树形dp
    Codeforces 773D Perishable Roads 最短路 (看题解)
    Codeforces 814E An unavoidable detour for home dp
    Codeforces 567E President and Roads 最短路 + tarjan求桥
    Codeforces 567F Mausoleum dp
    Codeforces 908G New Year and Original Order 数位dp
    Codeforces 813D Two Melodies dp
  • 原文地址:https://www.cnblogs.com/zhangfei/p/4450259.html
Copyright © 2020-2023  润新知