2012-07-20 17 views
61

मैं अपने ग्रैडल बिल्ड (संस्करण 1.0) में एकीकरण परीक्षण जोड़ना चाहता हूं। उन्हें अपने सामान्य परीक्षणों से अलग से भागना चाहिए क्योंकि उन्हें स्थानीयहोस्ट पर तैनात वेबपैप की आवश्यकता होती है (वे उस वेबपैप का परीक्षण करते हैं)। परीक्षण मेरे मुख्य स्रोत सेट में परिभाषित कक्षाओं का उपयोग करने में सक्षम होना चाहिए। मैं यह कैसे करूं?मैं ग्रैडल में नया स्रोत कैसे जोड़ूं?

उत्तर

87

मुझे पता लगाने में कुछ समय लगा और ऑनलाइन संसाधन बहुत अच्छे नहीं थे। तो मैं अपना समाधान दस्तावेज करना चाहता था।

यह एक सरल Gradle निर्माण स्क्रिप्ट एक intTest स्रोत मुख्य और परीक्षण स्रोत सेट के अलावा सेट किया है:

apply plugin: "java" 

sourceSets { 
    // Note that just declaring this sourceset creates two configurations. 
    intTest { 
     java { 
      compileClasspath += main.output 
      runtimeClasspath += main.output 
     } 
    } 
} 

configurations { 
    intTestCompile.extendsFrom testCompile 
    intTestRuntime.extendsFrom testRuntime 
} 

task intTest(type:Test){ 
    description = "Run integration tests (located in src/intTest/...)." 
    testClassesDir = project.sourceSets.intTest.output.classesDir 
    classpath = project.sourceSets.intTest.runtimeClasspath 
} 
+5

आपको अभी भी एकीकरण परीक्षण कार्य घोषित करने और कॉन्फ़िगर करने की आवश्यकता होगी। दस्तावेज़ीकरण के संदर्भ में, पूर्ण ग्रैडल वितरण में 'जावा/इंटिग्रेशनटेस्ट्स' नमूना है। –

+0

धन्यवाद @ पीटर Niederwieser मैंने अपनी नमूना निर्माण स्क्रिप्ट को सही किया है। – Spina

+2

मैं इसे भी करने की कोशिश कर रहा था ... समाधान पोस्ट करने के लिए बहुत बहुत धन्यवाद :) –

26

यहाँ मैं कैसे configurations{ } का उपयोग किए बिना यह लक्ष्य प्राप्त है।

apply plugin: 'java' 

sourceCompatibility = JavaVersion.VERSION_1_6 

sourceSets { 
    integrationTest { 
     java { 
      srcDir 'src/integrationtest/java' 
     } 
     resources { 
      srcDir 'src/integrationtest/resources' 
     } 
     compileClasspath += sourceSets.main.runtimeClasspath 
    } 
} 

task integrationTest(type: Test) { 
    description = "Runs Integration Tests" 
    testClassesDir = sourceSets.integrationTest.output.classesDir 
    classpath += sourceSets.integrationTest.runtimeClasspath 
} 

का उपयोग कर परीक्षण किया गया: Gradle 1.4 और Gradle 1,6

+2

साझा करने के लिए धन्यवाद! वैकल्पिक कार्यान्वयन देखना अच्छा होता है। – Spina

+1

जबकि 'जावा { srcDir 'src/integrationtest/जावा' } संसाधनों { srcDir 'src/integrationtest/संसाधनों' } के बाद से यह सिर्फ' redeclares src//... '' के लिए प्रासंगिक नहीं है 'src/integrationtest/... ': यहां: पूंजी टी को निचले टी –

9

दोनों वर्ष जवाब संक्षेप में (पाने के लिए सबसे अच्छा और न्यूनतम दोनों दुनिया का व्यवहार्य):

कुछ गर्म शब्द पहले:

  1. पहले, हमें स्रोतसेट को परिभाषित करने की आवश्यकता है:

     
    sourceSets { 
        integrationTest 
    } 
    

  2. अगले हम परीक्षण से sourceSet का विस्तार, की वजह हम परीक्षण runtimeClasspath व्युत्पन्न sourceSet

     
    sourceSets { 
        integrationTest { 
         compileClasspath += sourceSets.test.runtimeClasspath 
         runtimeClasspath += sourceSets.test.runtimeClasspath // ***) 
        } 
    } 
    

    • *** नोट) के लिए किसी भी तरह इस का उपयोग (जो परीक्षण से सभी deps भी शामिल है और खुद का परीक्षण) classpath के रूप में पुन: घोषणा/sourceSets.integrationTest.runtimeClasspath के लिए विस्तार की जरूरत है, लेकिन जब से runtimeClasspath हमेशा output + runtimeSourceSet फैलता अप्रासंगिक होना चाहिए, यह
  3. हम सिर्फ एकीकरण परीक्षणों +०१२३३०२८१०९१ को चलाने के लिए एक समर्पित कार्य को परिभाषित नहीं मिलता

  4. टेस्ट काम करने वाले परीक्षण वर्गों और classpaths "परीक्षण" sourceset से चूक के बजाय इस्तेमाल किया जाना चाहिए बता

     
    task integrationTest(type: Test) { 
        testClassesDir = sourceSets.integrationTest.output.classesDir 
        classpath = sourceSets.integrationTest.runtimeClasspath 
    } 
    

  5. (वैकल्पिक) ऑटो रन के बाद परीक्षण

     
    integrationTest.dependsOn test 
    

  6. (वैकल्पिक)के साथ ऑटो रन

     
    check.dependsOn integrationTest 
    

  7. (वैकल्पिक) जावा में जोड़ें, संसाधनों को संसाधनों को ऑटो-डिटेक्ट करने और अपने आईडीई में इन "आंशिक" बनाने की अनुमति देने के लिए। अर्थात।IntelliJ विचार स्वत: प्रत्येक सेट के sourceSet निर्देशिका जावा और संसाधनों का निर्माण करेगा अगर यह

     
    sourceSets { 
        integrationTest { 
         java 
         resources 
        } 
    } 
    

tl मौजूद नहीं है; डॉ

apply plugin: 'java' 

// apply the runtimeClasspath from "test" sourceSet to the new one 
// to include any needed assets: test, main, test-dependencies and main-dependencies 
sourceSets { 
    integrationTest { 
     // not necessary but nice for IDEa's 
     java 
     resources 

     compileClasspath += sourceSets.test.runtimeClasspath 
     // somehow this redeclaration is needed, but should be irrelevant 
     // since runtimeClasspath always expands compileClasspath 
     runtimeClasspath += sourceSets.test.runtimeClasspath 
    } 
} 

// define custom test task for running integration tests 
task integrationTest(type: Test) { 
    testClassesDir = sourceSets.integrationTest.output.classesDir 
    classpath = sourceSets.integrationTest.runtimeClasspath 
} 
integrationTest.dependsOn test 

की चर्चा करते हुए:

दुर्भाग्य से, github.com/gradle/gradle/subprojects/docs/src/samples/java/customizedLayout/build.gradle या …/gradle/…/withIntegrationTests/build.gradle पर उदाहरण के कोड इस संभाल करने के लिए नहीं लगता है या वैसे भी एक अलग/अधिक जटिल/मेरे लिए कोई स्पष्ट समाधान है!

+1

(!) में बदलें, जैसा कि यह पता चला है, कॉन्फ़िगरेशन या आउटपुट परिणामों के बिना स्रोतसेट संवर्धन का एकल उपयोग अंततः एक खोलने के बाद विचार में त्रुटि बनाने में परियोजना। नए "मॉड्यूल" (यहां: एकीकरण टेस्ट) के लिए निर्माण निर्भरता (यहां: परीक्षण) पहले 'compileTestJava' पर उपलब्ध नहीं है –

6

nebula-facet प्लगइन बॉयलरप्लेट समाप्त:

apply plugin: 'nebula.facet' 
facets { 
    integrationTest { 
     parentSourceSet = 'test' 
    } 
} 

एकीकरण परीक्षण के लिए विशेष रूप से, यहां तक ​​कि this is done for you, बस लागू होते हैं: प्रत्येक के लिए

apply plugin: 'nebula.integtest' 

Gradle प्लगइन पोर्टल लिंक कर रहे हैं:

  1. nebula.facet
  2. nebula.integtest
0

यहां मेरे लिए Gradle 4.0 के रूप में क्या काम करता है।

sourceSets { 
    integrationTest { 
    compileClasspath += sourceSets.test.compileClasspath 
    runtimeClasspath += sourceSets.test.runtimeClasspath 
    } 
} 

task integrationTest(type: Test) { 
    description = "Runs the integration tests." 
    group = 'verification' 
    testClassesDirs = sourceSets.integrationTest.output.classesDirs 
    classpath = sourceSets.integrationTest.runtimeClasspath 
} 

संस्करण 4.0 के अनुसार, ग्रैडल अब स्रोत सेट में प्रत्येक भाषा के लिए अलग-अलग कक्षा निर्देशिकाओं का उपयोग करता है। इसलिए यदि आपकी बिल्ड स्क्रिप्ट sourceSets.integrationTest.output.classesDir का उपयोग करती है, तो आपको निम्न बहिष्करण चेतावनी दिखाई देगी।

ग्रैडल अब प्रत्येक जेवीएम भाषा के लिए अलग आउटपुट निर्देशिका का उपयोग करता है, लेकिन यह बिल्ड स्रोत सेट से सभी वर्गों के लिए एक एकल निर्देशिका मानता है। इस व्यवहार को बहिष्कृत कर दिया गया है और इसे ग्रेडल 5.0

इस चेतावनी से छुटकारा पाने के लिए, बस sourceSets.integrationTest.output.classesDirs पर स्विच करने के लिए निर्धारित किया गया है। अधिक जानकारी के लिए, Gradle 4.0 release notes देखें।

संबंधित मुद्दे