9

I'm attempting to set up a testing framework for Spark jobs. I'd like to use spark-testing-base's SharedSparkContext trait which relies on ScalaTest's BeforeAndAfterAll trait to manage setup and tear-down. Something about my current environment is causing the beforeAll and afterAll methods to be called around each test case.

(Even if I wanted to permit this redundant behavior, I couldn't: I don't know how to tear down my HiveContext object properly, so the second call to beforeAll throws an exception that bottoms out at "ERROR XSDB6: Another instance of Derby may have already booted the database /Users/applemacbookpro/git/my-project/metastore_db.")

I'm using IntelliJ IDEA with an SBT-managed build.

  • MacOS 10.11.4
  • IntelliJ IDEA 2016.1.3
  • not sure about SBT version, should be recent
  • ScalaTest 2.2.6

Per the README of spark-testing-base and this question, I've put

parallelExecution in Test := false 

in build.sbt.

Here's my example:

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

I trigger it by right-clicking in the editor window and running from the context menu; the output is:

/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
Community
  • 1
  • 1
Cyan
  • 135
  • 1
  • 10
  • Was unable to reproduce with Windows-7 / IntelliJ-2016.2 / SBT-0.13.12 / ScalaTest-2.2.6 for both Scala-2.10 and Scala-2.11. – heenenee Aug 02 '16 at 19:25

1 Answers1

3

I think it's and Intellij/Scalatest bug.

I can reproduce your case, when right-clicking in the "Editor" window.

But if you right click on your class in the "Project" window and then run from the context menu, it works as expected :

in beforeAll
test 1
test2
in afterAll

When right-clicking in the editor window, Intellij seems to instantiate 2 runners, one for each test method. You can see in the "Run/Debug" window that ExampleSpec class appears several times instead of once.

L. CWI
  • 952
  • 9
  • 15
  • Oh wow, I gave up on that and wrote a kludge to get around the problem months ago. (The kludge is to do the setup in an object; first-class singletons for the win, I guess.) – Cyan Mar 23 '17 at 19:34