Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 隨筆:單元測試入門實作

編輯:關於 Android 程式設計

在實際開發中,開發 Android 軟體的過程需要不斷測試,因此掌握單元測試格外重要。你應該把單元測試當作 Android 應用開發週期的一部分——精心編寫的測試能在開發早期幫你揪出錯誤。

關於 Android 單元測試,可以參考 Google 官方的 Best Practices for Testing(需要 FQ)。

一、建立 Android Test Project

1. 先建立一個名為 Hello 的 Android Project,並把版面檔改成如下:

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >
   <TextView
     android:id="@+id/text"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="@string/hello_world" />
   <!-- 直接寫字串並不建議,應寫在 strings.xml 中 -->
   <EditText
     android:id="@+id/edit"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="請輸入" />
 </LinearLayout>

2. 建立 Hello 對應的測試專案:File → New → Other → Android → Android Test Project,在「Project Name」輸入「HelloTest」,點「Next」。
3. 在「Select Test Target」中選取要測試的 Android 專案,點 Finish 即可。

建立完成後,打開 HelloTest 的 AndroidManifest.xml,你會發現它比一般專案多了些設定:

 <manifest xmlns:android="https://schemas.android.com/apk/res/android"
   package="com.lsj.hello.test"
   android:versionCode="1"
   android:versionName="1.0" >
   <uses-sdk android:minSdkVersion="8" />
   <!-- 設定單元測試框架的啟動裝置,一般使用 InstrumentationTestRunner -->
   <!-- targetPackage 與 Hello 相同,代表測試框架與應用處於同一進程 -->
   <instrumentation
     android:name="android.test.InstrumentationTestRunner"
     android:targetPackage="com.lsj.hello" />
   <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
     <!-- 引入單元測試框架的相依庫 -->
     <uses-library android:name="android.test.runner" />
   </application>
 </manifest>

二、單元測試程式碼編寫

1. 建立 Android Test Project 後,HelloTest 會自動產生 com.lsj.hello.tests 套件。命名慣例如下:
套件:com.lsj.example → com.lsj.example.tests(對應套件 + 「.tests」)
類別:Hello.java → HelloTest.java(對應類別 + 「Test」)
方法:Hello() → testHello()(「test」+ 對應方法名)

2. 在 com.lsj.hello.tests 下建立 MainActivityTest.java,繼承自 ActivityInstrumentationTestCase2。

3. 編輯 MainActivityTest,加入單元測試程式碼:

 public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
   private MainActivity mActivity;
   private TextView mTextView;
   private EditText mEditText;
   private Instrumentation mInstrumentation;

   public MainActivityTest() {
     super(MainActivity.class); // super 內必須為泛型類,否則報錯
   }

   @Override protected void setUp() throws Exception {
     super.setUp();
     setActivityInitialTouchMode(false); // 關閉 touch 模式,否則 key 事件會被忽略
     mInstrumentation = getInstrumentation();
     mActivity = this.getActivity();
     mTextView = (TextView) mActivity.findViewById(com.lsj.hello.R.id.text);
     mEditText = (EditText) mActivity.findViewById(com.lsj.hello.R.id.edit);
   }

   /** 測試資料初始化是否為空 */
   public void testInit() {
     assertNotNull(mActivity);
     assertNotNull(mTextView);
     assertNotNull(mEditText);
   }

   /** 測試文字方塊字串是否相等 */
   public void testTextViewString() {
     assertEquals(
       mActivity.getResources().getString(com.lsj.hello.R.string.hello_world),
       mTextView.getText().toString());
   }

   /** 測試輸入 */
   public void testEditTextInput() {
     input();
     assertEquals("hello", mEditText.getText().toString());
   }

   /** 模擬輸入 */
   public void input() {
     // UI 操作需在 UI 執行緒進行,故用 runOnUiThread
     mActivity.runOnUiThread(new Runnable() {
       @Override public void run() {
         mEditText.requestFocus();
         mEditText.performClick();
       }
     });
     // 等待測試執行緒與 UI 執行緒同步
     mInstrumentation.waitForIdleSync();
     sendKeys(KeyEvent.KEYCODE_H, KeyEvent.KEYCODE_E, KeyEvent.KEYCODE_L,
       KeyEvent.KEYCODE_L, KeyEvent.KEYCODE_O);
   }

   @Override protected void tearDown() throws Exception {
     super.tearDown(); // 測試完成後進行善後
   }
 }

其中,ActivityInstrumentationTestCase2 繼承自 TestCase(位於 junit.framework 套件),Android 的測試類別皆繼承自 TestCase。建立一個單元測試大致需要:繼承 TestCase 或其子類、定義要使用的實例變數、在 setUp() 中初始化變數與測試環境、執行測試,最後在 tearDown() 中進行善後。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved