Android:单元测试Junit的配置

来源:互联网 发布:如何做好淘宝客服 编辑:程序博客网 时间:2024/05/16 12:20


 在实际开发中,开发android软件的过程需要不断地进行测试。而使用Junit测试框架,是正规Android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性..........

第一步:首先在AndroidManifest.xml中加入下面代码:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.android_db"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />         <!-- 记住这个一要放在application外面,不然会出现配置错误 信息 -->      <instrumentation android:name="android.test.InstrumentationTestRunner"         android:targetPackage="com.example.android_db">    </instrumentation>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >       <!-- 在本应用中导入需要使用的包,放在application里面activity外面 -->          <uses-library android:name="android.test.runner"/>        <activity            android:name="com.example.android_db.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></manifest>

第二步:编写单元测试代码:(编写一个类并继承AndroidTestCase类)
</pre><p></p><p><span style="font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: Arial; font-size: 14px; line-height: 26px;"><strong></strong></span></span></span></p><pre name="code" class="html">package com.example.android_db;import android.test.AndroidTestCase;public class MyTest extends AndroidTestCase{public MyTest(){}public void createDB(){MyDbOpenHelper myDbOpenHelper=new MyDbOpenHelper(getContext());myDbOpenHelper.getWritableDatabase();}}

选择要测试的方法,右键点击“Run As”--“Android Junit Test”。


0 0