android的ActivityInstrumentationTestCase2测试

这个测试类是activity测试例子,可以实现单个activity的各种测试

Open Declaration android.test.ActivityInstrumentationTestCase2<T extends Activity>
This class provides functional testing of a single activity. The activity under test will be created using the system infrastructure (by calling InstrumentationTestCase.launchActivity()) and you will then be able to manipulate your Activity directly. 

这里可以知道,这个是单例测试activity,让你构建出一个完整的activity,以便于操作

这个类的setUp()这个方法是初始配置,called before a test

Sets up the fixture, for example, open a network connection. This method is called before a test is executed. 
下面是我的类ParcelableTestActivity,这是要测试的activity
package com.socket.test.parcelable;

import com.socket.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ParcelableTestActivity extends Activity implements OnClickListener {

    public static final String KEY = "key";
    public static final String TAG = "Parcelable";
    private EditText testEd;
    private Button mLoginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testEd = (EditText) findViewById(R.id.testEd);
        mLoginButton = (Button) findViewById(R.id.login);
        mLoginButton.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.login:
            login();
            break;

        }
    }

    /**
     * 登陆,没有实际的验证等操作,这里就是跳转到另一个界面
     */
    private void login() {
        Person mPerson = new Person();
        mPerson.setName("tom");
        mPerson.setAge(25);
        Intent mIntent = new Intent(this,
                ResultActivity.class);
        Bundle mBundle = new Bundle();
        mBundle.putParcelable(KEY, mPerson);
        mIntent.putExtras(mBundle);
        this.startActivity(mIntent);
    }

}

下面是真正的测试类ParcelableTest

package com.socket.test.parcelable;

import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.EditText;

public class ParcelableTest extends
        ActivityInstrumentationTestCase2<ParcelableTestActivity> {

    private ParcelableTestActivity parcelableTestActivity;
    private Instrumentation mInstrumentation;
    private EditText testEd;
    private Button loginButton;

    public ParcelableTest() {
        super(ParcelableTestActivity.class);
    }

    // 初始设置
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        //避免手动触摸的情况,关闭TouchMode
        setActivityInitialTouchMode(false);
        mInstrumentation = getInstrumentation();
        //获取被测的Activity
        parcelableTestActivity = getActivity();
        //获取这个活动界面的控件
        testEd = (EditText) parcelableTestActivity
                .findViewById(com.socket.R.id.testEd);
        loginButton = (Button) parcelableTestActivity
                .findViewById(com.socket.R.id.login);
    }

    // 测试:确保每个控件都不为空
    public void testPreCondition() {
        assertNotNull(parcelableTestActivity);
        assertNotNull(testEd);
        assertNotNull(loginButton);
    }

    public void input() {
        // 通过这个方式在UI上模拟点击的事件
        parcelableTestActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                testEd.requestFocus();
                testEd.performClick();
            }
        });
        // 等待被测试的应用程序的主线程 与 测试用例的线程 同步,大概是这个意思吧
        mInstrumentation.waitForIdleSync();
        // 模拟输入 测试数据的键值
        sendKeys(KeyEvent.KEYCODE_T, KeyEvent.KEYCODE_E, KeyEvent.KEYCODE_S,
                KeyEvent.KEYCODE_T, KeyEvent.KEYCODE_N, KeyEvent.KEYCODE_A,
                KeyEvent.KEYCODE_M, KeyEvent.KEYCODE_E);        
    }

    public void testInput() {
        input();
        // 测试输入的值与预测的内容是否一致
        assertEquals("testname", testEd.getText().toString());  
    }

    /**
     * 测试登陆的功能
     */
    public void testLogin() {
        input();
        mInstrumentation.runOnMainSync(new Runnable() {
            @Override
            public void run() {
                loginButton.requestFocus();
                loginButton.performClick();
            }
        });
    }
}

自己去搭建工程并测试测试吧,挺有趣的测试类,这是测试类
链接: https://pan.baidu.com/s/1skUkJZB 密码: kb94

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值