首页 > 其他 > 详细

Activity Test1

时间:2014-04-21 03:23:41      阅读:423      评论:0      收藏:0      [点我收藏+]

源码下载(免积分) 下载

Acitivty测试的API的父类是InstrumentationTestCase,这个类能够获取Instrumentation,来操作Activity。


对于activity测试,InstrumentationTestCase提供了下面三个功能:
  1. 生命周期的控制:使用Instrumentation,您能控制activity的生命周期

  2. Dependency injection:使用Instrumentation,你可以创建mock的系统对象,能帮助你控制测试环境

  3. UI交互:使用Instrumentation可以发送按键或者触屏事件

InstrumentationTestCase测试Activity的主要的子类:

  1. ActivityInstrumentationTestCase2 :这个类用于一个或者多个activity的功能测试

  2. ActivityUnitTestCase:用于测试隔离的单个activity

  3. SingleLaunchActivityTestCase:这个类用于测试单个activity,比较少用。

其他和Activity相关的类
        Mock 相关的类,ViewAsserts等。

  • ActivityInstrumentationTestCase2类的测试activity的功能

                功能:在SendActivity中定义Button用于启动ReceiveActivity,并把EditText中的数据传递给ReceiveActivity

                注意:如果使用的是真机时,首先要把锁屏解开,其次要把键盘调成英文输入,不然的话死的很惨的,^_^

1. 继承ActivityInstrumentationTestCase2
   public class SendeActivityFunTest extends
                  ActivityInstrumentationTestCase2<SendActivity> 
2.增加构造方法,这是junit要求的。
    /*为了测试的应用能够正确的实例化,必须设置这个构造函数
     * test runner(InstrumentationTestRunner或者其子类)会调用构造函数去实例化测试类。
     */
    public SendeActivityFunTest() {
        /*android能够利用SendActivity.class和AndroidManifest.xml
         *中的instrumentation标签中的android:targetPackage属性可以获取要测试的类。
         */
        super(SendActivity.class);
        // TODO Auto-generated constructor stub
    }
3. 增加setUp方法


    //setUp()方法在其他测试方法之前调用,主要是用于初始化变量和清楚以前的测试。
    protected void setUp() throws Exception {
        // TODO Auto-generated method stub
        super.setUp();
        //保存系统对象的引用
        mSendActivity = getActivity();
        sendButton = (Button) mSendActivity.findViewById(R.id.sendButton);
        mEditText = (EditText) mSendActivity.findViewById(R.id.editText);
    }
4. 添加测试是否正确初始化的方法


    //验证初始化是否正确
    public void testPreconditions()
    {
        assertNotNull(mSendActivity);
        assertNotNull(sendButton);
        assertNotNull(mEditText);
    }
5. 测试是否能够正确的启动ReceiveActivity,并获取正确的数据

    //测试是否能够引导ReceiveActivity,并传递的数据正确
    public void testSendMessageToReceiverActivity()
    {
        //创建ActivityMonitor去监听系统和ReceiverActivity的通信
        Instrumentation.ActivityMonitor activityMonitor = getInstrumentation()
                            .addMonitor(ReceiveActivity.class.getName(), null, false);

        /*在UI线程的测试要注意:
         * 创建一个Runnable的匿名内部类,并作为参数传递给runOnUiThread()
         * 
         */
        getInstrumentation().runOnMainSync(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                //请求获取焦点
                mEditText.requestFocus();
            }
        });

        //等待直到MainHandler中的事件都有被执行完
        getInstrumentation().waitForIdleSync();

        //发送字符串到EditText中
        getInstrumentation().sendStringSync(message);
        getInstrumentation().waitForIdleSync();


        /*TouchUtils:有很多触摸事件,这些方法可以用在InstrumentationTestCase
         *和ActivityInstrumentationTestCase2上去模拟用户触摸屏幕。
         */
        TouchUtils.clickView(this, sendButton);
        //Block until an Activity is created that matches this monitor
        ReceiveActivity receiveActivity = (ReceiveActivity) activityMonitor
                                        .waitForActivityWithTimeout(TIME_OUT);
        assertNotNull("ReceiverActivity is null", receiveActivity);
        assertEquals("monitor for ReceiverActivity has not been called", 
                                                1,activityMonitor.getHits());

        final TextView receivedMessage = (TextView) receiveActivity
                                                .findViewById(R.id.textView);
        assertNotNull(receivedMessage);
        assertEquals("wrong received message ", message,
                                        receivedMessage.getText().toString());
        //移除activityMonitor
        getInstrumentation().removeMonitor(activityMonitor);
    }

使用ActivityInstrumentationTestCase2通常的测试步骤是:

  1. 初始化的测试(此时setup方法中的变量)
  2. UI测试
  3. activity的状态管理的测试(主要用于测试生命周期)


参考的资料:

      http://developer.android.com/training/activity-testing/index.html

      http://developer.android.com/tools/testing/activity_testing.html

      http://developer.android.com/tools/testing/activity_test.html






Activity Test1,布布扣,bubuko.com

Activity Test1

原文:http://blog.csdn.net/wangfei199101/article/details/24136549

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!