Android编程之单元测试实例分析

所属分类: 软件编程 / Android 阅读数: 1388
收藏 0 赞 0 分享

本文实例讲述了Android编程之单元测试用法。分享给大家供大家参考,具体如下:

在实际开发中,开发android软件的过程需要不断地进行测试。使用Junint测试框架,是正规Android开发的必用技术,在Junint中可以得到组件,可以模拟发送事件和检测程序处理的正确性。单元测试是嵌入到项目中;也可以作为一个单独的项目争对某个具体项目进行测试。

第一步:首先在AndroidManifest.xml中加入下面红色代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lee0000.test" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="android.test.runner"/>
</application>
<use-sdk android:minSdkVersion="6"/>
<instrumentation android:name="android.test.instrumentationTestRunner" android:targetPackage="com.lee0000.test" android:label="Tests"/> 
 ***上面targetPackage指定的包要和应用的package相同。

第二步:编写单元测试代码,一般对将要测试的方法命名testXXX。需要测试的时候选择大纲(Outline视图)选择测试的方法右键点击,选择"Run As" - "Android Junit Test"。

例:

项目结构:

AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.lee0000.test"
 android:versionCode="1"
 android:versionName="1.0" >
 <uses-sdk android:minSdkVersion="15" />
 <application
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name" >
  <activity
   android:name=".JUintTestActivity"
   android:label="@string/app_name" >
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
 <uses-library android:name="android.test.runner" />  
 </application>
 <instrumentation 
  android:name="android.test.InstrumentationTestRunner"
  android:targetPackage="com.lee0000.test" android:label="Tests"/> 
</manifest> 

定义测试的两个方法:

public class testclass {
 public void str(String s){
  System.out.println(s.substring(6));
 }
 public int add(int a,int b){
  return a+b;
 }
} 

一般继承的是AndroidTestCase,测试的时候就是测试这两个方法,如果在对应方法中选择"Run As" - "Android Junit Test"时出错,可以右键Test类,选择"Run as" - "Run Configurations",在 Instrumentation runner中选择:

import junit.framework.Assert;
import android.test.AndroidTestCase;
public class Test extends AndroidTestCase{
 public void teststr() throws Exception{
  testclass tc = new testclass();
  tc.str("null");
 }
 public void testadd(){
  testclass tc = new testclass();
  int t = tc.add(1, 2);
  Assert.assertEquals(3, t);
 }
} 

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多