junit-jupiter-api 和 junit-jupiter-engine 的区别

Difference between junit-jupiter-api and junit-jupiter-engine

maven 模块 junit-jupiter-apijunit-jupiter-engine 有什么区别?是否有必要在 build.gradle 中包含两个依赖项?

我需要同时提供这两个依赖项吗?

testCompile("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
testCompile("org.junit.jupiter:junit-jupiter-api:${junitVersion}")

或者只有一个依赖就够了?

testCompile("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")

我需要添加对 junit-vintage-engine 的依赖吗?

JUnit 5.4 之前的版本

来自 the docs:

junit-jupiter-api

JUnit Jupiter API for writing tests and extensions.

junit-jupiter-engine

JUnit Jupiter test engine implementation, only required at runtime.

junit-vintage-engine

JUnit Vintage test engine implementation that allows to run vintage JUnit tests, i.e. tests written in the JUnit 3 or JUnit 4 style, on the new JUnit Platform.

所以...

  • 您需要 junit-jupiter-apijunit-jupiter-engine 来编写和 运行 JUnit5 测试
  • 如果 (a) 您是 运行 JUnit5 (b) 您的测试用例使用 JUnit4 constructs/annotations/rules 等,则您只需要 junit-vintage-engine

JUnit 从 5.4 版开始

在 JUnit 5.4 中,这得到了简化,有关详细信息,请参阅

请注意,junit-jupiter-api 作为子依赖包含在 junit-jupiter-engine Maven 存储库中。因此,您实际上只需要添加 junit-jupiter-engine 即可获得两者。我确定 gradle 是一样的。 https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine/5.1.1

对于您的问题,最准确的答案在 junit-team/junit5-samples repository. Just take a look at junit5-jupiter-starter-gradle for Gradle and junit5-jupiter-starter-maven for maven 中。

如您在两个示例中所见,唯一需要的依赖项是 junit-jupiter

junit-jupiter 聚合神器

JUnit 5.4 provides much simpler Maven configuration if your intent is to write JUnit 5 tests. Simply specify the aggregate artifact named junit-jupiter.

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.8.1</version>
    <scope>test</scope>
</dependency>

这个神器作为一个集合,依次自动拉取以下三个神器,方便大家使用:

在您的项目中,您还将得到:

  • junit-platform-commons-1.4.0.jar
  • junit-platform-engine-1.4.0.jar

以上是您需要根据新的 Jupiter 范例编写和运行 JUnit 5 测试

遗留测试

如果您的项目有 JUnit 3 或 4 测试,您希望继续 运行,为 JUnit Vintage Enginejunit-vintage-engine. See tutorial by IBM 添加另一个依赖项.

<!-- https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine -->
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.8.1</version>
    <scope>test</scope>
</dependency>

如果您想 运行 使用 Junit 5 进行测试,您只需要 junit-jupiter-api。如果您同时使用 junit-jupiter-engine,您的测试将会失败,而不会 运行。

我不确定为什么要放置它,但是,之前的答案告知您需要同时使用这两者是不正确的。