qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

Android下junit单元测试、logCat的使用

 Android下junit单元测试
  软件测试小知识:
  根据测试是否知道源代码:
  黑盒测试:只关心程序执行的过程和结果
  白盒测试:根据源代码写测试方法或者测试用例
  根据测试的粒度:
  方法测试:function test
  单元测试:unit test
  集成测试:intergration test
  根据测试的次数:
  冒烟测试:smoke test(android 猴子)
  压力测试:prssure test
  Android单元测试:
  1.Android测试类要继承AndroidTestCase类
  2.写测试方法,在测试方法内使用断言assert来测试要测试的方法
  3.在AndroidManifest.xml中,要设置
  <instrumentation
  android:name="android.test.InstrumentationTestRunner"
  android:targetPackage="com.lee.test" />
  和<uses-library android:name="android.test.runner" >
  4.确保adb连接正常。
  MyService.java

package com.lee.test.service;

public class MyService {

 /**
  * 计算器相加的业务
  * @param a
  * @param b
  * @return
  */
 public int add(int a,int b){
  return a+b;
 }
}



 TestMyService.java
package com.lee.test.service.test;
import com.lee.test.service.MyService;
import android.test.AndroidTestCase;
public class TestMyService extends AndroidTestCase {
/**
* add方法的测试代码
* 把异常抛给测试框架
* @throws Exception
*/
public void testAdd()throws Exception{
MyService myService = new MyService();
int retVal = myService.add(3, 5);
//断言,预期结果是8,实际结果是retVal
assertEquals(8, retVal);
}
}
package com.lee.test.service.test;
import com.lee.test.service.MyService;
import android.test.AndroidTestCase;
public class TestMyService extends AndroidTestCase {
/**
* add方法的测试代码
* 把异常抛给测试框架
* @throws Exception
*/
public void testAdd()throws Exception{
MyService myService = new MyService();
int retVal = myService.add(3, 5);
//断言,预期结果是8,实际结果是retVal
assertEquals(8, retVal);
}
}

  AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lee.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- 这里是进行单元测试必须要添加,指令集必须在manifest节点下 -->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.lee.test" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.lee.test.MainActivity"
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节点下,使用函数库 -->
<uses-library android:name="android.test.runner" >
</uses-library>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lee.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- 这里是进行单元测试必须要添加,指令集必须在manifest节点下 -->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.lee.test" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.lee.test.MainActivity"
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节点下,使用函数库 -->
<uses-library android:name="android.test.runner" >
</uses-library>
</application>
</manifest>


  logCat的使用
  日志信息是分等级的:
  verbose:提醒  黑色
  debug:调试  蓝色
  info:信息  绿色
  warn:警告  橙色
  error:错误  红色
  使用上面的程序,可以做一个demo
  修改MyService.java

package com.lee.test.service;
import android.util.Log;
public class MyService {
private static final String TAG = "MyService";
/**
* 计算器相加的业务
* @param a
* @param b
* @return
*/
public int add(int a,int b){
Log.v(TAG, ""+a);
Log.d(TAG, ""+b);
Log.i(TAG, ""+b);
Log.w(TAG, ""+a);
Log.e(TAG, ""+b);
System.out.println("System.out的log");
System.err.println("System.err的log");
return a+b;
}
}
package com.lee.test.service;
import android.util.Log;
public class MyService {
private static final String TAG = "MyService";
/**
* 计算器相加的业务
* @param a
* @param b
* @return
*/
public int add(int a,int b){
Log.v(TAG, ""+a);
Log.d(TAG, ""+b);
Log.i(TAG, ""+b);
Log.w(TAG, ""+a);
Log.e(TAG, ""+b);
System.out.println("System.out的log");
System.err.println("System.err的log");
return a+b;
}
}


posted on 2013-10-14 10:16 顺其自然EVO 阅读(758) 评论(0)  编辑  收藏 所属分类: android


只有注册用户登录后才能发表评论。


网站导航:
 
<2013年10月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜