TestNg和Junit未在同一个gradle项目中运行

zfciruhq  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(130)

我在同一个gradle项目(Jenkins插件)中使用JUnit 4.13.1和TestNg 6.14.3。一个类有TestNg单元测试,另一个有JUnit单元测试。对我来说,这两个类不能一起工作。如果我在build.gradle中使用此配置,则只有JUnit可以工作

test {
    testLogging {
        showStandardStreams = true
    }

    doFirst {
        environment 'OUTPUT_DIR', project.buildDir
        systemProperty 'build.notifications.disabled', 'true'
    }
    useTestNG()
    useJUnit()
}

如果我使用下面的配置或删除useJUnit(),那么只有JUnit工作。

test {
    testLogging {
        showStandardStreams = true
    }

    doFirst {
        environment 'OUTPUT_DIR', project.buildDir
        systemProperty 'build.notifications.disabled', 'true'
    }
    useJUnit()
}

如果我只保留useTestNG(),那么只有TestNG可以工作。在同一个gradle项目中运行这两个函数的正确配置是什么?

Execution failed for task ':test'.
> No tests found for given includes: [com.build.plugins.TestDataTest](--tests filter)
4c8rllxm

4c8rllxm1#

在概念上,这样写是错误的

test {
    ...
    useTestNG()
    useJUnit()
}

因为一个测试只能使用一个测试框架。因此在这种情况下,JUnit会覆盖TestNG。

JUnit 5是此案例的解决方案。

为什么?
JUnit平台是在JVM上启动测试框架的基础,它还定义了用于开发在平台上运行的测试框架的TestEngine API
以及

  1. JUnit 4测试可以使用junit-vintage-engine运行
  2. Test Engine for TestNG已经可用。幸运的是它支持TestNg 6.14.3。
    怎么做?
    build.gradle设置为
dependencies {
    ...
    testImplementation('org.junit.jupiter:junit-jupiter-api:5.9.0')
    testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.9.0')
    // For Junit 4
    testRuntimeOnly('org.junit.vintage:junit-vintage-engine:5.9.0')
    testImplementation('junit:junit:4.13.1')
    // For testng
    testRuntimeOnly("org.junit.support:testng-engine:1.0.4")
    testImplementation('org.testng:testng:6.14.3')
}

test {
   // use Junit 5
   useJUnitPlatform()
}

有用资源:JUnit 5第三方扩展Gradle Testing in Java & JVM projects

相关问题