2016-07-29 126 views
9

我正在嘗試爲Spark作業設置測試框架。我想使用spark-testing-base的SharedSparkContext特性,它依賴於ScalaTest的BeforeAndAfterAll特性來管理安裝和拆卸。關於我當前環境的一些事情正在導致在每個測試用例周圍調用beforeAll和afterAll方法。 (即使我想允許這種多餘的行爲,我不能:我不知道如何正確拆除我的HiveContext對象,所以第二次調用beforeAll時會拋出一個異常,它出現在「ERROR XSDB6 :Derby的另一個實例可能已經啓動了數據庫/ Users/applemacbookpro/git/my-project/metastore_db。「)用sbt和IntelliJ IDEA使用ScalaTest的BeforeAndAfterAll特質的正確方法是什麼?

我正在使用IntelliJ IDEA和SBT管理的構建。

  • 的MacOS 10.11.4
  • 的IntelliJ IDEA 2016年1月3日
  • 不知道SBT版本,應該是最近
  • ScalaTest 2.2.6

每火花測試的自述-base和this question,我把

parallelExecution in Test := false 

in 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") 
    } 
} 

我在編輯器窗口中單擊鼠標右鍵並從上下文菜單中運行觸發它;輸出是:

/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 
+0

對於Scala-2.10和Scala-2.11,無法使用Windows-7/IntelliJ-2016.2/SBT-0.13.12/ScalaTest-2.2.6進行復制。 – heenenee

回答

1

我認爲這是和Intellij/Scalatest的bug。

在右鍵單擊「編輯器」窗口時,我可以重現您的情況。

但是如果你就在「項目」窗口類單擊,然後從上下文菜單中運行,它按預期工作:

in beforeAll 
test 1 
test2 
in afterAll 

當右鍵單擊在編輯器窗口中的IntelliJ似乎實例化2個跑步者,每個測試方法一個。 您可以在「運行/調試」窗口中看到ExampleSpec類多次出現而不是一次。

+0

哦,哇,我放棄了這一點,並寫了一個kludge在幾個月前解決了這個問題。 (kludge是爲了在一個對象中進行設置;我猜是爲了勝利獲得一流的單身人士。) – Cyan

相關問題