Android Test Case

====== eclipse下的andriod项目单元测试 ======


===== Android Junit 介绍 =====
JUnit是一个开源的java单元测试框架,android的测试套件是基于JUnit 3的(不完全兼容JUnit 4),Junit4只需简单了解即可,可以使用普通的junit来进行测试,推荐使用android的Junit测试框架进行高效全面的进行测试。
===== Android 单元测试框架中涉及的一些类的UML =====


{{:dolphin_news:share:androidjunituml.png?300 |}}
===== AndroidJunit =====


使用AndroidJunit来做单元测试,大致可以分以下几个步骤: 
    - 新建Android Test Project
    - 新建Junit test case
    - 新建Junit test suite
    - 运行Junit test
===== 新建 Android Test Project=====


以下所有讲述都在eclipse中导入了shell_en_aglie的基础上的完成的,请确保首先你已经成功导入了shell_en_agile工程。新建Android Test Project方法:
    - 在eclipse中File->New->Other->Android->AndroidTestProject, 输入测试工程名, 如输入dolphin_junit_test。
    - 下一步选择被测试的工程, 如选择测试DolphinBrowerEn, 这一步就完成了。
    
    此时我们打开AndroidManifest.xml看一下
<code>    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    <!-- 此处包名被修改了,自动会根据被测项目生成为mobi.mgeek.TunnyBrower.test-->
    package="com.dolphin.browser.core.test"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="7" />
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="mobi.mgeek.TunnyBrower" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>
    </manifest>
</code>    
可以看到, 已经自动生成了一些内容, 如uses-library和instrumentation已经自动生成了,这里需要注意一下包名,因为前面我们选择了被测试的工程为DolphinBrowerEn,所以这里的自动为我们指定了targetPackage="mobi.mgeek.TunnyBrowe", 并且为测试工程包名package="mobi.mgeek.TunnyBrowe.test",这里的包名因需要可以修改, 但targetPackage="com.dolphin.browser.core"为被测工程的包名,不能修改。  
===== 新建 Junit Test Case =====
新建Junit Test Case方法:
  - 在eclipse中File->New->Other->Junit->Junit Test Case
  - 在弹出的Junit Test Case 对话框中选择New Junit 4 test,Superclass 选择AndroidTestCase,并选择 Class under test 被测试类,输入Name类名,点击下一步后。
  - 选择需要被测试的方法,这里根据实际情况选择需要被测试的方法, 点击完成。
===== 新建 Junit Test Suite=====  
新建 Junit Test Suite方法:  
  - 在eclipse中File->New->Other->Junit->Junit Test Suite
  - 在弹出的Junit Test Case 对话框中选择需要测试的类。
  - 选择需要被测试的方法,这里根据实际情况选择需要被测试的方法, 输入Name后点击完成。
  - 编写TestSuite代码
  - 编写TestSuite后, 需要在AndroidManifest中添加一项instrumentation, 添加后的AndroidManifest就变成了
<code>    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dolphin.browser.core.test"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="7" />
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.dolphin.browser.core" />
    <!-- 添加 testsuite 项 -->    
    <instrumentation
        android:name="com.dolphin.browser.core.test.Testall"
        android:targetPackage="com.dolphin.browser.core" />    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>
    </manifest>
</code>
TestSuite代码完成后如下, 代码中使用了继承InstrumentationTestRunner,主要是通过addTestSuite来添加测试类,这里我只有一个测试类,当有多个测试类时,就可以把它们都加进去。
<code java>    
public class TestAll extends InstrumentationTestRunner{


    private static final String TAG = "TestAll";


    public TestSuite getAllTests(){
        Log.i(TAG, "Junit test, testsuite");
        TestSuite suite = new InstrumentationTestSuite(this);
        suite.addTestSuite(NewsRequestWrapperTest.class);
        return suite;
    }
}
</code>
===== 运行 Junit =====
可以参考 http://blog.csdn.net/xianming01/article/details/7463066 ,由于被测试工程较大, 在eclipse中导致不能通过图形化界面运行Junit,所以我们可以采用adb shell am 的方式运行生成的junit apk 来测试。具体方法是:
  - 将被测apk安装到手机中
  - 将junit程序生成后的apk安装到手机中
  - 输入adb shell pm list instrumentation, 会显示类似以下信息,下面显示了有两个instruementation, 第一个test case, 第二个是test suite.
    
    
**instrumentation:com.dolphin.browser.junittest/android.test.InstrumentationTestRunner (target=mobi.mgeek.TunnyBrowser)**\\
**instrumentation:com.dolphin.browser.junittest/.testall.TestAll (target=mobi.mgeek.TunnyBroer)**
    
按链接中的说明, 可以输入adb shell am instrument -w com.dolphin.browser.junittest/android.test.InstrumentationTestRunner执行单条测试用例, 也可以
输入adb shell am instrument -w com.dolphin.browser.junittest/.testall.TestAll来执行test suite。


===== 注意事项=====
  - 创建工程时, 注意package 和 targetPackage 的关系。
  - 由于我测试的是DolphinBrowserEn,在eclipse中无法编译,所以使用了生成测试apk,然后安装到手机中,使用adb shell am 方式进行测试。
  - 如何写测试方法, 这里不做说明, 需要注意被测试方法为异步方法时,如http,需要用到线程同步去等待异步任务的完成。
  - 我们也可以写一个activity,画一个简单的界面,通过点击button触发单元测试, 也可以对界面进行单元测试。
====== gradle 环境下做android单元测试的配置方法一  ======
将单元测试与被测工程合并在一起,这里以 DolphinBrowserEN 工程为例。
步聚:
  * 将上述单元测试工程tests复制到DolphinBrowserEN根目录。
  * 修改DolphinBuild下的common.gradle文件,为单元工程指定目录。     
在sourceSets中增加如下内容:
<code>
    instrumentTest {
        manifest.srcFile 'tests/AndroidManifest.xml'
        java.srcDir 'tests/src'
    }
</code>
注: 此处也可以另一种方式写成
instrumentTest.setRoots('tests'), 这样的话,需把src改为java, 为了在eclipse中能运行,建议采用第一种方法。
  * 修改 DolphinBrowserEN 的build.gradle,在android 里添加
<code>
    defaultConfig{
        testPackageName "com.dolphin.browser.junittest"
        testInstrumentatitionRunner "android.test.InstrumentationTestRunner"
    }
</code>      
  * 编译:
在 DolphinBrowserEN 运行 gradle --daemon assembleDebug 会生成咱们工程的apk, 运行 gradle --daemon assembleTest 会生成单元测试工程的apk。
  * 运行:
安装test apk, 运行 adb shell am instrument -w com.dolphin.browser.junittest/android.test.InstrumentationTestRunner。
====== gradle 环境下做android单元测试的配置方法二  ======
将单元测试独立出来管理,这样与被测工程相对独立,互不干扰,不影响Buildbot。
步聚:
  * 将单元测试工程tests放到与DolphinBrowserEN同级目录。
  * 修改DolphinBuild下的common.gradle文件,为单元工程指定目录。     
在sourceSets中增加如下内容:
<code>
    instrumentTest {
        manifest.srcFile 'tests/AndroidManifest.xml'
        java.srcDir 'tests/Java'
    }
</code>
注: 此处也可以用另一种方式写成
instrumentTest.setRoots('tests'), 这样的话,需把src改为java, 为了在eclipse中能运行,建议采用第一种方法。 
  * 修改 DolphinBrowserEN 的build.gradle,在android 里添加
<code>
    defaultConfig{
        testPackageName "com.dolphin.browser.junittest"
        testInstrumentatitionRunner "android.test.InstrumentationTestRunner"
    }
</code> 
  * cd DolphinBrowserEN , 将测试工程通过软链接的方式链接进来: ln -s ../tests tests。
  * 编译:在DolphinBrowserEN下编译 gradle --deamon installTest, 编译完成后, 可以查看build下是否生成了对应的单测测试文件的class文件,确认编译没有问题。
  * 运行:参照测试工程tests, 具体命令可参考AOSP/development/samples/ApiDemos/tests 里的 AllTests.java。
To run all suites found in this apk: adb shell am instrument -w com.dolphin.browser.junittest/android.test.InstrumentationTestRunner \\
To run just this suite from the command line: adb shell am instrument -w -e class com.dolphin.browser.junittest.TestAll com.dolphin.browser.junittest/android.test.InstrumentationTestRunner \\
To run an individual test case: adb shell am instrument -w -e com.dolphin.browser.junittest.xxxtest com.dolphin.browser.junittest/android.test.InstrumentationTestRunner \\
To run an individual testcase: adb shell am instrument -w -e class com.dolphin.browser.junittest.news.test.NewsRequestWrapperTest com.dolphin.browser.junittest/android.test.InstrumentationTestRunner \\
 
      
    
   
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值