2012-03-07 105 views
14

我正試圖從Ant構建遷移到我的項目中的Gradle。有很多測試用例(junit.framework.TestCase的子類)和很少的測試用例(junit.framework.TestSuite的子類)。 Gradle自動獲取所有運行的測試用例(junit.framework.TestCase的子類),但不包括套件(junit.framework.TestSuite的子類)。如何從gradle運行Junit TestSuites?

我大概可以通過調用ant.junit來運行它。但是,我覺得應該有一個本地的簡單方法來迫使gradle挑選它們並運行。我在文檔中找不到任何東西。我錯過了什麼嗎?

+1

你似乎是自相矛盾?你在說'Gradle自動提取所有要運行的測試用例'。所以問題是它編譯了它們,但沒有運行它們?請澄清。 – 2012-03-07 23:25:08

+0

@c_maker:我編輯了文字以提高清晰度。感謝您指出。 – James 2012-03-08 11:42:20

回答

13

這是我很難搞清楚,但這裏有一個例子:

// excerpt from https://github.com/djangofan/WebDriverHandlingMultipleWindows 
package webdriver.test; 
import http.server.SiteServer; 
import java.io.File; 
import java.io.IOException; 
import org.junit.AfterClass; 
import org.junit.BeforeClass; 
import org.junit.runner.RunWith; 
import org.junit.runners.Suite; 

@RunWith(Suite.class) 
@Suite.SuiteClasses({ TestHandleCacheOne.class, TestHandleCacheThree.class, TestHandleCacheThree.class }) 
public class SuiteOne extends MultiWindowUtils { 

    public static SiteServer fs; 

    @BeforeClass 
    public static void setUpSuiteOne() { 
     File httpRoot = new File("build/resources/test"); 
     System.out.println("Server root directory is: " + httpRoot.getAbsolutePath()); 
     int httpPort = Integer.parseInt("8080"); 
     try { 
      fs = new SiteServer(httpPort , httpRoot); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     initializeBrowser("firefox"); 
     System.out.println("Finished setUpSuiteOne"); 
    } 

    @AfterClass 
    public static void tearDownSuiteOne() { 
     closeAllBrowserWindows(); 
     System.out.println("Finished tearDownSuiteOne"); 
    } 

} 

而類似這樣的的build.gradle:

apply plugin: 'java' 
apply plugin: 'eclipse' 
group = 'test.multiwindow' 

ext { 
    projTitle = 'Test MultiWindow' 
    projVersion = '1.0' 
} 

repositories { 
    mavenCentral() 
} 

dependencies { 
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.+' 
    compile group: 'junit', name: 'junit', version: '4.+' 
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+' 
} 

task testGroupOne(type: Test) { 
    //include '**/*SuiteOne.*' 
    include '**/SuiteOne.class' 
    reports.junitXml.destination = "$buildDir/test-results/SuiteOne") 
    reports.html.destination = "$buildDir/test-results/SuiteOne") 
} 

task testGroupTwo(type: Test) { 
    //include '**/*SuiteTwo.*' 
    include '**/SuiteTwo.class' 
    reports.junitXml.destination = "$buildDir/test-results/SuiteTwo") 
    reports.html.destination = "$buildDir/test-results/SuiteTwo") 
} 
+0

我應該說,自從我發佈這個答案以來,用於配置單元測試的DSL已經發生了一些變化。所以,要小心。 – djangofan 2016-02-16 16:32:51