shakedzy shakedzy - 3 months ago 9
Scala Question

Scala: how can I perform actions when test are over?

I'm using

scalatest_2.11
version 2.2.1. I'm trying to write test that run on a SparkContext. How can I initiate a SparkContext when the tests begin, take this Sc thorugh all the tests and then stop it when they are all done?

(I know that it suppose to stop by itself, but I'sd still like to do it myself)

Answer

You can use BeforeAndAfterAll from ScalaTest. Define a base trait that starts and stops the SparkContext and use it with your other tests.

trait SparkTest extends BeforeAndAfterAll {
  self: Suite =>

  @transient var sc: SparkContext = _

  override def beforeAll {
    val conf = new SparkConf().
      setMaster("local[*]").
      setAppName("test")
    sc = new SparkContext(conf)
    super.beforeAll()
  }

  override def afterAll: Unit = {
    try {
      sc.stop()
    } finally {
      super.afterAll
    }
  }

}

// Mix-in the trait with your tests like below.
class MyTest extends FunSuite with SparkTest {
  test("my test") {
    // you can access "sc" here.
  }
}