您的位置:首页 > 移动开发 > Android开发

Android单元测试设置

2015-11-26 16:59 393 查看
很简单的东西,但是总是忘记。记一笔

Manifest文件添加内容

需要分别在Manifest文件中添加两个部分,一个是在Application外部,另外一个是在Application内Activity外部。具体添加部分如下:

添加在Application外部的内容:

其中的
targetPackage
根据实际的包路径填写,label随意

<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.zp.shortshot" android:label="Junit Test" />


添加在Application内Activity外的内容:

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


添加完成之后的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zp.shortshot"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- 第一个添加-- >
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.zp.shortshot" android:label="Junit Test" />

<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.zp.shortshot.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


例如对获取系统应用信息的一个功能类进行的测试:

public class TestGetAppInfo extends AndroidTestCase {

public void getApps() throws Exception {
AppInfoProvider provider = new AppInfoProvider(getContext());
List<AppInfo> infos = provider.getAllApps();
for (AppInfo info : infos) {
System.out.print("name : " + info.getName());
System.out.println("\t pkgName : " + info.getPkgName());
}
}
}


最后部署运行的时候以
Android Junit Test
即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息