2016-11-15 20 views
6

我最近一直在想這個問題,並希望得到一些有關我幾天前的想法的反饋。關於`test-support` gradle配置的反饋代碼

問題:

在典型的代碼的基礎上,每一個模塊具有一個maintest源極 - 集。這可以在一段時間內工作得很好,但是我遲早會偶然發現我想組合一堆允許更簡單地測試涉及某個模塊的代碼的類的情況。一個很好的例子是給定模塊的一組hamcrest匹配器類。

  • 假設1:
    作爲hamcrest爲測試代碼的庫,這些匹配不應進入main源極 - 集。

  • 假設2: 這些類也不能進入test源設置或者,作爲對test源的依賴,不過是爲了這些類可用的解決方法。一個人通常不希望依賴於實際的測試。 它也是not recommended(由Netflix提供)來定義對項目的test源集的依賴關係。

解決方案1 ​​

創建包含在這些類的專用模塊的main源設置和簡單地定義這個模塊測試的依賴,無論你需要他們。

這是我現在用了相當一段時間的方法,但我並不喜歡它。

  • 首先,我從來沒有一個很好的名字出現了,除了追加testSupport而導致該名狀core-testSupportpersistence-testSupport等原有模塊的名稱。

  • 其次,它創建了很多模塊,項目樹受到了這些模塊的污染。

解決方案2:(一個我將不勝感激反饋)

configurations { 
    testSupportCompile.extendsFrom compile 
    testSupportRuntime.extendsFrom runtime 
} 

sourceSets { 
    testSupport { 
     compileClasspath += sourceSets.main.output + configurations.testSupportCompile 
     runtimeClasspath += compileClasspath + configurations.testSupportRuntime 
    } 
} 

task testSupportJar(type: Jar) { 
    from sourceSets.testSupport.output 
    classifier 'testSupport' 
} 

artifacts { 
    testSupportCompile testSupportJar 
} 

以上gradle這個配置就可以去參加一個名爲testSupport.gradle文件,並可以應用到需要這個專用源 - 任何模塊設置爲提供可以在測試中重用的類。

定義的依賴會的工作是這樣的:

testCompile project(path: ':core', configuration: 'testSupportCompile') 

我仍然有種新搖籃,並研究了很多,但我仍然有幾個問題。

  1. 據我所知,宣佈新源設置自動創建兩個配置:<sourceSet>Compile<sourceSet>Runtime。我不太喜歡這種方法,那就是在聲明依賴關係時必須使用testSupport 編譯配置。有沒有辦法將這個別名僅僅testSupport或類似的東西?

  2. 我的項目目前編譯得很好。但是,我不確定我是否以正確的方式做事。這個配置怎麼能改進?

  3. 是否有其他方法來實現所需的功能?在研究的過程中,我並沒有真正發現這個話題,這讓我覺得我要麼使用錯誤的搜索術語,要麼做一些愚蠢的事情,根本就不應該這樣做。

我知道這是一個廣泛的問題,但我不確定在哪裏得到適當的反饋意見,除了這裏的東西。

回答

1

我有類似的情況,我一直在推遲解決方案一段時間,使用各種黑客和變通辦法。你的問題是研究它的最後一個動機。

這是我結束了 - 編輯合作與托馬斯做:

configurations { 
    // create a new configuration and inherit everything from compile 
    testlib.extendsFrom compile 
} 

sourceSets { 
    testlib { 
     // We will at least need access to our main sourceSet and all dependencies that are declared for our configuration. 
     compileClasspath += sourceSets.main.output + configurations.testlib 
    } 
} 

task testlibJar(type: Jar) { 
    from sourceSets.testlib.output 
    classifier 'testlib' 
} 

artifacts { 
    testlib testlibJar // include the classes into the new configuration 
    archives testlibJar // optional: include the support JAR into "uploadArchives", so it may also be used in other projects 
} 

然後,根據模塊中,只需使用:

dependencies { 
    testCompile project(path: ':otherproject', configuration: 'testlib') 
} 

注意, (空)testlibCompiletestlibRuntime配置仍然創建(由於引入了新的testlib源集),但我認爲忽略它們是安全的。

此外,您的項目自己的test配置需要使用testlib(項目測試取決於通用測試支持)通常是這種情況。在這種情況下,你可能需要添加依賴同一項目的兩種配置:

testCompile project(path: ':myproject', configuration: 'testlib') 

或單獨增強的編譯和運行時類路徑:

configurations { 
    testlib.extendsFrom compile 
    testCompile.extendsFrom testlib 
} 
sourceSets { 
    test { 
     compileClasspath += sourceSets.testlib.output 
     runtimeClasspath += sourceSets.testlib.output 
    } 
} 
+0

如果我正確地解釋你的解決方案,唯一的差異是testLib在測試中的隱含依賴性(這似乎是好的)。 我會盡快在家裏看看它。 –

+0

主要區別是在''配置'和'檔案'部分內使用'testSupport'配置來代替'testSupportCompile'。 – Marwin

+0

我認爲我也將此作爲中介解決方案,但沒有「test」配置對'testSupport'配置的隱式依賴關係。問題是,如果沒有這種隱式依賴性,如果不重用由源集自動創建的配置,則需要在每個不起作用的模塊中明確聲明它。 Btw:你不需要明確地設置你的sourceSet的srcDir。具有配置名稱的srcDir是隱式創建的。 –