gradle 編譯環境下進行android單元測試

來源:互聯網
上載者:User

gradle 編譯環境下進行android單元測試
====== android 單元測試介紹 ======
JUnit是一個開源的java單元測試架構,android的測試套件是基於JUnit 3的(不完全相容JUnit 4),Junit4隻需簡單瞭解即可,可以使用普通的junit來進行測試,推薦使用android的Junit測試架構進行高效全面的進行測試。
====== Android 單元測試架構UML ======
{{:dolphin_news:share:androidjunituml.png?300 |}}


====== eclipse下的andriod項目單元測試 ======
使用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看一下



package="com.dolphin.browser.core.test"
android:versionCode="1"
android:versionName="1.0" >

android:name="android.test.InstrumentationTestRunner"
android:targetPackage="mobi.mgeek.TunnyBrower" />
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >




可以看到, 已經自動產生了一些內容, 如uses-library和instrumentation已經自動產生了,這裡需要注意一下包名,因為前面我們選擇了被測試的工程為DolphinBrowerEn,所以這裡的自動為我們指定了targetPackage="mobi.mgeek.TunnyBrowser", 並且為測試工程包名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 3 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就變成了


package="com.dolphin.browser.core.test"
android:versionCode="1"
android:versionName="1.0" >

android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.dolphin.browser.core" />

android:name="com.dolphin.browser.core.test.Testall"
android:targetPackage="com.dolphin.browser.core" />
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >




TestSuite程式碼完成後如下, 代碼中使用了繼承InstrumentationTestRunner,主要是通過addTestSuite來添加測試類別,這裡我只有一個測試類別,當有多個測試類別時,就可以把它們都加進去。

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

===== 運行 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單元測試方法一 ======
這部分內容以AOSP 裡的development/samples/NotePad為例,NotePad工程下的單元測試代碼在它的tests檔案夾裡。
步聚:
* 先建立一個檔案夾testJunit(名字任意),將development/samples/NotePad項目複製到裡面。
* 現在檔案結構是 testJunit 裡面是NotePad工程,工程下就是src, res, tests, AndroidManifest等。
* 在testJunit裡建立三個檔案,build.gradle, common.gradle, settings.gradle.
build.gradle 內容如下:

def gradle = project.getGradle()
gradle.beforeProject { project ->


if (project.file('build.gradle').exists()) {
project.buildscript {
repositories {
maven {
name = "Baina Maven Proxy"
url = "http://mirrors.baina.com:8080/archiva/repository/internal"
}
}


dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
}
}

common.gradle內容如下:

tasks.withType(JavaCompile) {
options.fork = true
options.forkOptions.setMemoryMaximumSize("4096m")
options.forkOptions.setMemoryInitialSize("1024m")
options.encoding = "UTF-8"
}


android {
compileSdkVersion 17
buildToolsVersion "18.0.1"


sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest {
java.srcDirs = ['tests/src']
resources.srcDirs = ['tests/src']
aidl.srcDirs = ['tests/src']
renderscript.srcDirs = ['tests/src']
res.srcDirs = ['tests/res']
assets.srcDirs = ['tests/assets']
}


//instrumentTest.setRoot('tests')
}
}

此處instrumentTest也可以另一種方式寫成
instrumentTest.setRoots('tests'), 這樣的話,需把NotePad裡的src改為java, 為了在eclipse中能運行,建議採用分別指明目錄的方式。


settings.gradle內容如下:

include ':NotePad'



* 在NotePad 檔案夾裡建立build.gradle, 內容如下:

apply plugin: 'android'
apply from: "$project.rootDir/common.gradle"


android {
compileSdkVersion 16


buildTypes {
release {
runProguard true
proguardFile 'proguard.cfg'
}


debug {
runProguard true
proguardFile 'proguard-debug.cfg'
}
}

defaultConfig {
testPackageName "com.example.android.notepad.tests"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
}

}

說明:以上四個gradle檔案我是從shell_en_agile裡的複製過來的, 參考它的結構並修改而來的。
* 編譯
在 testJunit 運行 gradle --daemon assembleDebug 會產生NotePad工程的apk, 運行 gradle --daemon assembleTest 會產生單元測試工程的apk。
* 運行
安裝test apk, 運行 adb shell am instrument -w com.dolphin.browser.junittest/android.test.InstrumentationTestRunner。


====== gradle 環境下做android單元測試的配置方法二 ======
實現工作中,為了使tests工程與被測工程的git commit message相關不干擾,可以將單元測試獨立出來管理,這樣與被測工程相對獨立,tests工程不會影響NotePad工程。在方法一的基礎上繼續下面的操作。
步聚:
* 將NotePad 裡的 tests剪下出來放到NotePad 平行的位置, 現在檔案結構是 testJunit 裡有兩個檔案夾,NotePad 及tests, NotePad裡面只剩下了src, res等。
* cd NotePad , 將測試工程通過軟連結的方式連結進來: ln -s ../tests tests。
* 編譯:
在testJunit下編譯 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 \\


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.