如何从Gradle运行Junit TestSuites? [英] How to run Junit TestSuites from gradle?

查看:194
本文介绍了如何从Gradle运行Junit TestSuites?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的项目中的Ant构建迁移到Gradle。有很多测试用例(junit.framework.TestCase的子类)和很少的测试用例(junit.framework.TestSuite的子类)。 Gradle自动获取所有运行的测试用例(junit.framework.TestCase的子类),但不包括套件(junit.framework.TestSuite的子类)。

我可能会通过调用ant.junit来运行它。但是,我觉得应该有一个本地的简单方法来迫使gradle挑选它们并运行。我在文档中找不到任何东西。我错过了什么?

解决方案

这对我来说很难弄清楚,但这里是一个例子: b
$ b

  //摘自https://github.com/djangofan/WebDriverHandlingMultipleWindows 
package webdriver.test;
导入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})
公共类SuiteOne扩展MultiWindowUtils {

public static SiteServer fs;

@BeforeClass
public static void setUpSuiteOne(){
文件httpRoot = new File(build / resources / test);
System.out.println(服务器根目录是:+ httpRoot.getAbsolutePath());
int httpPort = Integer.parseInt(8080);
尝试{
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 ='测试MultiWindow'
projVersion ='1.0'
}

储存库{
mavenCentral()
}

依赖关系{
编译组:'org.seleniumhq.selenium',名称:'selenium-java',版本:'2. +'
编译组:'junit',名称:'junit',版本:'4. +'
编译组:'org.slf4j',名称:'slf4j-api',版本:'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。*'
包含'** / SuiteTwo.class'
reports.junitXml.destination =$ buildDir / test-results / SuiteTwo)
reports.html .destination =$ buildDir / test-results / SuiteTwo)
}


I am trying to migrate from Ant build to Gradle in my project. There are a bunch of test cases (subclasses of junit.framework.TestCase) and few test suites (subclasses of junit.framework.TestSuite). Gradle automatically picked up all test cases(subclasses of junit.framework.TestCase) to be run, but not the suites (subclasses of junit.framework.TestSuite).

I probably could work around by calling ant.junit to run it. But, I feel there should be a native easy way to force gradle to pick them and run. I couldn't find anything in the document . Am I missing something?

解决方案

This was hard for me to figure out, but here is an example:

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

}

And a build.gradle similar to this:

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

这篇关于如何从Gradle运行Junit TestSuites?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆