Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to use ScalaTest's BeforeAndAfterAll trait with sbt and IntelliJ IDEA?

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
like image 825
Cyan Avatar asked Jul 29 '16 18:07

Cyan


1 Answers

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.

like image 55
L. CWI Avatar answered Oct 25 '22 17:10

L. CWI