本文共 2498 字,大约阅读时间需要 8 分钟。
原文地址:
功能性测试包括模拟用户操作之类的组件验证。例如开发者可以通过功能性测试来验证在用户执行了UI操作之后Activity是否启动了Activity。
如要为Activity创建功能性测试,测试类应当继承。与ActivityUnitTestCase不同,既可以与Android系统通信,又能使程序可以接收键盘输入事件与屏幕点击事件。
一般功能性测试可能会有以下测试目的:
开发者所实现的代码可能如下:
@MediumTestpublic void testSendMessageToReceiverActivity() { final Button sendToReceiverButton = (Button) mSenderActivity.findViewById(R.id.send_message_button); final EditText senderMessageEditText = (EditText) mSenderActivity.findViewById(R.id.message_input_edit_text); // Set up an ActivityMonitor ... // Send string input value ... // Validate that ReceiverActivity is started ... // Validate that ReceiverActivity has the correct data ... // Remove the ActivityMonitor ...}
测试框架会等待ReceiverActivity启动,否则的话将会在超时后返回null。如果ReceiverActivity启动,那么则会收到一个命中。开发者可以通过断言方法来验证ReceiverActivity是否被启动,命中数是否会如所期望的那样有所增长。
如果需要监视Activity,可以注册。当目标Activity启动时,系统会通知一个事件。如果目标Activity启动,那么ActivityMonitor的计数器则会更新。
一般使用应当执行以下步骤:
例如:
// Set up an ActivityMonitorActivityMonitor receiverActivityMonitor = getInstrumentation().addMonitor(ReceiverActivity.class.getName(), null, false);// Validate that ReceiverActivity is startedTouchUtils.clickView(this, sendToReceiverButton);ReceiverActivity receiverActivity = (ReceiverActivity) receiverActivityMonitor.waitForActivityWithTimeout(TIMEOUT_IN_MS);assertNotNull("ReceiverActivity is null", receiverActivity);assertEquals("Monitor for ReceiverActivity has not been called", 1, receiverActivityMonitor.getHits());assertEquals("Activity is of wrong type", ReceiverActivity.class, receiverActivity.getClass());// Remove the ActivityMonitorgetInstrumentation().removeMonitor(receiverActivityMonitor);
如果Activity含有EditText,可能需要测试用户是否可以对其输入数据。
一般来说,要发送字符串到EditText,应当:
例如:
// Send string input valuegetInstrumentation().runOnMainSync(new Runnable() { @Override public void run() { senderMessageEditText.requestFocus(); }});getInstrumentation().waitForIdleSync();getInstrumentation().sendStringSync("Hello Android!");getInstrumentation().waitForIdleSync();
转载地址:http://rksxa.baihongyu.com/