简体   繁体   English

Gradle 无法同时在 java 和 groovy 中运行测试

[英]Gradle cannot run tests both in java and groovy

I'm facing an issue I cannot solve.我面临一个我无法解决的问题。 I have following code structure in my application:我的应用程序中有以下代码结构:

src
    - main
        - java
    - test
        - java
        - groovy

It's rather conventional.这是相当传统的。 Some tests are written in java using JUnit5 and some are written in groovy using Spock.一些测试使用 JUnit5 在 java 中编写,一些测试使用 Spock 在 groovy 中编写。 When I run gradlew test only tests in /src/test/groovy are run.当我运行gradlew test时,只运行/src/test/groovy中的测试。 There are no errors.没有错误。 Java test are simply not being run. Java 测试根本没有运行。 I added on purpose failing test to /src/test/java to check whether there's an issue with printing reports.我故意将失败测试添加到 /src/test/java 以检查打印报告是否存在问题。 The test is not failed, because whole package /src/test/java is ignored somehow.测试没有失败,因为整个 package /src/test/java 以某种方式被忽略了。 I tried to find solution on SO but none of the answeres helped me.我试图在 SO 上找到解决方案,但没有一个答案对我有帮助。 I messed around with ideas like:我搞砸了这样的想法:

sourceSets {
  test {
    java { srcDirs = "src/test/java" }
    groovy { srcDirs = "src/test/groovy" }
  }
}

That didnt help.那没有帮助。 Here are some snippets from my build.gradle以下是我的 build.gradle 的一些片段

Plugin:插入:

apply plugin: 'groovy'

Test dependencies:测试依赖:

testImplementation("org.springframework.boot:spring-boot-starter-test:2.1.4.RELEASE") {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation("org.junit.jupiter:junit-jupiter:5.5.2")
testImplementation(group: 'org.spockframework', name: 'spock-core', version: '1.3-groovy-2.5')
testImplementation(group: 'org.spockframework', name: 'spock-spring', version: '1.3-groovy-2.5')
testImplementation(group: 'org.codehaus.groovy', name: 'groovy-all', version: '1.3-groovy-2.5', ext: 'pom')

Test task configuration:测试任务配置:

test {
    useJUnit()
    failFast = true
    testLogging.showStandardStreams = true

    reports {
        junitXml.enabled = true
    }
    finalizedBy jacocoTestReport
    finalizedBy jacocoTestCoverageVerification
    finalizedBy check
    testLogging {
        events "PASSED", "STARTED", "FAILED", "SKIPPED"
    }
}

Do you have any ideas?你有什么想法?

Ok, I fixed this issue and just right after it I faced another one.好的,我解决了这个问题,就在它之后我遇到了另一个问题。 I'm posting this answer for future me or some fellow developers in troubles.我正在为未来的我或一些遇到麻烦的开发人员发布这个答案。

First of all I had to change useJUnit() to useJUnitPlatform() as suggested by Mike Hill.首先,我必须按照 Mike Hill 的建议将 useJUnit() 更改为 useJUnitPlatform() 。

That solved the first issue with java test not being run.这解决了 java 测试未运行的第一个问题。 The new problem that arose - junit tests were running extremely slow and some of them got randomly stuck.出现的新问题 - junit 测试运行速度极慢,其中一些随机卡住。 Every time I ran tests, one of them just froze and stayed that way for couple of minutes.每次我进行测试时,其中一个都会冻结并保持几分钟。 After that time OutOfMemory was thrown.在那之后,OutOfMemory 被抛出。 I do not know why, When running those test from IJ.我不知道为什么,从 IJ 运行这些测试时。 everything was running smooth.一切都很顺利。 I had to add this to build.gradle to fix the issue.我必须将此添加到 build.gradle 以解决此问题。

Test dependencies:测试依赖:

testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'

Also add this to test closure:还将其添加到测试闭包中:

test {
    useJUnitPlatform() {
            includeEngines 'junit-vintage'
            includeEngines 'junit-jupiter'
    }
}

I do not claim that this is proper solution.我并不声称这是正确的解决方案。 It was more of a guess after scrolling through the documentation.滚动浏览文档后,这更像是一种猜测。 I would not recommend copying it without further reading on differences between useJUnit() and useJUnitPlatform().如果不进一步阅读 useJUnit() 和 useJUnitPlatform() 之间的差异,我不建议复制它。 For me it did the trick and I didn't have more time do the research.对我来说,它成功了,我没有更多的时间做研究。

I have following configuration:我有以下配置:

testImplementation 'org.spockframework:spock-core:2.0-M1-groovy-2.5'
testImplementation 'org.spockframework:spock-junit4:2.0-M1-groovy-2.5'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
testImplementation 'junit:junit:4+'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.6.2'

And yes, in test task, I've used useJUnitPlatform()是的,在测试任务中,我使用了 useJUnitPlatform()

test {
        useJUnitPlatform()
        afterTest { desc, result ->
            def executionTime = result.endTime - result.startTime
            println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType} in ${executionTime} ms"
        }
        testLogging {
            showStandardStreams = true
        }
    }

Do not have to configure engine for junitTestPlatform My configuration works for junit 4 tests + spock tests.不必为 junitTestPlatform 配置引擎 我的配置适用于 junit 4 测试 + spock 测试。 (have no time not to upgrade -> junit 5). (没时间不升级 -> junit 5)。 I have junit 4 rules in my tests and they work properly with this configuration我的测试中有 junit 4 条规则,它们在此配置下正常工作

Documentation of junit 5 is very helpful: https://junit.org/junit5/docs/current/user-guide/#running-tests-build junit 5 的文档非常有用: https://junit.org/junit5/docs/current/user-guide/#running-tests-build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM