http://download.csdn.net/detail/stevenhu_223/4895796
本文是在上一篇文章《java單元測試》的基礎上繼續講解android的單元測試,android源碼中引入了java單元測試的框架(android源碼目錄:libcore\junit\src\main\java\junit\framework中可見),然後在java單元測試框架的基礎上擴展屬於android自己的測試框架。android具體框架類的關係圖如下:
從上圖的類關係圖中可以知道,通過android測試類可以實現對android中相關重要的組件進行測試(如Activity,Service,ContentProvider,甚至是application)。
其實在android源碼中,基本上每個系統應用都自帶一個測試工程,如下圖的源碼中settings(設置)模塊:
上圖的tests文件夾中就是settings模塊自帶的單元測試工程,有興趣的讀者可自行去研讀一下源代碼。
eclipse下(當然,前提是要保證eclipse中相關的android環境已經搭建好)進行android單元測試:
1.Application的測試:
新建一個android項目,在該android項目添加一個繼承Application的類,代碼如下:
- package com.phicomm.hu;
- import android.app.Application;
- public class FxAndroidApplication extends Application
- {
- @Override
- public void onCreate()
- {
- // TODO Auto-generated method stub
- super.onCreate();
- }
- @Override
- public void onTerminate()
- {
- // TODO Auto-generated method stub
- super.onTerminate();
- }
- public String getFavourite()
- {
- return "I Love Java";
- }
- }
Appication類創建好後,接着創建對應的測試工程:選中其所在的android工程---->鼠標右鍵----->new---->Android Test Project----->輸入測試工程名--->next----->選擇被測試的目標android工程(此處爲FxAndroidApplication所在的android工程)。這樣,一個測試工程就創建完成了。
通過eclipse創建自動生成的測試工程項目和android工程項目結構上沒什麼大的區別,主要是在AndroidManifest.xml中有變化,如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.phicomm.hu.test"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="10" />
- <instrumentation
- android:name="android.test.InstrumentationTestRunner"
- android:targetPackage="com.phicomm.hu" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <uses-library android:name="android.test.runner" />
- </application>
- </manifest>
在AndroidManifest.xml註冊了相關的測試環境(這些是android獨有的):<uses-library android:name="android.test.runner" />實現使用相關的運行測試類庫,<instrumentation
/>中的targetPackage爲被測試類所在的包。
接下來在測試工程中創建FxAndroidApplicationd的測試類,代碼如下:
- package com.phicomm.hu.test;
- import com.phicomm.hu.FxAndroidApplication;
- import android.app.Application;
- import android.test.ApplicationTestCase;
- public class FxApplicationTest extends ApplicationTestCase<FxAndroidApplication>
- {
- private FxAndroidApplication AppTest;
- public FxApplicationTest()
- {
- //調用父類構造函數,且構造函中傳遞的參數爲被測試的類
- super(FxAndroidApplication.class);
- }
- @Override
- protected void setUp() throws Exception
- {
- // TODO Auto-generated method stub
- super.setUp();
- //獲取application之前必須調用的方法
- createApplication();
- //獲取待測試的FxAndroidApplication
- AppTest = getApplication();
- }
- //測試FxAndroidApplication的getFavourite方法
- public void testGetFavourite()
- {
- /*驗證預測值"I Love C++"是否等於實際值,
- 由於實際值爲"I love Java",所以此處測試結果爲Failure*/
- assertEquals("I Love C++", AppTest.getFavourite());
- }
- }
測試類創建好後,就可以實現對FxAndroidApplicationd進行測試了。
測試方法:
啓動android模擬器(也可以通過android手機)----->運行android工程----->在測試工程中選中測試類FxApplicationTest---->鼠標右鍵--->Run As---->Android Junit Test。這樣,測試結果就可以在eclipse的Junit視圖上顯示了,如下圖:
通過上圖的測試結果可知,ApplicationTestCase測試類中有兩個測試方法是默認進行測試的(testGetFavourite纔是我們要測試的方法)。
當然,還可以通過adb進行測試:連接android手機------>打開電腦命令窗口(開始-->運行--->輸入cmd)---->在命令窗口輸入adb shell---->am instrument -w com.phicomm.hu.test(測試用例所在的包名)/android.test.InstrumentationTestRunner。
2.Activity的測試:
和上面application一樣,先創建一個android工程,該工程中創建了兩個activity,一個activity實現輸入用戶信息的登錄界面,另一個acticity顯示輸入的用戶信息。
效果圖如下:
登錄界面FxLoginActivity的代碼如下:
- package com.phicomm.hu;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class FxLoginActivity extends Activity
- {
- private EditText userName;
- private EditText passWord;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- userName = (EditText)findViewById(R.id.name);
- passWord = (EditText)findViewById(R.id.psd);
- Button login = (Button)findViewById(R.id.login);
- Button reset = (Button)findViewById(R.id.reset);
- //監聽登錄按鈕
- login.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v)
- {
- // TODO Auto-generated method stub
- Intent intent = new Intent(FxLoginActivity.this, FxResultActivity.class);
- //通過intent傳遞登錄信息到ResultActivity的界面中顯示
- intent.putExtra("userName", userName.getText().toString());
- intent.putExtra("passWord", passWord.getText().toString());
- //啓動ResultActivity顯示登錄界面信息
- startActivity(intent);
- }
- });
- //監聽重置按鈕
- reset.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- // TODO Auto-generated method stub
- resetInput();
- }
- });
- }
- public void resetInput()
- {
- userName.setText("");
- passWord.setText("");
- }
- }
main.xml佈局文件的代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <EditText
- android:id="@+id/name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="@string/name"/>
- <EditText
- android:id="@+id/psd"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="@string/psd"/>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- >
- <Button
- android:id="@+id/login"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="@string/login"/>
- <Button
- android:id="@+id/reset"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="@string/reset"/>
- </LinearLayout>
- </LinearLayout>
顯示用戶信息界面的FxResultActivity代碼如下:
- package com.phicomm.hu;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.EditText;
- import android.widget.TextView;
- public class FxResultActivity extends Activity
- {
- private static final String TAG = "ResultActivity";
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.result);
- TextView result = (TextView)findViewById(R.id.result);
- //通過得到intent獲取登錄界面傳來的信息
- Intent intent = getIntent();
- String userName = intent.getStringExtra("userName");
- String passWord = intent.getStringExtra("passWord");
- //將登錄信息在頁面中顯示
- result.setText("用戶名:" + userName + "\n" + "密碼:" + passWord);
- }
- }
以上的android工程創建好後,創建一個對應的測試工程:
測試工程中對應的FxLoginActivity類的測試代碼如下(詳細的代碼講解見代碼中的相關注釋,這裏不在累贅):
- package com.phicomm.hu.test;
- import android.app.Instrumentation;
- import android.test.ActivityInstrumentationTestCase2;
- import android.view.KeyEvent;
- import android.widget.Button;
- import android.widget.EditText;
- import com.phicomm.hu.FxLoginActivity;
- public class FxLoginActivityTest extends ActivityInstrumentationTestCase2<FxLoginActivity>
- {
- private Instrumentation mInstrumentation;
- private FxLoginActivity mLoginTest;
- private EditText userName;
- private EditText passWord;
- private Button login;
- private Button reset;
- public FxLoginActivityTest()
- {
- super(FxLoginActivity.class);
- }
- //重寫setUp方法,在該方法中進行相關的初始化操作
- @Override
- protected void setUp() throws Exception
- {
- // TODO Auto-generated method stub
- super.setUp();
- /**這個程序中需要輸入用戶信息和密碼,也就是說需要發送key事件,
- * 所以,必須在調用getActivity之前,調用下面的方法來關閉
- * touch模式,否則key事件會被忽略
- */
- //關閉touch模式
- setActivityInitialTouchMode(false);
- mInstrumentation = getInstrumentation();
- //獲取被測試的FxLoginActivity
- mLoginTest = getActivity();
- //獲取FxLoginActivity相關的UI組件
- userName = (EditText)mLoginTest.findViewById(com.phicomm.hu.R.id.name);
- passWord = (EditText)mLoginTest.findViewById(com.phicomm.hu.R.id.psd);
- login = (Button)mLoginTest.findViewById(com.phicomm.hu.R.id.login);
- reset = (Button)mLoginTest.findViewById(com.phicomm.hu.R.id.reset);
- }
- //該測試用例實現在測試其他用例之前,測試確保獲取的組件不爲空
- public void testPreConditions()
- {
- assertNotNull(mLoginTest);
- assertNotNull(userName);
- assertNotNull(passWord);
- assertNotNull(login);
- assertNotNull(reset);
- }
- /**該方法實現在登錄界面上輸入相關的登錄信息。由於UI組件的
- * 相關處理(如此處的請求聚焦)需要在UI線程上實現,
- * 所以需調用Activity的runOnUiThread方法實現。
- */
- public void input()
- {
- mLoginTest.runOnUiThread(new Runnable()
- {
- @Override
- public void run()
- {
- // TODO Auto-generated method stub
- userName.requestFocus();
- userName.performClick();
- }
- });
- /*由於測試用例在單獨的線程上執行,所以此處需要同步application,
- * 調用waitForIdleSync等待測試線程和UI線程同步,才能進行輸入操作。
- * waitForIdleSync和sendKeys不允許在UI線程裏運行
- */
- mInstrumentation.waitForIdleSync();
- //調用sendKeys方法,輸入用戶名
- sendKeys(KeyEvent.KEYCODE_P, KeyEvent.KEYCODE_H,
- KeyEvent.KEYCODE_I, KeyEvent.KEYCODE_C,
- KeyEvent.KEYCODE_O, KeyEvent.KEYCODE_M,
- KeyEvent.KEYCODE_M);
- mLoginTest.runOnUiThread(new Runnable()
- {
- @Override
- public void run()
- {
- // TODO Auto-generated method stub
- passWord.requestFocus();
- passWord.performClick();
- }
- });
- //調用sendKeys方法,輸入密碼
- sendKeys(KeyEvent.KEYCODE_1, KeyEvent.KEYCODE_2,
- KeyEvent.KEYCODE_3, KeyEvent.KEYCODE_4);
- }
- //測試輸入的用戶信息
- public void testInput()
- {
- //調用測試類的input方法,實現輸入用戶信息(sendKeys實現輸入)
- input();
- //測試驗證用戶信息的預期值是否等於實際值
- assertEquals("phicomm", userName.getText().toString());
- //密碼的預期值123與實際值1234不符,Failure;
- assertEquals("123", passWord.getText().toString());
- }
- //測試登錄按鈕
- public void testLogin()
- {
- input();
- //開新線程,並通過該線程在實現在UI線程上執行操作
- mInstrumentation.runOnMainSync(new Runnable()
- {
- @Override
- public void run()
- {
- // TODO Auto-generated method stub
- login.requestFocus();
- login.performClick();
- }
- });
- }
- //測試重置按鈕
- public void testReset()
- {
- input();
- mInstrumentation.runOnMainSync(new Runnable()
- {
- @Override
- public void run()
- {
- // TODO Auto-generated method stub
- reset.requestFocus();
- //點擊按鈕
- reset.performClick();
- }
- });
- //驗證重置按鈕的實現功能,是否點擊後內容爲空
- assertEquals("", userName.getText().toString());
- assertEquals("", passWord.getText().toString());
- }
- }
運行該測試類進行測試(選中---->Run As--->Android Junit Test),然後會自動啓動模擬器進行相關的輸入點擊測試。注:測試時可以發現,程序在測試到testLogin()方法登錄到另一個界面時,測試就停止了,也就是說testReset()沒測試到。所以,需要測試testReset()時可以先把testLogin()註釋掉,不然程序會測試到testLogin()後就不在對testReset()進行測試。
FxResultActivity的測試類代碼如下:
- package com.phicomm.hu.test;
- import android.content.Intent;
- import android.test.ActivityInstrumentationTestCase2;
- import android.widget.TextView;
- import com.phicomm.hu.FxResultActivity;
- public class FxResultActivityTest extends ActivityInstrumentationTestCase2<FxResultActivity>
- {
- private static final String LOGIN_INFO = "用戶名:feixun\n密碼:123";
- private FxResultActivity mResultActivity;
- private TextView result;
- public FxResultActivityTest()
- {
- super(FxResultActivity.class);
- }
- @Override
- protected void setUp() throws Exception
- {
- // TODO Auto-generated method stub
- super.setUp();
- //創建Intent,通過Intent傳遞用戶的登錄信息
- Intent intent = new Intent();
- intent.putExtra("userName", "feixun");
- intent.putExtra("passWord", "123");
- //通過攜帶用戶登錄信息的intent啓動FxResultActivity
- mResultActivity = launchActivityWithIntent("com.phicomm.hu",
- FxResultActivity.class, intent);
- //獲取UI組件
- result = (TextView)mResultActivity.findViewById(com.phicomm.hu.R.id.result);
- }
- //測試驗證用戶的登錄信息
- public void testLoginInfo()
- {
- //驗證預期值是否等於實際值
- assertEquals(LOGIN_INFO, result.getText().toString());
- }
- }
運行上面的測試類,結果正確