2011-01-21 90 views
11

我正在嘗試讓Gradle執行一些使用testng.xml文件定義的測試。我的testng.xml文件如下所示:如何告訴Gradle使用我的testng.xml文件進行測試類和排序?

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > 
<suite name="mySuite" verbose="1"> 

    <listeners> 
    <listener class-name="mypackage.MyListener" /> 
    <listener class-name="mypackage.TestOrderer" /> 
    </listeners> 

    <test name="Tests"> 
    <classes> 
     <class name="mytestpackage.CrazyTest1"/> 
     <class name="mytestpackage.CrazyTest2"/> 
     <class name="mytestpackage.CrazyTest3"/> 
    </classes> 
    </test> 
</suite> 

那麼爲什麼我需要這個?我想確保我的測試按照與here中列出的註釋類似的註釋方式進行組織。正如你可能猜到的,TestOrderer是一個IMethodInterceptor。

這是問題所在,Gradle似乎在接管我的testng.xml文件並且抓取測試目錄來找到它想要執行的測試。即使我禁用了它,也無法正確執行這些方法。以下是我認爲應該工作的內容,但僅僅是,不。

test { 
    useTestNG() 
    options.suites("src/test/resources/crazyTestNG.xml") 
    # Adding 
    # scanForTestClasses = false 
    # causes zero tests to be executed, since the class names don't end in Test 
} 

現在看來似乎應該是一個沒有腦子...叉TestNG的過程中,並讓它運行,但我無法弄清楚如何告訴搖籃走出的方式,只是執行我的試驗。

回答

1

Gradle TestNG runner假定如果沒有指定測試類,通過掃描它們或者對名稱進行模式匹配,那麼它應該完全跳過測試執行。

相反,它應該考慮是否已經提供套件xml。你能爲這個問題添加一個jira問題嗎?

一種可能的解決辦法是使用options.listener指定的聽衆,而不是用在所有的一套XML文件:

test { 
    options.listeners << 'mypackage.MyListener' 
    options.listeners << 'mypackage.TestOrderer' 
} 

這樣,您就不需要指定的測試類,和你可以讓掃描找到你。

+0

否我需要指定測試類。由於TestNG引擎的特定用例,我需要讓它們以特定的順序運行。掃描打破了這個過程。 – 2011-01-24 13:20:41

0

此方法不使用您的testng.xml文件,但您也可以模擬testNG測試組並通過將JUnit測試組創建爲Gradle任務來進行排序,然後在執行構建時通過訂購任務執行來手動排序:

// in build.gradle 
task testGroupOne(type: Test) { 
    //include '**/*SuiteOne.*' 
    include '**/SuiteOne.class' 
    testReportDir = file("${reportsDir}/SuiteOne") 
    testResultsDir = file("${buildDir}/test-results/SuiteOne") 
} 

task testGroupTwo(type: Test) { 
    //include '**/*SuiteTwo.*' 
    include '**/SuiteTwo.class' 
    testReportDir = file("${reportsDir}/SuiteTwo") 
    testResultsDir = file("${buildDir}/test-results/SuiteTwo") 
} 

然後,運行構建這樣的:gradle這個testGroupTwo testGroupOne

6

我討厭在gradle這個TestNG的支持...發現比使用原始TestNG的它是最聯合國flexable。我厭倦了用gradle擺弄。我的解決方案..運行TestNG的直接使用搖籃任務

task runTests(type: JavaExec, dependsOn: 'classes') { 
    main = 'org.testng.TestNG' 
    classpath = files("./src/test/resources", 
         project.sourceSets.main.compileClasspath, 
         project.sourceSets.test.compileClasspath, 
         project.sourceSets.main.runtimeClasspath, 
         project.sourceSets.test.runtimeClasspath) 
    args = ["-parallel", "methods", "-threadcount", "1", "-d", "./build/test-output", "./src/test/resources/test.xml"] 
} 

這是我從命令行中運行:

gradle runTests

6

這裏是你如何配置一個測試套件(XML)在被執行Gradle任務:

apply plugin: 'java' 

repositories { 
    mavenCentral() 
} 
dependencies { 
    // add the dependencies as needed 
    testCompile group: 'org.testng', name: 'testng', version:'6.8.8' 
    testCompile fileTree('lib') 
} 
test { 
    useTestNG() { 
     // runlist to executed. path is relative to current folder 
     suites 'src/test/resources/runlist/my_test.xml' 
    } 
} 
+0

更多選項可以在這裏找到(https://docs.gradle.org/current/groovydoc/org/gradle/api/tasks/testing/testng/TestNGOptions.html)。 – Nick 2015-08-19 03:25:25

相關問題