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);
}
}
下面來簡單講解一下代碼:setUp()和tearDown()都是受保護的方法,通過繼承可以覆寫這些方法。
在android Developer中有如下的解釋
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,初始化資源等。tearDown ()則用來垃圾清理與資源回收。
在testActivity()這個測試方法中,我模擬了一個按鈕點擊事件,然後來判斷程序是否按照預期的執行。在這裏PerformClick這個方法繼承了Runnable接口,通過新的線程來執行模擬事件,之所以這麼做,是因爲如果直接在UI線程中運行可能會阻滯UI線程。
<instrumentation android:targetPackage="com.hustophone.sample"
android:name="android.test.InstrumentationTestRunner" />
表示被測試的目標包與instrumentation的名稱。
經過以上步驟,下面可以開始測試了。測試方法也有以下幾種,下面介紹兩個常用的方法:
在Eclipse中選擇工程Sample,單擊右鍵,在Run as子菜單選項中選擇Android JUnit Test
點擊模擬器界面的Dev Tools菜單
按鈕點擊前
至此,一個簡單的測試過程結束了。當然,android的測試內容還有很多,也有比較複雜的,我會在以後的學習過程中繼續分享我的體會。好了,今天就到這裏吧!
參考資料: