程序员人生 网站导航

Android开发基础之Android Studio单元测试

栏目:综合技术时间:2015-03-31 08:02:35
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,如图:


------分隔线----------------------------
------分隔线----------------------------

最新技术推荐