添加多个源测试目录以进行测试

添加多个源测试目录以进行测试

本文介绍了添加多个源测试目录以进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下目录结构的项目:

I have a project with the following directory structure:

src/main/java
src/main/resources
src/test/java
src/test/resources

我要添加一个新文件夹integrationTest:

I want to add a new folder, integrationTest:

src/integrationTest/java
src/integrationTest/resources

我想使集成测试与单元测试完全分开的地方.我应该如何添加呢?在build.gradle中,我不确定如何指定一个新任务来选择要创建的文件夹并单独运行测试.

Where I want to keep integration tests totally separate from unit tests. How should I go about adding this? In the build.gradle, I'm not sure how to specify a new task that'd pick this folder build it and run the tests separately.

推荐答案

Gradle具有source sets的概念,这正是您在这里需要的.您可以在 Java插件文档中找到有关此文档的详细文档: https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_source_sets

Gradle has a concept of source sets which is exactly what you need here. You have a detailed documentation about that in the Java Plugin documenation here : https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_source_sets

您可以在build.gradle

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

这将自动创建新的配置 integrationTestCompileintegrationTestRuntime,可用于定义新的 Task integrationTests:

This will automatically create new configurations integrationTestCompile and integrationTestRuntime, that you can use to define an new Task integrationTests:

task integrationTest(type: Test) {
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
}

供参考:可以在这里找到完整的示例: https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-integration-testing/

For reference : a working full example can be found here : https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-integration-testing/

这篇关于添加多个源测试目录以进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:28