复制自:http://www.oschina.net/question/54100_27061
参考资料:http://hi.baidu.com/shenhuanyu09/item/91c57656107e47c79e2667b4
Android JUnit深入浅出系列博文
十分感谢原作者!
Instrumentation和Activity有点类似,只不过Activity 是需要一个界面的,而Instrumentation并不是这样的,我们可以将它理解为一种没有图形界面的,具有启动能力的,用于监控其他类(用 Target Package声明)的工具类。
1 package com.hustophone.sample.test; 2 3 import com.hustophone.sample.R; 4 import com.hustophone.sample.Sample; 5 import android.content.Intent; 6 import android.os.SystemClock; 7 import android.test.InstrumentationTestCase; 8 import android.util.Log; 9 import android.widget.Button; 10 import android.widget.TextView; 11 12 public class SampleTest extends InstrumentationTestCase { 13 private Sample sample = null; 14 private Button button = null; 15 private TextView text = null; 16 17 /* 18 * 初始设置 19 * @see junit.framework.TestCase#setUp() 20 */ 21 @Override 22 protected void setUp() { 23 try { 24 super.setUp(); 25 } catch (Exception e) { 26 e.printStackTrace(); 27 } 28 Intent intent = new Intent(); 29 intent.setClassName("com.hustophone.sample", Sample.class.getName()); 30 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 31 sample = (Sample) getInstrumentation().startActivitySync(intent); 32 text = (TextView) sample.findViewById(R.id.text1); 33 button = (Button) sample.findViewById(R.id.button1); 34 } 35 36 /* 37 * 垃圾清理与资源回收 38 * @see android.test.InstrumentationTestCase#tearDown() 39 */ 40 @Override 41 protected void tearDown() { 42 sample.finish(); 43 try { 44 super.tearDown(); 45 } catch (Exception e) { 46 e.printStackTrace(); 47 } 48 } 49 50 /* 51 * 活动功能测试 52 */ 53 public void testActivity() throws Exception { 54 Log.v("testActivity", "test the Activity"); 55 SystemClock.sleep(1500); 56 getInstrumentation().runOnMainSync(new PerformClick(button)); 57 SystemClock.sleep(3000); 58 assertEquals("Hello Android", text.getText().toString()); 59 } 60 61 /* 62 * 模拟按钮点击的接口 63 */ 64 private class PerformClick implements Runnable { 65 Button btn; 66 public PerformClick(Button button) { 67 btn = button; 68 } 69 70 public void run() { 71 btn.performClick(); 72 } 73 } 74 75 /* 76 * 测试类中的方法 77 */ 78 public void testAdd() throws Exception{ 79 String tag = "testAdd"; 80 Log.v(tag, "test the method"); 81 int test = sample.add(1, 1); 82 assertEquals(2, test); 83 } 84 }
在testActivity()这个测试方法中,我模拟了一个按钮点击事件,然后来判 断程序是否按照预期的执行。在这里PerformClick这个方法继承了Runnable接口,通过新的线程来执行模拟事件,之所以这么做,是因为如果 直接在UI线程中运行可能会阻滞UI线程。
要想正确地执行测试,还需要修改AndroidManifest.xml这个文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.hustophone.sample" android:versionCode="1" 4 android:versionName="1.0"> 5 <application android:icon="@drawable/icon" android:label="@string/app_name"> 6 <!--用于引入测试库--> 7 <uses-library android:name="android.test.runner" /> 8 <activity android:name=".Sample" android:label="@string/app_name"> 9 <intent-filter> 10 <action android:name="android.intent.action.MAIN" /> 11 <category android:name="android.intent.category.LAUNCHER" /> 12 </intent-filter> 13 </activity> 14 </application> 15 16 <uses-sdk android:minSdkVersion="3" /> 17 <!--表示被测试的目标包与instrumentation的名称。--> 18 <instrumentation android:targetPackage="com.hustophone.sample" android:name="android.test.InstrumentationTestRunner" /> 19 </manifest>
也可以通过模拟器运行单元测试。点击模拟器界面的Dev Tools菜单,再点击Instrumentation选项,进入Instrumentation菜单,这里有一个InstrumentationTestRunner,它是测试的入口,点击这个选项,就可以自动运行我们的测试代码。