现在的位置: 首页 > 综合 > 正文

android 简单的单元测试

2012年02月13日 ⁄ 综合 ⁄ 共 1520字 ⁄ 字号 评论关闭

最近累积了好多篇文章还没发出来,今天很晚了,就写到这里吧。做下要发的文章笔记免得忘记了,呵呵 人老了啊!

如下:
google 定位,加标注!
sqlite数据库操作,存储数据的总结(sqlite,bundel,sharedPreferences)

二维码的编码解码,zxing 使用全解析

................

好了言归正传,这里我就简单讲讲单元测试!

 

一,新建测试工程:
新建android test project项目
在Test Target中选择你要测试的工程
二,新建测试类
    继承activityInsrumentationTestCase2<HelloWorld>
    <HelloWorld>是你要测试项目中的activity
三,写测试代码
    方法以test开头,如:testAbc,testTank  请注意命名规范

贴代码:

 

代码

package com.example.android.test;

import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;

import com.example.android.HelloWorld;

publicclass HelloTest extends ActivityInstrumentationTestCase2<HelloWorld> {

private HelloWorld mActivity; // the activity under test
private TextView mView; // the activity's TextView (the only view)
private String resourceString;

public HelloTest() {
super("com.example.android", HelloWorld.class);//实例化单元测试时传入要测试的包,类

// TODO Auto-generated constructor stub
}

@Override
protectedvoid setUp() throws Exception {
super.setUp();
mActivity
=this.getActivity();//得到你在构造函数传的类的activity实例
mView = (TextView) mActivity
.findViewById(com.example.android.R.id.txt);
//得到HelloWorld项目中界面view的对象
resourceString = mActivity
.getString(com.example.android.R.string.hello);
//resourceString="tank";
}
publicvoid testText() {
assertEquals(resourceString, (String) mView.getText());
//textView的值 是不是预期的值(应该是,实际是)
}
publicvoid testPreconditions() {
assertNotNull(mView);
//判断是否为null
}

}

Assert类封装了很多的方法提供我们测试使用,你可以查看源码,清关注我前几篇文章有讲到在MyEclipse中查看源码的方法http://www.cnblogs.com/tankaixiong/archive/2010/10/20/1856899.html
 方法列举: assertTrue(),assertFalse(),assertSame(),assertNull()............

测试结果如下图:

 

抱歉!评论已关闭.