android jUnit test 進行自動化測試

一. 被test的工程:
 
新建一個android工程:D_session;它有一個activity:D_sessionActivity;package名:com.mysession
 
 
二.測試工程:
 
新建一個測試工程:D_sessionTest, 類型是android test project;
 
1. menifest文件:
 
<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="https://schemas.android.com/apk/res/android"
     package="com.mysession.test"
     android:versionCode="1"
     android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="8" />
 
    <instrumentation
         android:name="android.test.InstrumentationTestRunner"
         android:targetPackage="com.mysession" />
 
    <application
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name" >
         <uses-library android:name="android.test.runner" />
     </application>
 
</manifest>
 2. 一個base activity 來定義各種模擬測試者的動作和判斷測試結果,各個測試類都繼承該類:
 
package com.mysession.test;
 
import android.app.Activity;
 import android.app.Instrumentation;
 import android.app.Instrumentation.ActivityMonitor;
 import android.content.Intent;
 import android.test.InstrumentationTestCase;
 import android.test.TouchUtils;
 import android.widget.Button;
 import android.widget.TextView;
 
import com.mysession.D_sessionActivity;
 
public class SessionActivityTest extends InstrumentationTestCase {
 
    private Instrumentation mInstrumentation;
     private ActivityMonitor mSessionMonitor;
     private Activity mCurrentActivity, mSessionActivity;
 
    private String TextNotEqual = "text not equal.";
     private static final String PackageName = "com.mysession";
 
    @Override
     protected void setUp() throws Exception {
 
// 初始化
         super.setUp();
 
        if (mInstrumentation == null) {
             mInstrumentation = getInstrumentation();
         }
 
        mSessionActivity = null;
     }
 
    @Override
     protected void tearDown() throws Exception {
         super.tearDown();
       
 //釋放資源
         closeActivity(mSessionActivity);
         mCurrentActivity = null;
     }
 
    private void closeActivity(Activity activity) {
         if(activity != null){
             activity.finish();
             activity = null;
         }
       
     }
 
    public void openSessionActivity() {
 
// 打開session activity
         try {
             setUp();
         } catch (Exception e) {
             e.printStackTrace();
         }
 
        mSessionMonitor = mInstrumentation.addMonitor(
                 D_sessionActivity.class.getName(), null, false);
 
        Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         intent.setClassName(PackageName, D_sessionActivity.class.getName());
         mInstrumentation.startActivitySync(intent);
 
        mSessionActivity = getInstrumentation().waitForMonitor(mSessionMonitor);
         assertNotNull(mSessionActivity);
         mCurrentActivity = mSessionActivity;
     }
 //判斷text是否正確
     public void assertTextEqual(int resId, String strText) {
         TextView textView = (TextView) mCurrentActivity.findViewById(resId);
         assertNotNull(textView);
 
        assertEquals(TextNotEqual,
                 strText,
                 textView.getText().toString());
     };
   
 // 模擬按鈕點擊事件
     public void clickButton(int resId){
         Button button = (Button) mCurrentActivity.findViewById(resId);
         assertNotNull(button);
       
         TouchUtils.clickView(this, button);
     }
 }
 
3. 各個測試類:
 
測試類一:
 package com.mysession.test.cases;
 
import com.mysession.R;
 import com.mysession.test.SessionActivityTest;
 
public class MyCase1 extends SessionActivityTest {
 
    public void testCase1() {
         openSessionActivity();
 
        assertTextEqual(R.id.etUrl,
                 "https://172.20.230.5/iportal/samples/jsapi/mobile.html");
     }
 
    public void testCase3() {
         openSessionActivity();
 
        clickButton(R.id.btnLoad);
       
         clickButton(R.id.btnHistory);
     }
 }
 
測試類二:
 
package com.mysession.test.cases;
 
import com.mysession.R;
 import com.mysession.test.SessionActivityTest;
 
public class MyCase2 extends SessionActivityTest{
 
    public void testCase2() {
         openSessionActivity();
       
         clickButton(R.id.btnLoad);
     }
 
}
 
三. 有些動作(如點擊menu)需要通過包robotium-solo-1.8.0.jar來完成。
 
所以要在Build Path->Configure Build Path…中導入:robotium-solo-1.8.0.jar
 程序中:
 
    private Solo solo;
 
    solo = new Solo(getInstrumentation(),getActivity);
 
 
就可以使用solo瞭, 如:
 
        solo.clickOnMenuItem(text);
 
        solo.goBack();
 
 
摘自fhy_2008的專欄

發佈留言