使用gradle在不同的命令下进行编译和测试

mutmk8jj  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(344)

我正在尝试做一个poc,用一种不同的目录方法来保存单元、集成验收测试等。我见过许多博客,人们在其中创建文件夹以及main和test,但我的意图不是隔离,而是将这些文件夹合并到test文件夹下。我想知道你对这种方法的看法,我不确定这是否有任何缺陷
对于上面的场景,我在spring引导应用程序的test文件夹下创建了三个文件夹,如下所示,用于保存单元测试、集成测试和验收测试。
我的渐变配置如下所示

sourceSets {
    test {
        java {
            srcDirs += ['src/test/integration/java', 'src/test/unit/java', 'src/test/acceptance/java']
        }
        resources {
            srcDirs += ['src/test/integration/resources', 'src/test/unit/resources', 'src/test/acceptance/resources']
        }
    }
}

上面的配置工作得很好,唯一的问题是我不知道我们如何遵守和建立文件使用

./gradle test -----------> builds unit, integration, acceptance tests & generates report 
./gradle unit -----------> builds only unit tests & generates report
./gradle integration ----> builds only integration tests & generates report
./gradle acceptance -----> builds only acceptance tests & generates report
t98cgbkg

t98cgbkg1#

我做的 gradle 两种情况的配置:分离和合并测试。您可以看到,在合并测试的情况下,配置非常简单 classpath 总是一个 src/test/java ,这种方法提供了对测试位置的相当简单的人类理解,对于隔离测试,配置很复杂,需要微调,如果没有测试名称模式,可能会出现类和资源冲突,这种情况对您将选择哪种类型的编写测试具有关键意义。
这是多测试文件夹的配置:

configurations {
    integrationImplementation.extendsFrom(testImplementation)
    integrationRuntimeOnly.extendsFrom(testRuntimeOnly)

    unitRuntimeOnly.extendsFrom(testRuntimeOnly)
    unitImplementation.extendsFrom(testImplementation)
}

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

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

test {
    useJUnitPlatform()

    testClassesDirs += sourceSets.integration.output.classesDirs
    testClassesDirs += sourceSets.unit.output.classesDirs
    classpath += sourceSets.integration.runtimeClasspath
    classpath += sourceSets.unit.runtimeClasspath
    beforeTest { descriptor ->
        logger.lifecycle("Running test: $descriptor")
    }

    testLogging {
        events "PASSED", "FAILED", "SKIPPED"
    }
}

task integration(type: Test) {
    useJUnitPlatform()

    testClassesDirs += sourceSets.integration.output.classesDirs
    classpath += sourceSets.integration.runtimeClasspath
    beforeTest { descriptor ->
        logger.lifecycle("Running test: $descriptor")
    }

    testLogging {
        events "PASSED", "FAILED", "SKIPPED"
    }
}

task unit(type: Test) {
    useJUnitPlatform()

    testClassesDirs = sourceSets.unit.output.classesDirs
    classpath = sourceSets.unit.runtimeClasspath
    beforeTest { descriptor ->
        logger.lifecycle("Running test: $descriptor")
    }

    testLogging {
        events "PASSED", "FAILED", "SKIPPED"
    }
}

如果所有测试类型都有一个文件夹,则配置为:

task integrationSingleFolder(type: Test) {
    filter {
        includeTestsMatching "*IT"
    }

    useJUnitPlatform()

    testLogging {
        events "PASSED", "FAILED", "SKIPPED"
    }
}

task unitSingleFolder(type: Test) {
    filter {
        includeTestsMatching "*Test"
    }

    useJUnitPlatform()

    testLogging {
        events "PASSED", "FAILED", "SKIPPED"
    }
}
e0uiprwp

e0uiprwp2#

你可以在你的系统中创建新的渐变任务 build.gradle 从原始扩展 test 任务并配置相应的src和资源目录。像这样的

task unit(type: Test) {
  // your configuration
  include 'org/foo/unit/**'
}

使 test.DependsOn(unit) 执行 test 任务。

相关问题