ANDROID 单元测试
来源:互联网 发布:最佳适应算法例题 编辑:程序博客网 时间:2024/03/29 00:38
android.test.ActivityInstrumentationTestCase2
一、在Manifest.xml加入
<uses-library android:name="android.test.runner" /><uses-permission android:name="android.permission.RUN_INSTRUMENTATION" /><instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.travelsky.android.test"android:label="Test for my app"/>
二、编写单元测试代码:必须继承自AndroidTestCase类
package com.travelsky.android.test; import android.test.AndroidTestCase;import junit.framework.Assert; public class MyTest extends AndroidTestCase {private static final String Tag="MyTest"; public void testSave() throws Throwable{int i=4+8;Assert.assertEquals(5,i);} public void testSomethingElse() throws Throwable {Assert.assertTrue(1 + 1 == 12);} }run--> Android JUnit Test
运行后会,会出现如下警告:
Warning: No instrumentation runner found for the launch, using
android.test.InstrumentationTestRunner.
模拟器不能记住Androidmanifest的配置,在运行时需要重新设置运行配置,如下:
1.在工程名字上点击右键,选择properties
2.在Run/Debug setting中选择要运行的工程名字,点击右边的Edit,会进入下面的界面,
在 instrumentation runner后面的下拉列表中选择:android.test.InstrumentationTestRunner
3.重新运行该测试单元,则就不会出现上面的警告了。
还可以继承android.test.ActivityInstrumentationTestCase2<T>
添加构造函数
添加setUp()方法,这个方法在所有的测试之前进行变量和测试环境的初始化。
@Override protected void setUp() throws Exception { super.setUp(); mActivity = this.getActivity(); mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview); resourceString = mActivity.getString(com.example.helloandroid.R.string.hello); }添加testPreconditions()方法,检查初始化环境,只执行一次public void testPreconditions() { assertNotNull(mView); }添加单元测试public void testText() { assertEquals(resourceString,(String)mView.getText()); }
这时候会遇到这样一个问题, 运行的时候提示Test run failed: Test run incomplete. Expected 1 tests, received 0。
后来找到是构造函数的事,于是把泛型去掉, 然后用不带参数的构造函数,在构造函数里调用super(“com.travelsky.test”, ActivityName.class); 就Ok了。
REFERENCES:http://www.189works.com/article-19108-1.html
http://www.cnblogs.com/GnagWang/archive/2011/07/08/2101066.html
http://www.cnblogs.com/feisky/archive/2012/03/09/1783826.html
- android 单元测试
- Android单元测试
- Android单元测试
- android单元测试
- Android单元测试
- Android单元测试
- android单元测试
- android单元测试
- Android单元测试
- Android单元测试
- android 单元测试
- android 单元测试
- Android--单元测试
- android单元测试
- Android单元测试
- Android单元测试
- Android单元测试
- ANDROID 单元测试
- java.lang.IllegalStateException: Web app root system property already set to different value
- (C#)利用Aspose.Cells组件导入导出excel文件
- apache的用多个env作为或条件的配置
- 数组作为函数参数
- Prototype使用学习手册指南之base.js
- ANDROID 单元测试
- 从Web 2.0到推荐引擎2.0
- 二叉树相似性
- Intent组件
- 进程之基础知识(二)
- hibernate outer join属性浅析
- 字符串常量
- oracle
- C语言常用头文件