Activity Test2
来源:互联网 发布:中国城市癌症最新数据 编辑:程序博客网 时间:2024/06/10 02:17
资源下载(免积分):下载
-
ActivityUnitTestCase的使用
activity的单元测试能够很好的验证activity的状态,和其他组件的交互(在孤立的情况下)。
一个单元测试通常
时最小的代码单元
(
方法,类,组件)但不适合与系统复杂的UI交互,在这种情况下,
你应该使用 ActivityInstrumentationTestCase2
。
在测试activity前,你可以注入mock Context和Appleication,可以孤立的运行activity测试,可以测试方法。
注意: ActivityUnitTestCase和ActivityInstrumentationTestCase2不同,android Instrumentation不会自
动启动Activity,必须调用startActivity()方法来启动Activity。
测试功能:在SendActivity中,当点击button时,会触发启动ReceiveActivity的Intent,下面就是测试
intent的信息是否正确
1. 继承ActivityUnitTestCase类
/* * ActivityUnitTestCase和ActivityInstrumentationTestCase2不同, * android Instrumentation不会自动启动Activity, * 必须调用startActivity()方法来启动Activity。 */public class SendActivityUnitTest extends ActivityUnitTestCase<SendActivity>
2. 添加构造函数,这是必须的
//用于初始化测试用例, public SendActivityUnitTest() { /* * android能够利用SendActivity.class和AndroidManifest.xml * 中的instrumentation标签中的android:targetPackage属性可以获取要测试的类。 */ super(SendActivity.class); }
3. 添加setUp()方法,这个方法在每个测试方法之前调用
/* * 在每个测试方法之前调用,用于清除以前测试或者变量的初始化 *(在这里不能获取系统对象的引用,所以处理构造Intent,其他就没啥作用了) */ @Override protected void setUp() throws Exception { // TODO Auto-generated method stub super.setUp(); launchIntent = new Intent(getInstrumentation() .getTargetContext(), SendActivity.class); }
4. 简单测试初始化是否正确,必须在其他测试方法之前
//预先测试,在正式测试前的简单测试^_^ public void testPreconditions() { //启动activity startActivity(launchIntent,null,null); Button button =(Button) getActivity() .findViewById(R.id.sendButton); EditText editText = (EditText) getActivity() .findViewById(R.id.editText); /* *UI 测试看布局文件中是否包含button,editText, *ViewAsserts提供了一系列的方法用于测试UI */ ViewAsserts.assertOnScreen(getActivity() .findViewById(R.id.sendLayout), button); ViewAsserts.assertOnScreen(getActivity().findViewById(R.id.sendLayout) , editText); }
5. 测试Intent的信息是否正确
public void testLaunchReceiveActivityIntent() { //启动activity startActivity(launchIntent, null,null); //设置editText的内容 EditText editText = (EditText) getActivity().findViewById(R.id.editText); editText.setText(message); Button button =(Button) getActivity().findViewById(R.id.sendButton); //Call this view's OnClickListene button.performClick(); //获取启动ReceiveActivity的intent final Intent intent = getStartedActivityIntent(); assertNotNull("Intent is null", intent); final String data = intent.getStringExtra(SendActivity.EXTRA); //验证启动的是不是ReceiveActivity String launchActivityName = intent.getAction(); assertEquals("data don't matches",message, data); }
参考的资料:
http://developer.android.com/training/activity-testing/index.html
http://developer.android.com/tools/testing/activity_testing.html
0 0
- Activity Test2
- Test2
- test2
- test2
- Test2
- test2
- Test2
- test2
- test2
- test2
- test2
- TEST2
- TEST2
- test2
- test2
- test2
- test2
- test2
- 45 如何在Linux下查看和切换shell
- 重振旗鼓
- error LNK2019: 无法解析的外部符号
- Linux 在Eclipse中设置NDK编译
- eclipse 中node.js环境搭建
- Activity Test2
- 完全二叉树学习
- 字符hash + hash_table
- 信宇科技工作室
- spring ContextLoaderListener的作用
- UVa: 10010 - Where's Waldorf?
- UTF-8简史
- jdbc笔记(二)事务
- 四封短信【非专业、算法与应用】