如何从gradle运行Junit TestSuite?

时间:2012-03-07 18:14:19

标签: java gradle test-suite junit3

我正在尝试在我的项目中从Ant构建迁移到Gradle。有一堆测试用例(junit.framework.TestCase的子类)和几个测试套件(junit.framework.TestSuite的子类)。 Gradle自动选择要运行的所有测试用例(junit.framework.TestCase的子类),而不是套件(junit.framework.TestSuite的子类)。

我可能可以通过调用ant.junit来运行它。但是,我觉得应该有一种原生的简单方法来强制gradle选择并运行。我在文档中找不到任何内容。我错过了什么吗?

1 个答案:

答案 0 :(得分:14)

这对我来说很难弄明白,但这是一个例子:

// 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")
}