1、在进行单元测试前首先必须AndroidManifest.xml中进行配置:


(1)<!-- 在根节点 manifest节点下放置 指令集 instrumentation -->

<instrumentation

android:name="android.test.InstrumentationTestRunner"

android:label="Tests for My App"

android:targetPackage="com.xunfang.junit" />

上面这些代表单元测试的框架的启动装置,启动装置有好几类,默认使用上面的这种。


targetPackage与上面的package相同,为的是使单元测试和当前应用处于同一进程中


(2)<!-- 使用的函数库 application节点的下面-->

<uses-library android:name="android.test.runner" />


代表单元测试的函数库


附上代码:

AndroidManifest.xml中的配置:


<uses-sdk

android:minSdkVersion="14"

android:targetSdkVersion="16" />

<!-- 配置在manifest中 -->

<instrumentation

android:name="android.test.InstrumentationTestRunner"

android:targetPackage="com.xunfang.junit" >

</instrumentation>


<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<!-- 配置在application标签中 -->

<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>

上面两段有背景色的是配置全局文件中的配置


业务逻辑中的代码:

public class MyMath {


public int add(int a, int b ){

return a + b;

}

}


单元测试代码:

public class MyJuniteTestCase extends AndroidTestCase {


public void test(){

MyMath my = new MyMath() ;

assertEquals(10, my.add(4, 6)) ;

}

}


测试步骤:

(1) 项目--》右键--》Run As--》Android Jutil Test

(2)打开单元测试代码--》在outLine中选择你要测试的方法--》右键--》Run As--》Android Jutil Test