简体   繁体   English

如何在相同的Gradle构建中运行JUnit5和JUnit4?

[英]How to run JUnit5 and JUnit4 in same Gradle build?

I read an answer about Maven but I was wondering how I would achieve this task in Gradle - Executing JUnit 4 and JUnit 5 tests in a same build . 我读了一篇关于Maven的答案,但我想知道如何在Gradle中完成这项任务 - 在同一版本中执行JUnit 4和JUnit 5测试

Currently my Gradle build only picks up tests with: import org.junit.jupiter.api.Test; 目前我的Gradle构建仅使用以下方法获取测试: import org.junit.jupiter.api.Test;

My problem is that I'm using @RunWith which needs JUnit4 to run but I want to execute it on a JUnit5 Vintage Engine. 我的问题是,我使用@RunWith这就需要JUnit4运行,但我要执行它在JUnit5老式发动机。

How do I make my build such that I can run JUnit4 and JUnit5 together. 如何使我的构建能够一起运行JUnit4和JUnit5。 Thanks. 谢谢。

Update: Now there is a native Mockito Junit Jupiter for JUnit 5 - https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter 更新:现在有一个用于JUnit 5的本机Mockito Junit Jupiter - https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter

The junit5-migration-gradle project demonstrates how to execute tests based on JUnit 5 using Gradle. junit5-migration-gradle项目演示了如何使用Gradle基于JUnit 5执行测试。 In addition, it showcases that existing JUnit 4 based tests can be executed in the same test suite as JUnit Jupiter based tests or any other tests supported on the JUnit Platform. 此外,它还展示了现有的基于JUnit 4的测试可以在与基于JUnit Jupiter的测试或JUnit平台支持的任何其他测试相同的测试套件中执行。

Basically it boils down to having both engines, Jupiter and Vintage, on the runtime class-path: 基本上它归结为在运行时类路径上同时具有引擎,Jupiter和Vintage:

dependencies {
    testCompile("org.junit.jupiter:junit-jupiter-api:5.2.0")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.2.0")
}

dependencies {
    testCompile("junit:junit:4.12")
    testRuntime("org.junit.vintage:junit-vintage-engine:5.2.0")
}

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

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