【问题标题】:how to create test suite as in junit 4 using Junit5如何在junit 4中使用Junit5创建测试套件
【发布时间】:2020-10-23 21:59:56
【问题描述】:

由于我们出于单元测试目的从 junit4 升级到 junit5,我无法找到在 junit5 中创建测试套件的解决方案,例如在 junit4 中: 下面是我们的junit4套件类:

@RunWith(Suite.class)
@SuiteClasses({ 
    ClassA.class, 
    ClassB.class, 
    ClassC.class 
    } )
public class TestSuite {

}

我搜索后尝试的代码如下:

@RunWith(JUnitPlatform.class)

@SelectClasses( { 
    OrderAnnotationAlphanumericExperiment.class, 
    orderAnnotationExperiment.class, 
    ParameterizedAnnotation.class 
    } )
public class TestSuite {
    

}

在寻找解决方案时,他们中的大多数都提供了使用相同的测试套件使用老式 api 运行它的解决方案,但我正在寻找的是在 junit 5 中创建测试套件。

很少有人建议 @ExtendsWith(SpringExtension.class),但它的文档也较少,无法找到在 junit 5 中创建套件的解决方案

我提到的一些博客/问题/网站:

Create TestSuite in JUnit5 (Eclipse) Are test suites considered deprecated in JUnit5? https://howtodoinjava.com/junit5/junit5-test-suites-examples/

有人帮我解决这个问题。

【问题讨论】:

  • 这有帮助吗:junit.org/junit5/docs/current/user-guide/…“@RunWith 不再存在;被@ExtendWith 取代。”
  • @ExtendWith(SpringExtension.class) @SelectClasses( { OrderAnnotationAlphanumericExperiment.class, orderAnnotationExperiment.class, ParameterizedAnnotation.class } ) public class TestSuite {} 现在我已经试过这个了! junit-jupiter-engine 的版本:5.5.0 我有以下依赖项仍然会引发错误:java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: org/junit/platform/engine/EngineDiscoveryListener
  • 我相信您拥有的第二个代码示例是正确的,根据文档:junit.org/junit5/docs/current/user-guide/… 但正如文档中所述:“使用 @RunWith(JUnitPlatform.class 注释的测试类和套件) 不能直接在 JUnit 平台上执行(或作为某些 IDE 中记录的“JUnit 5”测试)。此类类和套件只能使用 JUnit 4 基础架构执行。”
  • 这能回答你的问题吗? Are test suites considered deprecated in JUnit5?

标签: java spring-boot junit junit5 junit-runner


【解决方案1】:

其实很简单。以下将在目录foo.bar.tests 及其子目录中找到所有 JUnit 测试:

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.SuiteDisplayName;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages("foo.bar.test")
public class FullTestSuite 

}

Sec 4.4.4 of the User's Guide

【讨论】:

  • 这实际上是使用 JUnit 4 引擎运行测试,即您需要 org.junit.vintage:junit-vintage-engine 依赖项。
猜你喜欢
  • 1970-01-01
  • 2010-10-02
  • 2011-07-22
  • 2013-11-04
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 2017-07-14
  • 2020-02-27
相关资源
最近更新 更多