星空网 > 软件开发 > 操作系统

android菜鸟学习笔记15

Android中的Junit测试与Java Junit测试有所不同,不能简单的使用标注……

假设写了一个MathUtils类,有两个静态方法:

 1 public class MathUtils { 2  3    public static int add(int x, int y){ 4  5      return x+y; 6  7    } 8  9    public static int mul(int x, int y){10 11      return x*y;12 13    }14 15 }

 要测试这两个方法,可以在当前项目中新建一个类继承自AndroidTestCase,在该类中编写测试方法:

 1 public class Test extends AndroidTestCase { 2  3    @Override 4  5    protected void setUp() throws Exception { 6  7      // TODO Auto-generated method stub 8  9      super.setUp();10 11    }12 13    public void testAdd(){14 15      assertEquals(5, MathUtils.add(2, 3));16 17    }18 19 public void testMul(){20 21      assertEquals(6, MathUtils.mul(2, 3));22 23    }24 25    @Override26 27    protected void tearDown() throws Exception {28 29      // TODO Auto-generated method stub30 31      super.tearDown();32 33    }34 35 }

 注意重写父类的setUp()方**在所有测试用例执行之前被调用,可以用来放置一些初始化操作;tearDown()方法在所有测试用例执行之后被调用,可以用来放置一些资源释放操作。

这时,Run as Junit Test,会出现如下错误:

008test does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.

这是因为在执行之前,还需要在Manifest.

修改Manifest.

 1 <manifest ="http://schemas.android.com/apk/res/android" 2  3   package="cn.csc.test" 4  5   android:versionCode="1" 6  7   android:versionName="1.0" > 8  9 10 11   <uses-sdk12 13     android:minSdkVersion="10"14 15     android:targetSdkVersion="18" />16 17   <instrumentation android:name="android.test.InstrumentationTestRunner"18 19     android:targetPackage="cn.csc.test">20 21   </instrumentation>22 23   <application24 25     android:allowBackup="true"26 27     android:icon="@drawable/ic_launcher"28 29     android:label="@string/app_name"30 31     android:theme="@style/AppTheme" >32 33    34 35     <uses-library android:name="android.test.runner"/>36 37   </application>38 39 40 41 </manifest>

注意:instrumentation节点是防止manifest根节点下的,其targetPackage属性指定要测试的项目的包名,这里测试自己,所以写的是该项目自己的包名。

uses-library是放在application节点下的。

此时,再右击Test,选择Run as Android Junit Test

 android菜鸟学习笔记15images/loading.gif' data-original="http://images0.cnblogs.com/blog2015/693030/201507/080934288614251.png" />

发现testAdd()和testMul()都被调用了。

若修改下testMul方法,将其重命名为mulTest(),再右击Test,选择Run as Android Junit Test

 android菜鸟学习笔记15

只有testAdd()方法被调用了,mulTest()没有被调用。

可以如同Java Junit一样,右击某个方法名,如mulTest(),run as android junit test:

 android菜鸟学习笔记15

这时,mulTest()自己执行了。

通过以上可以,执行测试类时,会自动把所有以test打头的方法都执行一遍的。要执行单个测试方法,可以右击要执行的方法名,然后run as android junit test。

上面的示例是在一个项目中自己测试自己,若要测试别的项目怎么办?

测试其他项目

如,新建一个009mathutils项目,把MathUtils放到该项目的cn.csc.mathutils包下,然后要在008test中测试这个MathUtils的方法:

修改008test中的Manifest.

1 <instrumentation android:name="android.test.InstrumentationTestRunner"2 3     android:targetPackage="cn.csc.mathutils">

 

只是这样,还是不行的,因为cn.csc.mathutils中的MathUtils类在008test中是不可见的,这时需要进行一下build path的设置:

右击008test项目名,选择build path,configure build path:

 android菜鸟学习笔记15

在Projects选项卡中,点击add按钮,添加009mathutils项目,然后Ok即可。

 android菜鸟学习笔记15

此时,009mathutils项目中的类在008test中就可见了。然后就可以进行正常的测试工作了。

其实,要测试其他项目有更简单的方式:

新建一个Android Test Project :

new ->  other

 android菜鸟学习笔记15

选择Android Test Project ,然后next

 android菜鸟学习笔记15

设置项目名称,然后next

 android菜鸟学习笔记15

选择要测试的项目,这里选择009mathutils,next

之后,默认,然后finish即可。

在创建该项目的过程中, Manifest.

补充:adb工具还能对项目进行冒烟测试(压力测试)   命令格式:adb shell monkey -p 要测试项目的包名 -v 测试事件数量

如: adb shell monkey -p cn.csc.lifecycle -v 1000

测试过程:

android菜鸟学习笔记15

android菜鸟学习笔记15

可以测试出程序中一些不易发现的bug。

 




原标题:android菜鸟学习笔记15

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

KC认证:https://www.ikjzd.com/w/3375
梯拓跨境:https://www.ikjzd.com/w/3376
Teezily:https://www.ikjzd.com/w/3377
dhgate:https://www.ikjzd.com/w/3378
亚马逊A+:https://www.ikjzd.com/w/3379
Supply Chain Connect:https://www.ikjzd.com/w/338
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流