当前位置:操作系统 > 安卓/Android >>

android学习笔记5--------业务bean(单元测试) .

android的单元测试非常好用,它可以检测你的功能类或方法是否正确,而不依赖于一些复杂的操作。
单元测试配置:

1.单元测试类继承AndroidTestCase

2.AndroidManifest.xml文件添加

 <uses-library android:name="android.test.runner"></uses-library> 

<instrumentation
     android:name="android.test.InstrumentationTestRunner"
     android:targetPackage="com.luku.log"//你的包名
     android:label="Test for my app"
    ></instrumentation>

3.测试类编写

例子:

[java]
import android.test.AndroidTestCase; 
import android.util.Log; 
 
public class test extends AndroidTestCase 

    public void add() throws Exception 
    { 
        int i =1+1; 
        xxx(); 
        Log.i("test", "结果是="+i); 
    } 
     
    public String xxx() 
    { 
        String str="输出"; 
         
        int i=1; 
        int x=2; 
        int c=i+x; 
        str=str+c; 
        return str; 
    } 

import android.test.AndroidTestCase;
import android.util.Log;

public class test extends AndroidTestCase
{
 public void add() throws Exception
 {
  int i =1+1;
  xxx();
  Log.i("test", "结果是="+i);
 }
 
 public String xxx()
 {
  String str="输出";
  
  int i=1;
  int x=2;
  int c=i+x;
  str=str+c;
  return str;
 }
}
 

 

[java]
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.luku.log" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="8" /> 
     
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
       <uses-library android:name="android.test.runner"></uses-library> 
        <activity android:name=".LogActivity" 
                  android:label="@string/app_name"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
 
    </application> 
      
    <instrumentation  
        android:name="android.test.InstrumentationTestRunner" 
        android:targetPackage="com.luku.log" 
        android:label="Test for my app" 
    ></instrumentation> 
</manifest> \

摘自 奔跑的蜗牛


补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,