首页 > 解决方案 > gradle : java 测试运行了两次

问题描述

我正在制作一个java项目的骨架;gradle 构建文件有一个烦人的问题:测试运行了两次,一次是由任务“JUnitPlatformTest”,第二次是由任务“test”。

第一个似乎触发了第二个,所以我不能禁用它,我想保留第二个,因为它们之间有一点区别:第一个在控制台(intelliJ)中,第二个使用集成的智能窗口。

这是 gradle.build

buildscript {
    repositories {
        mavenCentral()
        mavenLocal()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.3'
        classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.8.2'
        classpath 'eu.appsatori:gradle-fatjar-plugin:0.3'
    }
}

plugins {
    id 'java'
    id 'edu.sc.seis.launch4j' version '2.4.4'
}

apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'javafx-gradle-plugin'
apply plugin: 'eu.appsatori.fatjar'

junitPlatform {
    platformVersion '1.0.3'
    reportsDir file('build/test-results/junit-platform')
    enableStandardTestTask true
    //show results summary even on success.
    details details.SUMMARY
    filters {
        tags {
            // Framework tests need to be run only when required to verify that this framework is still working.
            exclude "Framework"
        }
        includeClassNamePatterns '.*Test', '.*Tests'
    }
}



group 'lorry'
version '1'

sourceCompatibility = 1.8
//mainClassName="imports.ColorfulCircles"

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    def final junitVersion = "5.2.0"
    compile group: 'com.google.inject', name: 'guice', version: '4.1.0'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
    compile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junitVersion
    //compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.11.0'
    compile group: 'org.assertj', name: 'assertj-core', version: '3.9.0'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'

    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: junitVersion
    testCompile group: 'org.mockito', name: 'mockito-core', version: '2.7.22'

    testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitVersion

    compile 'org.hamcrest:hamcrest-all:1.3'

    testCompile "org.testfx:testfx-core:4.0.13-alpha"
    testCompile 'org.testfx:testfx-junit5:4.0.13-alpha'
    testRuntime 'org.testfx:openjfx-monocle:8u60-b27'

}

test {
    useJUnitPlatform()
    jvmArgs = [
            "-Dtestfx.robot=glass",
            "-Dtestfx.headless=true",
            "-Dprism.order=sw",
            "-Dprism.text=t2k",
            "-Dheadless.geometry=1920x1200-32"
    ]
}

test.dependsOn 'clean'

jfx {
    // minimal requirement for jfxJar-task
    mainClass = 'imports.ColorfulCircles'

    // minimal requirement for jfxNative-task
    vendor = 'lolveley'
}

jar {
    baseName = 'executable3'
    version =  ''
    manifest {
        attributes(
                'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
                'Main-Class': 'imports.ColorfulCircles'
        )
    }
}
launch4j {
    outfile='bibliotek-v3.exe'
    mainClassName = 'imports.ColorfulCircles'
    icon = "${projectDir}\\icons\\hands2.ico"
    copyConfigurable = project.tasks.fatJar.outputs.files
    jar = "lib/${project.tasks.fatJar.archiveName}"
    //headerType = "console"
    //jar = "${buildDir}\\productFatJar\\fat.jar"
}

junitPlatformTest {
        jvmArgs = [
                "-Dtestfx.robot=glass",
                "-Dtestfx.headless=true",
                "-Dprism.order=sw",
                "-Dprism.text=t2k",
                "-Dheadless.geometry=1920x1200-32"
        ]
    }

结果如下:

Testing started at 19:25 ...
19:25:01: Executing task 'test'...

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :clean
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :junitPlatformTest
constructeur appelé
Before all
Before each
my test 1
Before each
my test 2
This test method should be run
Test run finished after 3630 ms
[         4 containers found      ]
[         0 containers skipped    ]
[         4 containers started    ]
[         0 containers aborted    ]
[         4 containers successful ]
[         0 containers failed     ]
[         7 tests found           ]
[         0 tests skipped         ]
[         7 tests started         ]
[         0 tests aborted         ]
[         7 tests successful      ]
[         0 tests failed          ]
> Task :test
constructeur appelé
Before all
Before each
my test 1
Before each
my test 2
This test method should be run
BUILD SUCCESSFUL in 13s
5 actionable tasks: 5 executed
19:25:15: Task execution finished 'test'.

标签: testinggradleintellij-ideajunit5

解决方案


根据这个官方网站,...

JUnit Platform Gradle 插件已弃用

JUnit 团队开发的非常基本 junit-platform-gradle-plugin 的功能在 JUnit Platform 1.2 中已弃用,并将在 1.3 中停止使用。请切换到 Gradle 的标准测试任务。

因此,您应该从构建文件中删除此插件,并在必要时尝试将剩余设置移植到插件的test任务中。java


推荐阅读