android 简单的单元测试

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

如下:
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()............

测试结果如下图:

 

posted @   itank  阅读(863)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示