android studio默认支持单元测试,相对于Eclipse要方便一些。
新建完一个项目之后你会发现同时生成了一个ApplicationTest类,如图:

同样,假设已经实现了一个计算百分比功能需要测试,代码:
新建完一个项目之后你会发现同时生成了一个ApplicationTest类,如图:
同样,假设已经实现了一个计算百分比功能需要测试,代码:
public class ProgressService {
public Integer getCurrentProgerss(double current, double max) {
Integer i=(int)((current / max) * 100) ;
return i;
}
}
具体步骤如下:
一 测试类
在ApplicationTest包中新建一个测试类,来执行我们的功能,这个类需要继承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());
}
}
二 运行
选中刚刚新建的测试类TestClass,右键->run->TestClass,如图: