android开发平台下搭建junit测试框架环境

来源:互联网 发布:公司域名 编辑:程序博客网 时间:2024/05/16 14:59

   在android平台下搭建junit和java平台不太一样。在android平台下主要有两点。

   第一点:写测试用例:

public class AppServiceTest  extends AndroidTestCase{public void testAdd() throws Exception{AppService as = new AppService();int result = as.add(1, 2);assertEquals(3, result);}}

在这个测试用例里面需要注意两点,一:必须继承AndroidTestCase  二:测试方法必须抛出所有异常也就是 throw Exception


第二点:在AndroidManifest.xml文件里面添加两处配置。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.evan.junit"    android:versionCode="1"    android:versionName="1.0" >    <!-- 新添加 -->    <instrumentation        android:name="android.test.InstrumentationTestRunner"        android:targetPackage="com.evan.junit" />    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <!-- 新添加 -->        <uses-library android:name="android.test.runner" />        <activity            android:name="com.evan.junit.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>

就是代码中“新添加”注释的下面语句,记得别添加错了。

现在你就可以回到你的单元测试里面了,进行右键run as ----- android junit test

0 0