Android 單元測試 二 之JUnit高階

2021-08-19 10:47:09 字數 2085 閱讀 2458

本文接上篇文章junit基礎繼續學習junit。

套件測試

套件測試說的通俗點,就是批量執行測試類。涉及註解@runwith @suite

接著使用上篇的calculater,建立兩個測試類:

/**

* created by scy on 2018/4/26.

*/public

class

calculatertest

@test

//我們需要測試的方法

public

void

sum() throws exception

@test

public

void

substract() throws exception

}

/**

* created by scy on 2018/4/27.

*/public

class

calculatertest2

@test

public

void

divide() throws exception

@ignore

//表示該方法禁用

public

void

multiply() throws exception

}

然後建立乙個測試套件 suitetest ,以便將上面的類在一起執行:

/**

* created by scy on 2018/4/27.

*/@runwith(suite.class)

@suite.suiteclasses()//被測試類

public class

suitetest

執行suitetest即可,結果顯示兩個類裡面的三個待測方法,全部通過;

引數化測試

junit 4 引入了乙個新的功能引數化測試。引數化測試允許開發人員使用不同的值反覆執行同乙個測試。它要滿足下列要求:

/**

* created by scy on 2018/4/27.

*/@runwith(parameterized.class)

public

class

calculatertest3 , });

}//接收並儲存(例項化)測試資料

public

calculatertest3(int expected, int a, int b)

@test

//使用測試資料測試

時間測試junit 提供了乙個暫停的方便選項。如果乙個測試用例比起指定的毫秒數花費了更多的時間,那麼 junit 將自動將它標記為失敗。

/**

* created by scy on 2018/4/27.

*/public

class

timetest

}

上述例子中,testtime()方法執行時間小於1000,單元測試會通過。

異常測試

/**

* created by scy on 2018/4/27.

*/public

class

excetiontest

}

在上述例子中,testexception()方法將丟擲nullpointerexception異常,因為這是乙個預期的異常,因此單元測試會通過。

Android之單元測試學習

1.單元測試概念 單元測試 又稱為模組測試 檢驗程式模組 軟體設計的最小單位 正確性的測試工作,常常是程式設計師寫的一段 對於物件導向程式設計,最小單元就是方法,包括基類 超類 抽象類 或者派生類 子類 中的方法。android中的測試框架是擴充套件的junit3,所以在學習android的單元測試...

android 單元測試

1 manifest.xml 檔案的新增 xmlversion 1.0 encoding utf 8 manifest xmlns android package com.zsw.test android versioncode 1 android versionname 1.0 uses sdk ...

Android單元測試

單元測試 認識 在每個專案完成時,測試的過程中總是會發現各種bug,瞬間就奇怪了明明沒問題卻出現了問題why?你問我我也不知道啊 言歸正傳,在開發實際中,往往大部分開發者思維都會被侷限在某乙個功能上面,一些隱藏的問題沒想到也不會想到,那麼單元測試就有了,最近看了一些單元測試基礎 第一次接觸 就我個人...