首先AndroidManifest.xml
View Code
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.travelsky.test" android:versionCode="1"android:versionName="1.0"><uses-sdk android:minSdkVersion="3" /><uses-permission android:name="android.permission.RUN_INSTRUMENTATION" /><instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.travelsky.test" android:label="Test for my app" /><application android:icon="@drawable/icon" android:label="@string/app_name"><uses-library android:name="android.test.runner" /><activity android:name=".AndroidTestActivity" 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>
添加
<uses-library android:name="android.test.runner" />
<instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.travelsky.test" android:label="Test for my app" />
编写单元测试代码:
View Code
package com.travelsky.test;import android.app.Activity; import android.test.ActivityInstrumentationTestCase2; import android.widget.TextView;public class Testa extends ActivityInstrumentationTestCase2 {Activity mActivity = null;TextView mView = null;public Testa() {super("com.travelsky.test", AndroidTestActivity.class);// TODO Auto-generated constructor stub }@Overrideprotected void setUp() throws Exception {super.setUp();mActivity = this.getActivity();mView = (TextView) mActivity.findViewById(com.travelsky.test.R.id.tv);}public void testPreconditions() {assertNotNull(mView);}@Overrideprotected void tearDown() throws Exception {// TODO Auto-generated method stubsuper.tearDown();}}
run