当前位置:操作系统 > 安卓/Android >>

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="http://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,
                 "http://172.20.230.5/iportal/samples/jsapi/mobile.html");
     }
 
    public void testCase3() {
         openSessionActivity();
 
        clickButton(R.id.btnLoad);
       
   &nbs

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,