Android 漫游之路------Android下junit单元测试、logCat的使用

来源:互联网 发布:安阳淘宝客服公司招聘 编辑:程序博客网 时间:2024/05/18 15:03

---------------------- 路漫漫其修远兮,吾将上下而求索。学无止境!---------------------- 

 

 

微笑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,实际结果是retValassertEquals(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>



微笑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;}}

运行测试后,对logCat中的log通过配置l过滤器进行过滤

 

---------------------- 路漫漫其修远兮,吾将上下而求索。学无止境!---------------------- 

我的博客:http://blog.csdn.net/helloxiaobi

 

原创粉丝点击