将单元测试 运行 改为集成测试

Making unit test run as integration test instead

我有一些项目同时进行了单元测试和集成测试。我将它们分开,以便单元测试 运行 作为常规构建的一部分,而集成测试仅 运行 通过特定的 "integTest" 任务。一切正常。

我有另一个项目,我没有写,也不能重构,它有一个单元测试,但实际上不是单元测试。我有这个项目的库存 Gradle 构建脚本,但我想将我在其他项目中放入的部分添加到 运行 此测试中作为集成测试。我还没有这样做,但我认为我在其他项目中所做的只会起到一半的作用。我确定它会让我 运行 作为集成测试进行测试,但我还不知道如何使其不 运行 作为单元测试。

一个测试在 "src/test/java" 中,我现在将其与我的 "integTest" 任务相关联(我之前使用 "src/integTest/groovy",我想我可以添加 "src/integTest/java"也)。如何删除默认 "test" 任务考虑的目录,以便 "test" 永远不会 运行 进行任何测试?

更新:

虽然这篇帖子的标题是关于运行将单元测试作为集成测试,但我真的只需要知道如何从单元测试通过中排除现有测试,这已得到解答。

有人看到这个标题可能想知道如何做到这一点,所以我将添加我如何做到这一点的细节。

以下显示了我添加到构建脚本中以实现此目的的所有内容:

// Without refactoring the source code of the project, this excludes the one test that looks like a unit test, but is
// actually an integration test.
test { exclude '**/*.*' }

sourceSets {
    integTest {
        java.srcDir file("src/test/java")
        resources.srcDir file("src/test/resources")
        runtimeClasspath = output + compileClasspath
    }
}

dependencies {
    integTestCompile sourceSets.main.output
    integTestCompile configurations.testCompile
    integTestCompile sourceSets.test.output
    integTestRuntime configurations.testRuntime
}

task integTest(type: Test) {
    testClassesDir  = sourceSets.integTest.output.classesDir
    classpath       = sourceSets.integTest.runtimeClasspath
    // This forces integration tests to always run if the task is run.
    outputs.upToDateWhen { false }
}

在你的build.gradle中你可以做:

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

或者通过添加行 test.enabled = false

来禁用 test 任务