【问题标题】:Gradle can't find tests in additional source setGradle 在其他源集中找不到测试
【发布时间】:2019-09-22 02:39:52
【问题描述】:

我的目标是为集成测试设置一个源集。我创建了一个名为“intTest”的源集。我在里面放了一个简单的测试:

package com.github.frozensync;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class Application {

    @Test
    @DisplayName("should work")
    void foo() {
        assertEquals(2, 2);
    }
}

当我尝试运行它时,我收到以下错误:

FAILURE:构建失败并出现异常。
* 出了什么问题:
任务“:integrationTest”执行失败。
> 未找到给定包括的测试:[com.github.frozensync.Application](filter.includeTestsMatching)
* 试试:
使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。运行 --scan 以获得完整的见解。
* 在https://help.gradle.org获得更多帮助
0ms 内构建失败

这是我的项目结构:

这是我的build.gradle

plugins {
    id 'java'
}

group 'com.github.frozensync'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

sourceSets {
    intTest {
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
    }
}

idea {
    module {
        testSourceDirs += sourceSets.intTest.java.srcDirs
        testResourceDirs += sourceSets.intTest.resources.srcDirs
    }
}

configurations {
    intTestImplementation.extendsFrom implementation
    intTestRuntimeOnly.extendsFrom runtimeOnly
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
    intTestImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
}

test {
    useJUnitPlatform()
}

task integrationTest(type: Test) {
    description = 'Runs integration tests.'
    group = 'verification'

    testClassesDirs = sourceSets.intTest.output.classesDirs
    classpath = sourceSets.intTest.runtimeClasspath
    shouldRunAfter test
}

check.dependsOn integrationTest

我关注了guide by Gradle。当我遇到这个问题时,我尝试了以下方法:
- 运行 ./gradlew cleanIntegrationTest integrationTest 绕过 IntelliJ,但它仍然运行 0 个测试
- 添加the idea Gradle plugin.
- 从this 添加dependsOn
- 来自this 的解决方案。

如何让 Gradle 发现源集“intTest”中的测试?

【问题讨论】:

    标签: java gradle junit5 source-sets


    【解决方案1】:

    您的测试是 JUnit 5 测试,但您没有告诉 Gradle。就像你需要调用的测试任务一样

    useJUnitPlatform()
    

    integrationTest 任务的配置中。

    【讨论】:

    • 琐碎的评论/添加,但我解决了这个问题,以避免指定两次(只有在对其他人有帮助时才分享。tasks.withType(Test) { useJUnitPlatform() }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2013-07-05
    相关资源
    最近更新 更多