使用sbt和IntelliJ IDEA使用ScalaTest的BeforeAndAfterAll特性的正确方法是什么?

Cya*_*yan 9 intellij-idea sbt scalatest

我正在尝试为Spark作业建立一个测试框架.我想使用spark-testing-base的SharedSparkContext特性,该特性依赖于ScalaTest的BeforeAndAfterAll特性来管理设置和拆除.关于我当前环境的一些事情导致在每个测试用例周围调用beforeAll和afterAll方法.

(即使我想允许这种冗余行为,我也不能:我不知道如何正确地拆除我的HiveContext对象,所以对beforeAll的第二次调用抛出了一个异常,它在"ERROR XSDB6:另一个Derby可能已经启动了数据库/ Users/applemacbookpro/git/my-project/metastore_db.")

我正在使用IntelliJ IDEA和SBT管理的构建.

  • MacOS 10.11.4
  • IntelliJ IDEA 2016.1.3
  • 不确定SBT版本,应该是最近的
  • ScalaTest 2.2.6

根据spark-testing-base的README和这个问题,我已经说过了

parallelExecution in Test := false 
Run Code Online (Sandbox Code Playgroud)

在build.sbt中.

这是我的例子:

import org.scalatest.{BeforeAndAfterAll, FlatSpec}

class ExampleSpec extends FlatSpec with BeforeAndAfterAll {
  override def beforeAll(): Unit = {
    println("in beforeAll")
    super.beforeAll()
  }

  override def afterAll() {
    println("in afterAll")
    super.afterAll()
  }

  behavior of "example"

  it should "succeed" in {
    println("test 1")
  }

  it should "succeed again" in {
    println("test2")
  }
}
Run Code Online (Sandbox Code Playgroud)

我通过右键单击编辑器窗口并从上下文菜单运行来触发它; 输出是:

/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/bin/java...
Testing started at 2:50 PM ...
in beforeAll
test 1
in afterAll
in beforeAll
test2
in afterAll

Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)

L. *_*CWI 3

我认为这是 Intellij/Scalatest 的错误。

在“编辑器”窗口中右键单击时,我可以重现您的情况。

但是,如果您在“项目”窗口中右键单击您的类,然后从上下文菜单运行,它将按预期工作:

in beforeAll
test 1
test2
in afterAll
Run Code Online (Sandbox Code Playgroud)

在编辑器窗口中右键单击时,Intellij 似乎会实例化 2 个运行程序,每个测试方法对应一个运行程序。您可以在“运行/调试”窗口中看到该类ExampleSpec出现多次而不是一次。