2013-10-10 26 views
5

我使用awesome plugin from Jake Wharton Android的單元測試。爲了讓這些單元測試正常運行,我的目標是速度(TDD快速反饋和全部)。運行的gradle從單一的Android(單元)測試,而無需加載其他項目的依賴

我設法正確配置,並在運行一些樣本測試如下:

./gradlew test 

每當我進行的測試,雖然我注意到下面的輸出:

Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0 
The Test.testReportDir property has been deprecated and is scheduled to be removed in Gradle 2.0. Please use the Test.getReports().getHtml().getDestination() property instead. 
The TaskContainer.add() method has been deprecated and is scheduled to be removed in Gradle 2.0. Please use the create() method instead. 
:mySampleApp:preBuild UP-TO-DATE 
:mySampleApp:preDebugBuild UP-TO-DATE 
:mySampleApp:preReleaseBuild UP-TO-DATE 
:libraries:facebook:compileLint 
:libraries:facebook:copyReleaseLint UP-TO-DATE 
:libraries:facebook:mergeReleaseProguardFiles UP-TO-DATE 
:libraries:facebook:packageReleaseAidl UP-TO-DATE 
:libraries:facebook:preBuild UP-TO-DATE 
:libraries:facebook:preReleaseBuild UP-TO-DATE 
:libraries:facebook:prepareReleaseDependencies 
:libraries:facebook:compileReleaseAidl UP-TO-DATE 
:libraries:facebook:compileReleaseRenderscript UP-TO-DATE 
:libraries:facebook:generateReleaseBuildConfig UP-TO-DATE 
:libraries:facebook:mergeReleaseAssets UP-TO-DATE 
:libraries:facebook:mergeReleaseResources UP-TO-DATE 
:libraries:facebook:processReleaseManifest UP-TO-DATE 
:libraries:facebook:processReleaseResources UP-TO-DATE 
:libraries:facebook:generateReleaseSources UP-TO-DATE 
:libraries:facebook:compileRelease UP-TO-DATE 
:libraries:facebook:processReleaseJavaRes UP-TO-DATE 
:libraries:facebook:packageReleaseJar UP-TO-DATE 
:libraries:facebook:packageReleaseLocalJar UP-TO-DATE 
:libraries:facebook:packageReleaseRenderscript UP-TO-DATE 
:libraries:facebook:packageReleaseResources UP-TO-DATE 
:libraries:facebook:bundleRelease UP-TO-DATE 
:mySampleApp:prepareComAndroidSupportAppcompatV71800Library UP-TO-DATE 
:mySampleApp:preparemySampleAppandroidLibrariesFacebookUnspecifiedLibrary UP-TO-DATE 
:mySampleApp:prepareDebugDependencies 
:mySampleApp:compileDebugAidl UP-TO-DATE 
:mySampleApp:compileDebugRenderscript UP-TO-DATE 
:mySampleApp:generateDebugBuildConfig UP-TO-DATE 
:mySampleApp:mergeDebugAssets UP-TO-DATE 
:mySampleApp:mergeDebugResources UP-TO-DATE 
:mySampleApp:processDebugManifest UP-TO-DATE 
:mySampleApp:processDebugResources UP-TO-DATE 
:mySampleApp:generateDebugSources UP-TO-DATE 

搖籃似乎加載我的項目的所有依賴關係。

我的樣品測試如下:

package com.mycompany.mysampleapp; 

import org.junit.Test; 

import static org.fest.assertions.api.Assertions.assertThat; 

public class AdditionOperationsTest { 
    @Test public void testModulus() { 
    assertThat(1).isEqualTo(1); 
    } 
} 

這個測試實際上應該採取幾分之一秒到運行。我的理解是,所有預加載項目依賴關係都讓它陷入困境。

在美好的日子,我會確保我有什麼,我需要在CLASSPATH中,只是運行是這樣的:

javac src/test/java/main/java/com/micromobs/pkk/AdditionOperationsTest.java 
java org.junit.runner.JUnitCore com.micromobs.pkk.AdditionOperationsTest 

考慮到這是一個Android項目,gradle這個,我假設我將有做一些有點不同的事情,比如在Gradle構建文件中創建特定任務,其中只包含我的示例項目的測試文件,然後運行gradle命令./gradlew taskName?

問:是否可以運行我的項目(com.mycompany.mysampleapp)範圍內的單個測試「AdditionOperationsTest」獨所以它不會加載外部項目依賴

下面是我的配置文件目前看起來像:

# settings.gradle 
include ':libraries:gradle-android-test-plugin' 
include ':libraries:facebook', ':mysampleapp' 

# build.gradle 
... 
apply plugin: 'android-test' 

dependencies { 
    testCompile 'junit:junit:4.10' 
    testCompile 'org.robolectric:robolectric:2.1.+' 
    testCompile 'com.squareup:fest-android:1.0.+' 
} 

# location of my test files: 
androidproj/mysampleapp/src/test/java/main/com/mycompany/mysampleapp/AdditionOperationsTest.java 
+0

我試圖解決這個同樣的問題。我認爲要做的事情就是擁有多模塊項目,以便測試存在於一個單獨的模塊中,但我希望這不是必需的。 – Turnsole

+0

是的,我真的不想把我的測試分解成不同的模塊。我覺得我會失去將我們整個項目轉移到gradle的好處。我希望這只是我必須添加的一些愚蠢的參數或者我必須聲明的任務,它會忽略外部項目依賴關係,並運行我指向的確切測試以及它只需要的依賴關係。 –

回答

4

您應該能夠通過使用-acommand line option(不重建項目依賴的)這樣做。執行gradle -a test應導致libraries:facebookmysampleapp項目不被重建。

編輯:如前所述下面你可以顯著提高您的搖籃建立使用Gradle daemon性能。

+0

謝謝Benjamin,哇,這是完全正確的。添加參數確保facebook lib不被重建。但我注意到,我只颳了幾分鐘。沒有顯着地讓我的測試運行得更快:(你可以提供的任何提示使測試運行得更快,例如只有在使用時纔會延遲加載依賴項?我沒有使用「框架」的任何部分,所以應該真的跑得快。乾杯。 –

+0

不是我所知道的。多長時間你的測試需要跑?你可能想看看爲什麼它其實需要,只要它需要的時刻(例如,不測試撥打電話到外部系統)。 –

+0

總時間:10.612秒我認爲這與設置有關,因爲測試類每個都只是一個虛擬測試類(與我在問題中發佈的測試類完全相同),它應該即時運行。我認爲在進行單個測試之前,gradle會重新計算/重新編譯或者做一些準備整個項目的工作。 –

相關問題