Activity Test1
来源:互联网 发布:vmware12 mac os补丁 编辑:程序博客网 时间:2024/06/03 16:13
源码下载(免积分) :下载
Acitivty测试的API的父类是InstrumentationTestCase,这个类能够获取Instrumentation,来操作Activity。
对于activity测试,InstrumentationTestCase提供了下面三个功能:
生命周期的控制:使用Instrumentation,您能控制activity的生命周期
Dependency injection:使用Instrumentation,你可以创建mock的系统对象,能帮助你控制测试环境
UI交互:使用Instrumentation可以发送按键或者触屏事件
InstrumentationTestCase测试Activity的主要的子类:
ActivityInstrumentationTestCase2 :这个类用于一个或者多个activity的功能测试
ActivityUnitTestCase:用于测试隔离的单个activity
SingleLaunchActivityTestCase:这个类用于测试单个activity,比较少用。
其他和Activity相关的类
Mock 相关的类,ViewAsserts等。
- ActivityInstrumentationTestCase2类的测试activity的功能
功能:在SendActivity中定义Button用于启动ReceiveActivity,并把EditText中的数据传递给ReceiveActivity
注意:如果使用的是真机时,首先要把锁屏解开,其次要把键盘调成英文输入,不然的话死的很惨的,^_^
1. 继承ActivityInstrumentationTestCase2
public class SendeActivityFunTest extendsActivityInstrumentationTestCase2<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通常的测试步骤是:
- 初始化的测试(此时setup方法中的变量)
- UI测试
- 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
- test1
- test1
- test1
- test1
- test1
- test1
- TEST1
- test1
- test1
- TEST1
- Test1
- test1
- Test1
- Test1
- Test1
- test1
- test1
- 发票语音查询系统的状态图
- 带权并查集 La 3027
- Best Practices for Speeding Up Your Web Site
- Ubuntu 14.04 编译newLISP 10.6.0
- 黑马程序员-HashSet讲解学习日记
- Activity Test1
- android.annotation.SuppressLint 报错分析
- 英国部份城市中英文对照
- UVa 10161 棋盘上的蚂蚁
- ACM_3:在一个字符串中找到第一个只出现一次的字符
- c++第三章 类域、友元、运算符重载
- vi学习笔记
- 表单设计改善用户体验
- Makefile写作的一种模板