简体   繁体   中英

How to run a Spark-Scala unit test notebook in Databricks?

I am trying to write a unit test code for my Spark-Scala notebook using scalatest.funsuite but the notebook with test() is not getting executed in databricks. Could you please let me know how can I run it?

Here is the sample test code for the same.

import org.apache.spark.sql.{Row, SparkSession}
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.Assertions._
import org.apache.spark.eventhubs._
import com.fasterxml.uuid.Generators
import org.apache.spark.sql.functions._
import org.apache.spark.sql.{DataFrame, Row, SparkSession, Column}

class ImsLoyaltyTest extends AnyFunSuite {
  test ("Date Format Checker") {
    val sampleIpData = Seq(
      Row("india","hello","05-03-2021","14","50"),
      Row("india","hello","15-06-2021","14","50"),
      Row("india","hello","05/06/2021","6100","50"),
      Row("india","hello","05-31-2021","14","50")
    )
    
    val sampleIpSchema = new StructType()
      .add("a", StringType)
      .add("b", StringType)
      .add("c", StringType)
      .add("d", StringType)
      .add("e", StringType)
    
    val sampleIpDF = spark.createDataFrame(spark.sparkContext.parallelize(sampleIpData), sampleIpSchema)
    
    assert (sampleIpDF.collectAsList() == sampleIpDF.collectAsList())
    
  }
}

You need to explicitly create the object for that test suite & execute it. In IDE you're relying on specific runner, but it doesn't work in the notebook environment.

You can use either the .execute function of create object ( docs ):

(new ImsLoyaltyTest).execute()

but it could be better to use the .run method of ScalaTest shell ( docs ) - you can control color output, run multiple tests, etc.:

import org.scalatest._

nocolor.durations.stats.run(new ImsLoyaltyTest)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM