Android 项目添加单元测试

来源:互联网 发布:网络规划设计师 没人考 编辑:程序博客网 时间:2024/04/30 00:46

Android 单元测试

修改配置文件:AndroidManifest.xml

1.在<application> 中添加 引入单元测试包

<uses-library android:name="android.test.runner"/>
  2.在<manifest>中添加测试
<instrumentation android:name="android.test.InstrumentationTestRunner" 
android:targetPackage="com.yueking.callocation"/>
配置文件 整体像这样:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"          package="com.yueking.callocatioin"          android:versionCode="1"          android:versionName="1.0">    <uses-sdk android:minSdkVersion="15"/>    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">        <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>       <span style="color:#ff0000;"> </span><span style="background-color: rgb(255, 0, 0);"><uses-library android:name="android.test.runner"/></span>    </application>    <span style="background-color: rgb(255, 0, 0);"><instrumentation android:name="android.test.InstrumentationTestRunner"                     android:targetPackage="com.yueking.callocatioin"/></span></manifest>
3.最后编写测试用类,并 继承至 android.test.AndroidTestCase即可
测试中打印日志 用 Log.i(String,String) 方法
实例代码:
<pre name="code" class="java">package com.yueking.callocatioin.service;import android.test.AndroidTestCase;import android.test.AndroidTestRunner;import android.util.Log;public class PersonServiceTest extends AndroidTestCase {    static String tag = "yueking";    public void setUp() throws Exception {    }    public void testGetLocation() throws Exception {         Log.i(tag, "hello yueking");    }}


0 0
原创粉丝点击