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

Android JUnit测试说明和实例演示

什么是 JUnit ?
JUnit是采用测试驱动开发的方式,也就是说在开发前先写好测试代码,主要用来说明被测试的代码会被如何使用,错误处理等;然后开始写代码,并在测试代码中逐步测试这些代码,直到最后在测试代码中完全通过。

现简要说JUnit的4大功能
管理测试用例。修改了哪些代码,这些代码的修改会对哪些部分有影响,通过JUnit将这次的修改做个完整测试。这也就JUnit中所谓的TestSuite。
定义测试代码。这也就是JUnit中所谓的TestCase,根据源代码的测试需要定义每个TestCase,并将TestCase添加到相应的TestSuite方便管理。
定义测试环境。在TestCase测试前会先调用“环境”配置,在测试中使用,当然也可以在测试用例中直接定义测试环境。
检测测试结果。对于每种正常、异常情况下的测试,运行结果是什么、结果是否是我们预期的等都需要有个明确的定义,JUnit在这方面提供了强大的功能。
以上部分与我们平常使用IDE调试的过程是完全一样的,只不过是增加了测试用例管理、测试结果检测等功能,提高了单元的效率,保证了单元测试的完整性,明确了单元测试的目标。

 

 

一个 JUnit 测试包含以下元素:

开发代码部分 测试代码部分 测试工具部分
待测试类 A 通过扩展 TestCase 或者构造 TestSuit 方法
编写测试类 B 一个测试运行器(TestRunner)R,可以选择图形界面或文本界面
 


操作步骤:

将 B 通过命令行方式或图形界面选择方式传递给 R,R 自动运行测试,并显示结果。

首先看下junit测试类库和android中单元测试类库:
 

SDK 功能说明
junit.framework JUnit测试框架
junit.runner 实用工具类支持JUnit测试框架
android.test Android 对JUnit测试框架的扩展包
android.test.mock Android的一些辅助类
android.test.suitebuilder 实用工具类,支持类的测试运行

 

 

junit.framework中的方法解释:


TestSuit:TestSuite是测试用例的集合;
TestCase:定义运行多个测试用例;
TestResult:收集一个测试案例的结果,测试结果分为失败和错误,如果未能预计的断言就是失败,错误就像一个ArrayIndexOutOfBoundsException异常而导致的无法预料的问题;
TestFailure:测试失败时捕获的异常;
Assert:断言的方法集,当断言失败时显示信息;

 

下面给出一个实例:

AndroidManifest.xml


[html]
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.android.junit" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="8" /> 
 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
        <activity android:name="com.android.junittest.MyJUnitActivity" 
                  android:label="@string/app_name"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
        <!-- Android JUnit配置 --> 
        <uses-library android:name="android.test.runner" /> 
    </application> 
    <!-- 注:targetPackage与上面mainfest的package相同即可 --> 
    <instrumentation android:targetPackage="com.android.junit"  
            android:name="android.test.InstrumentationTestRunner"  
            android:label="Android JUnit测试" /> 
</manifest> 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.junit"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="com.android.junittest.MyJUnitActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- Android JUnit配置 -->
  <uses-library android:name="android.test.runner" />
    </application>
 <!-- 注:targetPackage与上面mainfest的package相同即可 -->
 <instrumentation android:targetPackage="com.android.junit"
   android:name="android.test.InstrumentationTestRunner"
   android:label="Android JUnit测试" />
</manifest>
Activity类,只是给出一个界面,没有实际的用途


[java]
package com.android.junit; 
 
import com.android.junit.R; 
 
import android.app.Activity; 
import android.os.Bundle; 
 
public class MyJUnitActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 

package com.android.junit;

import com.android.junit.R;

import android.app.Activity;
import android.os.Bundle;

public class MyJUnitActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
需要测试的类


[java]
package com.android.junit; 
 
 
public class Apps { 
    public void methodA() { 
        System.out.println("---Hello!"); 
 

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