android基础知识12:android自动化测试06—Instrumentation 01 例子

 下面通过一个简单的例子来讲解Instrumentation的基本测试方法。在这个例子中我们会建立一个简单的android应用,同时在其上添加Instrumentation测试程序。

    1.首先建立一个android  project,其文件结构最终如下:


2、布局文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.hustophone.sample" android:versionCode="1"
	android:versionName="1.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<!--用于引入测试库-->
		<uses-library android:name="android.test.runner" />
		<activity android:name=".Sample" 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>
	<uses-sdk android:minSdkVersion="3" />
	<!--表示被测试的目标包与instrumentation的名称。-->
	<instrumentation android:targetPackage="com.hustophone.sample"
		android:name="android.test.InstrumentationTestRunner" />
</manifest>
3、被测程序 Sample类

package com.hustophone.sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; 

public class Sample extends Activity {

    private TextView myText = null;
    private Button button = null; 

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        myText = (TextView) findViewById(R.id.text1);

        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {
 

            @Override

            public void onClick(View arg0) {

                // TODO Auto-generated method stub

                myText.setText("Hello Android");

            }

        });

    } 

    public int add(int i, int j) {

        // TODO Auto-generated method stub

        return (i + j);

    }

}
        这个程序的功能比较简单,点击按钮之后,TextView的内容由Hello变为Hello Android.同时,在这个类中,我还写了一个简单的方法,没有被调用,仅供测试而已。
4、测试类SampleTest
      通常可以将测试程序作为另一个android应用程序。但是这里我们为了操作方便,写在了一个应用里面了。
package com.hustophone.sample.test;
import com.hustophone.sample.R;
import com.hustophone.sample.Sample;
import android.content.Intent;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView; 

public class SampleTest extends InstrumentationTestCase {

    private Sample sample = null;
    private Button button = null;
    private TextView text = null;

    /*

     * 初始设置
     *
     * @see junit.framework.TestCase#setUp()
     */

    @Override

    protected void setUp()  {

        try {
            super.setUp();
        } catch (Exception e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Intent intent = new Intent();
        intent.setClassName("com.hustophone.sample", Sample.class.getName());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        sample = (Sample) getInstrumentation().startActivitySync(intent);
        text = (TextView) sample.findViewById(R.id.text1);
        button = (Button) sample.findViewById(R.id.button1);

    }

    /*

     * 垃圾清理与资源回收

     *
     * @see android.test.InstrumentationTestCase#tearDown()

     */

    @Override

    protected void tearDown()  {

        sample.finish();

        try {

            super.tearDown();

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

 

    /*

     * 活动功能测试

     */

public void testActivity() throws Exception {

Log.v("testActivity", "test the Activity");

        SystemClock.sleep(1500);

        getInstrumentation().runOnMainSync(new PerformClick(button));

        SystemClock.sleep(3000);

        assertEquals("Hello Android", text.getText().toString());

    } 

    /*

     * 模拟按钮点击的接口

     */

    private class PerformClick implements Runnable {

        Button btn; 

        public PerformClick(Button button) {

            btn = button;

        } 

        public void run() {

            btn.performClick();

        }

    } 

    /*

     * 测试类中的方法

     */

    public void testAdd() throws Exception{

        String tag = "testAdd";

        Log.v(tag, "test the method");

        int test = sample.add(1, 1);

        assertEquals(2, test);

    } 

}
下面来简单讲解一下代码:
  setUp()和tearDown()都是受保护的方法,通过继承可以覆写这些方法。
  在android Developer中有如下的解释




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值