首页 / 操作系统 / Linux / 对Android应用进行单元测试
是关于对Android应用进行单元测试的,在android应用开发中很常用的,可以测试android应用的代码测试、检测程序处理的正确性,在一个应用开发中单元测试框架是不可必少的,下面通过一个例子来进行讲解:我的思路:首先创建个单元测试项目,再在项目中创建一个被测试的类文件,通过单元测试对被测试类里面的一个方法进行测试,为了好解释就写给简单的了,代码如下:package com.betest.test;public class betest {public int test() { String in = "test"; int b = new Integer(in);} }从上面看代码执行的过程中会出错吧 下面编辑测试文件: 因为是在android项目中使用单元测试,首先需要在项目功能清单文件(也就是AndroidMainfest.xml)中加入调用测试类库。 代码如下:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.test" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <!--调用测试类库--> <uses-library android:name="android.test.runner" /> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="8" /> <!-- 包名要去测试文档包名相同 --> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.test.test" /> </manifest>上面的代码中也就是加入了 <uses-library android:name="android.test.runner" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.test.test" /> 这两句话。 开始创建测试类,创建个单元测试方法,代码如下: package com.test.test;import junit.framework.Assert; import com.betest.test; import android.test.AndroidTestCase; import android.util.Log;public class PersonServiceTest extends AndroidTestCase { public void testtest() throws Throwable{ PersonService service = new PersonService(); service.test();//检验save()方法运行是否正常} }到这里代码已经写完,下面就开始进行测试了,在outline里面找到相应的单元测试方法,右键 找到 Android Junit Test 在日志文件中就可以看到错误原因了。更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11
收藏该网址