java 向 Gradle 添加额外的测试套件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16741004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:50:19  来源:igfitidea点击:

Adding an additional test suite to Gradle

javaunit-testingjunitintegration-testinggradle

提问by Mike Rylander

I am attempting to add Gradle (1.4) to an existing project that has multiple test suites. The standard unit test located in src/test/javaran successfully, but I am having trouble setting up a task to run the JUnit test located in src/integration-test/java.

我正在尝试将 Gradle (1.4) 添加到具有多个测试套件的现有项目中。位于 中的标准单元测试src/test/java成功运行,但我在设置任务以运行位于src/integration-test/java.

When I run gradle intTestI get several cannot find symbolerrors for classes in src/main. This leads me to believe that the dependencies are not set up correctly. How do I setup intTestso that it will run my JUnit integration tests?

当我运行时,gradle intTestcannot find symbolsrc/main. 这让我相信依赖关系设置不正确。我如何设置intTest才能运行我的 JUnit 集成测试?

build.gradle

构建.gradle

apply plugin: 'java'

sourceCompatibility = JavaVersion.VERSION_1_6

sourceSets {
    integration {
        java {
            srcDir 'src/integration-test/java'
        }
        resources {
            srcDir 'src/integration-test/resources'
        }
    }
}

dependencies {
    compile(group: 'org.springframework', name: 'spring', version: '3.0.7')

    testCompile(group: 'junit', name: 'junit', version: '4.+')
    testCompile(group: 'org.hamcrest', name: 'hamcrest-all', version: '1.+')
    testCompile(group: 'org.mockito', name: 'mockito-all', version: '1.+')
    testCompile(group: 'org.springframework', name: 'spring-test', version: '3.0.7.RELEASE')

    integrationCompile(group: 'junit', name: 'junit', version: '4.+')
    integrationCompile(group: 'org.hamcrest', name: 'hamcrest-all', version: '1.+')
    integrationCompile(group: 'org.mockito', name: 'mockito-all', version: '1.+')
    integrationCompile(group: 'org.springframework', name: 'spring-test', version: '3.0.7.RELEASE')
}


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

Details:Gradle 1.4

详细信息:Gradle 1.4

Solution:I had not set the compile classpath for the integration test source set (see below). In my I code I set the compile class path to sourceSets.test.runtimeClasspathso that I don't have the duplicate dependencies for "integrationCompile"

解决方案:我没有为集成测试源集设置编译类路径(见下文)。在我的 I 代码中,我将编译类路径设置为,sourceSets.test.runtimeClasspath以便我没有“integrationCompile”的重复依赖项

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

采纳答案by Rene Groeschke

the "integration" sourceSet has not configured its compile and runtime classpath. That's why it can't find the classes from your main sourceset. you can configure the compile and runtime classpath in the following way:

“集成”源集尚未配置其编译和运行时类路径。这就是为什么它无法从您的主要源集中找到类的原因。您可以通过以下方式配置编译和运行时类路径:

sourceSets {
    integTest {
        java.srcDir file('src/integration-test/java')
        resources.srcDir file('src/integration-test/resources')
        compileClasspath = sourceSets.main.output + configurations.integTest
        runtimeClasspath = output + compileClasspath
    }
}

回答by qwertzguy

In most cases you want to use the same dependencies as your unit tests as well as some new ones. This will add the dependencies of your unit tests on top of the existing ones for integration tests (if any).

在大多数情况下,您希望使用与单元测试相同的依赖项以及一些新的依赖项。这将在现有的集成测试(如果有)之上添加单元测试的依赖项。

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

回答by David

Another way:

其他方式:

test {
    exclude '**/*IntegrationTest*'
    ...
}

task testIntegration(type: Test) {
    include '**/*IntegrationTest*'
    ...
}