1.首先建立一个android project,其文件结构最终如下:
2、布局文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hustophone.sample" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <!--用于引入测试库--> <uses-library android:name="android.test.runner" /> <activity android:name=".Sample" 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> <uses-sdk android:minSdkVersion="3" /> <!--表示被测试的目标包与instrumentation的名称。--> <instrumentation android:targetPackage="com.hustophone.sample" android:name="android.test.InstrumentationTestRunner" /> </manifest>3、被测程序 Sample类
package com.hustophone.sample; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class Sample extends Activity { private TextView myText = null; private Button button = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myText = (TextView) findViewById(R.id.text1); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub myText.setText("Hello Android"); } }); } public int add(int i, int j) { // TODO Auto-generated method stub return (i + j); } }这个程序的功能比较简单,点击按钮之后,TextView的内容由Hello变为Hello Android.同时,在这个类中,我还写了一个简单的方法,没有被调用,仅供测试而已。
package com.hustophone.sample.test; import com.hustophone.sample.R; import com.hustophone.sample.Sample; import android.content.Intent; import android.os.SystemClock; import android.test.InstrumentationTestCase; import android.util.Log; import android.widget.Button; import android.widget.TextView; public class SampleTest extends InstrumentationTestCase { private Sample sample = null; private Button button = null; private TextView text = null; /* * 初始设置 * * @see junit.framework.TestCase#setUp() */ @Override protected void setUp() { try { super.setUp(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Intent intent = new Intent(); intent.setClassName("com.hustophone.sample", Sample.class.getName()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); sample = (Sample) getInstrumentation().startActivitySync(intent); text = (TextView) sample.findViewById(R.id.text1); button = (Button) sample.findViewById(R.id.button1); } /* * 垃圾清理与资源回收 * * @see android.test.InstrumentationTestCase#tearDown() */ @Override protected void tearDown() { sample.finish(); try { super.tearDown(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * 活动功能测试 */ public void testActivity() throws Exception { Log.v("testActivity", "test the Activity"); SystemClock.sleep(1500); getInstrumentation().runOnMainSync(new PerformClick(button)); SystemClock.sleep(3000); assertEquals("Hello Android", text.getText().toString()); } /* * 模拟按钮点击的接口 */ private class PerformClick implements Runnable { Button btn; public PerformClick(Button button) { btn = button; } public void run() { btn.performClick(); } } /* * 测试类中的方法 */ public void testAdd() throws Exception{ String tag = "testAdd"; Log.v(tag, "test the method"); int test = sample.add(1, 1); assertEquals(2, test); } }下面来简单讲解一下代码:
protected void setUp () Since: API Level 3 Sets up the fixture, for example, open a network connection. This method is called before a test is executed. protected void tearDown () Since: API Level 3 Make sure all resources are cleaned up and garbage collected before moving on to the next test. Subclasses that override this method should make sure they call super.tearDown() at the end of the overriding method.setUp ()用来初始设置,如启动一个Activity,初始化资源等。
至此,一个简单的测试过程结束了。当然,android的测试内容还有很多,也有比较复杂的,我会在以后的学习过程中继续分享我的体会。好了,今天就到这里吧!
参考资料: