您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
了解JUnit核心類、接口及生命周期
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/8/29 15:25:33 ] 推薦標(biāo)簽:

TestResult

TestResult 有兩個(gè) List,用來記錄 Exception 和 Failure。捕獲 runBare() 拋出的 Exception,首先判斷是否為 AssertionFailedError,是則調(diào)用 addFailure() 把,把異常加到 fFailures。否則則并調(diào)用 addError() 方法,把異常加到 fErrors 中。

catch (AssertionFailedError e) {
    addFailure(test, e);
}
catch (ThreadDeath e) { // don't catch ThreadDeath by accident
    throw e;
}
catch (Throwable e) {
    ddError(test, e);
}

TestListener

前面提到 result 加上了一個(gè) ResultPrinter,ResultPrinter 會記錄運(yùn)行中的所有 Exception,并且實(shí)時(shí)地以不同的格式輸出。當(dāng)所有的 Test 都運(yùn)行完畢后,ResultPrinter 會對 result 進(jìn)行分析,首先輸出運(yùn)行的時(shí)間,接著 printError() 輸出 fErrors 的個(gè)數(shù),printFailures() 則輸出 fFailures 的個(gè)數(shù)。PrintFooter() 根據(jù) result.wasSuccessful(),如果成功,則打印 OK 和 test 運(yùn)行的總次數(shù),如果失敗,則打印出 test 總的運(yùn)行的個(gè)數(shù),失敗和錯(cuò)誤的個(gè)數(shù)。

參數(shù)一的統(tǒng)計(jì)輸出結(jié)果:

    
Time: 0.016
There was 1 failure:
1) testPay(TestShoppingCart)junit.framework.AssertionFailedError:
expected:<30> but FAILURES!!!
Tests run: 2,  Failures: 1,  Errors: 0

 

清單一:

Java代碼

    synchronized void print(TestResult result, long runTime) {  
        printHeader(runTime);  
        printErrors(result);  
        printFailures(result);  
        printFooter(result);  
    }  

清單二:

Java代碼

    protected void printFooter(TestResult result) {  
        if (result.wasSuccessful()) {  
            getWriter().println();  
            getWriter().print("OK");  
            getWriter().println (" (" + result.runCount() + " test"  
                + (result.runCount() == 1 ? "": "s") + ")");  
     
        } else {  
            getWriter().println();  
            getWriter().println("FAILURES!!!");  
            getWriter().println("Tests run: "+result.runCount()+  
                ",  Failures: "+result.failureCount()+  
                ",  Errors: "+result.errorCount());  
        }  
        getWriter().println();  
    }  

完整生命周期

整個(gè)生命周期將在下圖顯示:

上一頁1234下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd