Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1066505
  • 博文数量: 226
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 2504
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-21 14:12
文章分类

全部博文(226)

文章存档

2011年(1)

2010年(2)

2009年(68)

2008年(4)

2007年(27)

2006年(124)

我的朋友

分类: LINUX

2009-04-27 17:39:34

如果想在android里面做单元测试,有以下三种方法可行。

第一,  就是java程序员最为熟悉和常用的JUnit, 但是由于目前android sdk (version 1.1)中只是提供了stubbed methods/classes,没有具体的实现代码,所以如果用JUnit的话,我们需要在运行单元测试时,一定要 用JDK来运行,利用java命令来启动JUnit的某个Runner。如果是用Eclipse的话,可以在Run Configuration里新建一个JUnit。但是一定要记得在Classpath选项卡里将Bootstrap Entries中的Android Library改成JRE,并且添加junit.jar。具体的设置可以参考:http://developer.android.com/guide/appendix/faq/troubleshooting.html#addjunit 而且,更为遗憾的是,这种方法运行的JUnit运行在JDK之上的,而不是android,所以,只能测试一些和android无关的东西,比如业务逻辑,数据封装,数值计算等等。并不能测试android api

第二,  采用Instrumentation. Android单元测试的主入口是InstrumentationTestRunner。它相当于JUnit当中TestRunner的作用。你可以将Instrumentation理解为一种没有图形界面的,具有启动能力的,用于监控其他类(用Target Package声明)的工具类。任何想成为Instrumentation的类必须继承android.app.Instrumentation。

第三,利用android提供的androidTestCase,通过继承这个类来实现自己的test case,然后自己为test设计UI,该方法具体用法放在了另外一篇博客中,可以点击下面的链接阅读:

      第一部分

      第二部分

 

 

下面通过一个实例来看一下如何通过Instrumentation来做单元测试。

Step 1. 首先编写需要测试的activity:


package com.android.ut;

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

public class AndroidUT extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public int add(int a, int b)
    {
        return a + b;
    }
}


Step 2. 

接下来编写测试类,其中主要来测试add()方法。我们在当前代码目录下,在新建一个文件夹,命名为test,并在里面新建了包com.android.ut.test。然后往里面新增加一个class.具体如下:



package com.android.ut.test;

import com.android.ut.AndroidUT;

import android.test.ActivityInstrumentationTestCase;

public class TestApp extends ActivityInstrumentationTestCase<AndroidUT> {
    
    public TestApp()
    {
        super("com.android.ut", AndroidUT.class);
    }
    
    public void testSum()
    {
        assertEquals(5, getActivity().add(2, 3));
    }
    
}



Step 3.最后一步就是要改一下Manifest文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
      package="com.android.ut"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroidUT"
                  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:targetPackage="com.android.ut" android:name="android.test.InstrumentationTestRunner" android:label="Test Unit Tests"></instrumentation>
</manifest>



需要注意的是,在这里面我加上了:

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

以及:

<instrumentation android:targetPackage="com.android.ut" android:name="android.test.InstrumentationTestRunner" android:label="Test Unit Tests">instrumentation>

 

Step 4. 运行

首先通过模拟器运行一下AndroidUT,然后在命令行终端中运行

 adb shell am instrument -e class com.android.ut.test.TestApp -w com.android.ut/android.test.InstrumentationTestRunner

这样你就可以看到测试结果了。


更新于2009年5月11日:

Android 1.5 SDK对JUnit有了新的改进,现在可以在Run和Debug菜单中,可以找到Run as “Android JUnit Test”。具体可以参考Android SDK 1.5 release notes.


更新于2009年5月27日:

1.5 SDK文档的 FAQ中少了,在1.5中试验了一下,发现Run as Junit test不能用了,即使按照以前的方法配置Junit.

  1. In the Package Explorer view, select your project.
  2. Open the launch configuration manager.
    • In Eclipse 3.3 (Europa), select Run > Open Run Dialog... or Run > Open Debug Dialog...
    • In Eclipse 3.4 (Ganymede), select Run > Run Configurations... or Run > Debug Configurations...
  3. In the configuration manager, right-click the "JUnit" configuration type and select New
  4. In the new configuration's Test tab, specify the project and test class, as well as any options for running the test.
  5. In the new configuration's Classpath tab, find "Android Library" under Bootstrap Entries and remove it.
  6. Still in the Classpath tab, select Bootstrap Entries and click the Advanced button.
    • Choose Add Library and click OK.
    • Select JRE System Library and click Next.
    • Select Workspace Default JRE and click Finish.

        Select Bootstrap Entries again and click Advanced.

    • Choose Add Library and click OK.
    • Select JUnit 3 and click Finish.

而可喜的是,Run As.. -> Android Junit Test似乎可以用了,不过前提是需要配置一下:首先在manifest.xml中加上:

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

<instrumentation android:targetPackage="com.ut.prac" android:name="android.test.InstrumentationTestRunner" android:label="Test Unit Tests">instrumentation>

  

其次在Run configuration…中,选择Android Junit Test,再选择你要测试的test case(如果没有,需要在右键点击Android Junit Test,选择新建), 然后在右边的Test 那个tab中,指定Instrumentation Runner为android.test.InstrumentationTestRunner.

运行方法很简单,右键单击你的test case code文件,选择run as… -> android junit test.


 

 


阅读(6643) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-08-19 20:36:59

请问博主,为什么我按照你的设置运行1.5的Unit testing,会出现这个错误: Test run failed: Unable to instantiate instrumentation ComponentInfo{com.android.ut/android.test.InstrumentationTestRunner}: java.lang.ClassNotFoundException: android.test.InstrumentationTestRunner in loader dalvik.system.PathClassLoader@43735370

chinaunix网友2009-06-03 15:52:13

学习了,谢谢 很多地方都有这个文章,原来出处在这里 :)