一.概述
測試用的執(zhí)行通過與否,需要對比期望結果和實際結果的值,因此在自動化測試中我們需要進行斷言來獲得執(zhí)行的結果,那么如果在執(zhí)行的過程中有異常,該怎么辦呢,我們是否要查看異常信息呢,于是乎如何得到我們所需要的異常信息成為我們需要思考的問題,該怎么解決呢,其實so eazy!tesgNG的測試用例監(jiān)聽器可以幫助我們解決這個問題。
二.監(jiān)聽斷言類AssertionListener
1 public class AsserttionListener extends TestListenerAdapter{
2
3 private Log log=new Log(this.getClass());
4
5 @Override
6 public void onTestStart(ITestResult result) {
7 // TODO Auto-generated method stub
8 Assertion.begin();
9 }
10
11 @Override
12 public void onTestFailure(ITestResult tr) {
13 // TODO Auto-generated method stub
14 this.handleAssertion(tr);
15 }
16
17 @Override
18 public void onTestSkipped(ITestResult tr) {
19 // TODO Auto-generated method stub
20 this.handleAssertion(tr);
21 }
22
23 @Override
24 public void onTestSuccess(ITestResult tr) {
25 // TODO Auto-generated method stub
26 this.handleAssertion(tr);
27 }
28
29 private int index;
30
31 private boolean isNew=false;
32
33 /**
34 * 得到測試類所需的測試異常信息
35 * @param tr
36 */
37 private void handleAssertion(ITestResult tr){
38 Throwable throwable=tr.getThrowable();
39 if(!Assertion.flag||throwable!=null){
40 if(throwable==null){
41 throwable=new Throwable();
42 isNew=true;
43 }
44 StackTraceElement[] alltrace=new StackTraceElement[0];
45 for (Error e : Assertion.errors) {
46 alltrace=this.getAllStackTraceElement(tr, e, null, alltrace);
47 }
48 if(!isNew){
49 alltrace=this.getAllStackTraceElement(tr, null, throwable, alltrace);
50 }else{
51 isNew=false;
52 }
53 throwable.setStackTrace(alltrace);
54 tr.setThrowable(throwable);
55 Assertion.flag=true;
56 Assertion.errors.clear();
57 tr.setStatus(ITestResult.FAILURE);
58 }
59 }
60
61 /**
62 * 得到測試類需要的StackTraceElement數(shù)組
63 * @param tr
64 * @param e
65 * @param throwable
66 * @param alltrace
67 * @return
68 */
69 private StackTraceElement[] getAllStackTraceElement(ITestResult tr,Error e,Throwable throwable,StackTraceElement[] alltrace){
70 StackTraceElement[] traces=(e==null?throwable.getStackTrace():e.getStackTrace());
71 StackTraceElement[] et=this.getKeyStackTrace(tr, traces);
72 String msg=(e==null?throwable.getMessage():e.getMessage());
73 StackTraceElement[] message=new StackTraceElement[]{new StackTraceElement("message : "+msg+" in method : ",tr.getMethod().getMethodName(),tr.getTestClass().getRealClass().getSimpleName(),index)};
74 index=0;
75 alltrace=this.merge(alltrace, et);
76 alltrace=this.merge(alltrace, message);
77 return alltrace;
78 }
79
80 /**
81 * 根據(jù)測試類名獲得該測試類的StackTraceElement數(shù)組
82 * @param tr
83 * @param stackTraceElements
84 * @return
85 */
86 private StackTraceElement[] getKeyStackTrace(ITestResult tr,StackTraceElement[] stackTraceElements){
87 List<StackTraceElement> ets=new ArrayList<StackTraceElement>();
88 for (StackTraceElement stackTraceElement : ets) {
89 if(stackTraceElement.getClassName().equals(tr.getTestClass().getName())){
90 ets.add(stackTraceElement);
91 index=stackTraceElement.getLineNumber();
92 }
93 }
94 StackTraceElement[] et=new StackTraceElement[ets.size()];
95 for (int i = 0; i < et.length; i++) {
96 et[i]=ets.get(i);
97 }
98 return et;
99 }
100
101 /**
102 * 合并兩個StackTraceElement數(shù)組
103 * @param trace1
104 * @param trace2
105 * @return
106 */
107 private StackTraceElement[] merge(StackTraceElement[] trace1,StackTraceElement[] trace2){
108 StackTraceElement[] ste=new StackTraceElement[trace1.length+trace2.length];
109 for (int i = 0; i < trace1.length; i++) {
110 ste[i]=trace1[i];
111 }
112 for (int i = 0; i < trace2.length; i++) {
113 ste[trace1.length+i]=trace2[i];
114 }
115 return ste;
116 }