InstrumentationTestCase类的使用

来源:互联网 发布:表格数据分析表 编辑:程序博客网 时间:2024/06/15 23:31

本文参考:http://blog.csdn.net/xianming01/article/details/7893391

InstrumentationTestCase类封装了Junit类,是Android的测试类。下面以一个实例讲解它的使用:

该实例是用来测试Webkit的加载的,测试的是WebView的loadUrl()方法:


WebkitLoad.java

package com.android.webkit;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.webkit.WebView;import android.widget.Button;public class WebkitLoad extends Activity {    /** Called when the activity is first created. */private Button btn1, btn2;private WebView webview;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        btn1 = (Button)findViewById(R.id.btn1);        btn2 = (Button)findViewById(R.id.btn2);        webview = (WebView)findViewById(R.id.webView1);                btn1.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubwebview.loadUrl("file:///android_asset/3.htm");}});                btn2.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubwebview.loadUrl("file:///android_asset/1.html");}});    }}


main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    /><Button android:text="Button" android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><Button android:text="Button" android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent"></WebView></LinearLayout>

以上是被测试对象的代码,下面是测试的代码:

(1)改写AndroidManifest.xml文件:

主要是添加“<uses-library android:name="android.test.runner" />” 和 “<instrumentation android:targetPackage="com.android.webkit" android:name="android.test.InstrumentationTestRunner" />”

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.android.webkit"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="1" />    <application android:icon="@drawable/icon" android:label="@string/app_name">    <uses-library android:name="android.test.runner" />              <activity android:name=".WebkitLoad"                  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>    <instrumentation android:targetPackage="com.android.webkit"          android:name="android.test.InstrumentationTestRunner" />      </manifest>


(2)在src目录下添加目录com.anroid.webkit.test,

WebkitLoadTest.java

package com.android.webkit.test;import com.android.webkit.R;import com.android.webkit.WebkitLoad;import android.content.Intent;import android.os.SystemClock;import android.test.InstrumentationTestCase;import android.widget.Button;public class WebkitLoadTest extends InstrumentationTestCase {private Button btn1, btn2;private WebkitLoad webkitload;@Overrideprotected void tearDown() throws Exception {// TODO Auto-generated method stubwebkitload.finish();super.tearDown();}@Overrideprotected void setUp() throws Exception {// TODO Auto-generated method stubsuper.setUp();Intent intent = new Intent();intent.setClassName("com.android.webkit", WebkitLoad.class.getName());intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);webkitload = (WebkitLoad)getInstrumentation().startActivitySync(intent);btn1 = (Button)webkitload.findViewById(R.id.btn1);btn2 = (Button)webkitload.findViewById(R.id.btn2);}public void testActivity() throws Exception{SystemClock.sleep(2000);for(int i = 0; i < 10; i++){getInstrumentation().runOnMainSync(new PerformClick(btn1));SystemClock.sleep(2000);getInstrumentation().runOnMainSync(new PerformClick(btn2));SystemClock.sleep(2000);}}}

PerformClick.java

package com.android.webkit.test;import android.widget.Button;public class PerformClick implements Runnable {Button btn;public PerformClick(Button button){btn = button;}@Overridepublic void run() {// TODO Auto-generated method stubbtn.performClick();}}