Last active
November 6, 2023 17:55
-
-
Save khalidr/25811888ca0a4132192d56cebedabbc0 to your computer and use it in GitHub Desktop.
Shared SparkSession for tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.spark.SparkConf | |
import org.apache.spark.sql.SparkSession | |
import org.my.tests.SparkSpec.sparkSession | |
import org.scalatest.flatspec.FixtureAnyFlatSpec | |
import org.scalatest.matchers.should.Matchers | |
import org.scalatest.Outcome | |
trait SparkSpec extends FixtureAnyFlatSpec with Matchers { | |
type FixtureParam = SparkSession | |
// see https://www.scalatest.org/user_guide/sharing_fixtures#withFixtureOneArgTest for an explanation | |
def withFixture(test: OneArgTest): Outcome = { | |
val theFixture = sparkSession | |
try { | |
withFixture(test.toNoArgTest(theFixture)) // "loan" the fixture to the test | |
} | |
finally sparkSession.sqlContext.clearCache() // clean up | |
} | |
} | |
object SparkSpec { | |
private lazy val sparkSession = SparkSession | |
.builder() | |
.config( | |
new SparkConf() | |
.setMaster("local[*]") | |
.set("spark.sql.shuffle.partitions", "1") | |
) | |
.getOrCreate() | |
} | |
// Sample test | |
class MyTest1 extends SparkSpec { | |
it should "work" in {sparkSession => // get a handle to the "loaned" spark session | |
import sparkSession.implicits._ | |
List.empty[String].toDF.show | |
succeed | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment