單元測試系列二 單元測試如何測試異常與超時

2021-07-23 21:32:47 字數 1199 閱讀 9197

一、 測試異常

1. 使用@test的expected屬性測試異常

// 第一種方式, 使用expected屬性

@test(expected = filenotfoundexception.class)

public

void

usingexpected() throws filenotfoundexception

2. 使用try/catch,fail的方式測試異常
// 第二種方式, 使用try/catch和fail方法

@test

public

void

usingtrycatchandfail() catch (filenotfoundexception e)

}

3. 使用@rule,expectedexception的方式測試異常
// 第三種方式, 使用expectedexception規則

@rule

public expectedexception thrown = expectedexception.none();

@test

public

void

shouldtestexceptionmessage() throws filenotfoundexception

二、 測試超時

1. 使用@test的timeout屬性測試超時

// 第一種方式,使用timeout屬性

@test(timeout=1000)

public

void

timeoutin1seconds()

}

2. 使用@rule,timeout的方式測試超時
// 第二種方式,使用timeout規則

@rule

public timeout globaltimeout=new timeout(5000);

這個seconds方法只在junit 4.12版本之後才有效

@test

public

void

timeoutin5seonds()

}

單元測試 單元測試文章收藏

前言 前段時間公司計畫做自動化測試,自己也打算圍繞幾個點做相關調研,現在想想呢?其實對自動化測試的概念都還不是十分清晰,當時主要還是圍繞 單元測試 向qa小夥伴學習了一段時間,現由於公司重組,學習中斷,這裡簡單記錄一些單元測試好文,留待後續參考.什麼叫自動化測試?自動化測試覆蓋率?覆蓋率如何做到的?...

單元測試之Django單元測試

每個應用,自帶tests.py 整合在django的專案檔案裡,更多是開發人員寫django自動的測試執行 3.1 前後置方法執行特點 django.test.testcase類主要由前 後置處理方法和test開頭的方法組成 特點 繼承於django.test.testcase 測試用例都是test...

走進單元測試五 單元測試文章系列目錄

目錄 1,走進單元測試一 初認unit test 2,走進單元測試二 測試需要從哪些方面著手 3,走進單元測試三 實戰單元測試 4,走進單元測試四 單元測試背後的思考和感悟 note 在編 的過程進行單元測試將是最佳實踐!通過一周的時間整理了一下自己對於單元測試的理解,也是一種反省把,在總結過程中發...