【问题标题】:Run all tests without the @RunWith(classOf[JUnitRunner]) dance?在没有 @RunWith(classOf[JUnitRunner]) 舞蹈的情况下运行所有​​测试?
【发布时间】:2013-02-10 14:17:28
【问题描述】:

有什么方法可以设置 IntelliJ 的 JUnit“运行所有测试”命令来自动获取 Scala Specs2?即删除此 sn-p 中的样板注释:

@RunWith(classOf[JUnitRunner])
class MySpec extends Specification

不得不记住添加这个非常烦人。

我见过SpecificationWithJUnit,但这也有点不合时宜(并且与TestKit 不兼容)。我正在寻找 maven/sbt/intelliJ 方面的解决方案。

【问题讨论】:

    标签: scala junit intellij-idea specs2


    【解决方案1】:

    使用 Intellij,您不需要注释来执行您的规范,因为它有自己的 specs2 运行器。我正在使用 IntelliJ 123.100 和最新版本的 Scala 插件(我认为是 82),一切正常(this issue 除外)。

    【讨论】:

    • 这适用于单个测试,但不适用于“在...中运行所有测试”
    • 这也适用于我。我可以选择要执行的目录(比如'data')并右键单击“Run '!Specs2 in 'data'!'”(奇怪的标签,但它有效)。
    【解决方案2】:

    使用@RunWith(classOf[JUnitPlatform]) 代替JUnitRunner 也许这可以帮助你:

    import org.junit.jupiter.api.{DisplayName, Test}
    import org.junit.platform.runner.JUnitPlatform
    import org.junit.runner.RunWith
    import org.scalatest.{BeforeAndAfterAll, Matchers}
    import org.scalatest.junit.JUnitSuite
    
    @RunWith(classOf[JUnitPlatform])
    class Junit5Test  extends JUnitSuite with Matchers with BeforeAndAfterAll {
    
      override def beforeAll: Unit = { println(">>> beforeAll <<< ") }
    
      @Test
      @DisplayName("Example with JUnitSuite")
      @throws(classOf[RuntimeException])
      def throwsExceptionWhenCalled() {
        println("Запуск теста...")
        assertThrows[RuntimeException] { throw new RuntimeException }
      }
    
    }
    

    还有build.sbt文件:

    val junitJupiter  = "5.2.0"
    val junitPlatform = "1.2.0"
    libraryDependencies ++= Seq(
      "junit"              % "junit"                   % "4.12" % Test,
      "org.junit.jupiter"  % "junit-jupiter-api"       % junitJupiter % Test,
      "org.junit.jupiter"  % "junit-jupiter-engine"    % junitJupiter % Test,
      "org.junit.jupiter"  % "junit-jupiter-params"    % junitJupiter % Test,
      "org.junit.platform" % "junit-platform-launcher" % junitPlatform % Test,
      "org.junit.platform" % "junit-platform-engine"   % junitPlatform % Test,
      "org.junit.platform" % "junit-platform-runner"   % junitPlatform % Test,
    )
    

    【讨论】:

      猜你喜欢
      • 2023-01-09
      • 2010-12-06
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 2014-07-29
      • 2020-08-18
      相关资源
      最近更新 更多