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,如图:
![](http://www.wfuyu.com/uploadfile/cj/20150307/20150306123840358.png)