Android開發基礎之Android Studio單元測試
來源:程序員人生 發布時間:2015-03-31 08:02:35 閱讀次數:3844次
android studio默許支持單元測試,相對Eclipse要方便1些。
新建完1個項目以后你會發現同時生成了1個ApplicationTest類,如圖:
一樣,假定已實現了1個計算百分比功能需要測試,代碼:
public class ProgressService {
public Integer getCurrentProgerss(double current, double max) {
Integer i=(int)((current / max) * 100) ;
return i;
}
}
具體步驟以下:
1 測試類
在ApplicationTest包中新建1個測試類,來履行我們的功能,這個類需要繼承InstrumentationTestCase。
import android.test.InstrumentationTestCase;
public class TestClass extends InstrumentationTestCase {
public void test() throws Exception{
ProgressService progressService=new ProgressService();
Integer pro=progressService.getCurrentProgerss(20, 70);
Log.i(TAG, pro.toString());
}
}
2 運行
選中剛剛新建的測試類TestClass,右鍵->run->TestClass,如圖:

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈