首页 > 解决方案 > sbt 测试未通过我的 spark 测试套件,而 intellij 测试有效

问题描述

我正在尝试测试一个吃和处理 DataFrames 的类的行为。

继之前的问题之后:如何在 Spark 2.0+ 中编写单元测试?我尝试使用贷款模式以下列方式运行我的测试:我有一个 SparkSession 提供者特征:

/**
  * This trait allows to use spark in Unit tests
  * https://stackoverflow.com/questions/43729262/how-to-write-unit-tests-in-spark-2-0
 */
trait SparkSetup {

  def withSparkSession(testMethod: SparkSession => Any) {
    val conf = new SparkConf()
      .setMaster("local")
      .setAppName("Spark test")
    val sparkSession = SparkSession
      .builder()
      .config(conf)
      .enableHiveSupport()
      .getOrCreate()
    try {
      testMethod(sparkSession)
    }
    // finally sparkSession.stop()
  }
}

我在我的测试课中使用:

class InnerNormalizationStrategySpec
  extends WordSpec
  with Matchers
  with BeforeAndAfterAll
  with SparkSetup {
 ...
 "A correct contact message" should {
    "be normalized without errors" in withSparkSession{ ss => {

      import ss.implicits._

      val df = ss.createDataFrame(
        ss.sparkContext.parallelize(Seq[Row](Row(validContact))),
        StructType(List(StructField("value", StringType, nullable = false))))

      val result = target.innerTransform(df)

      val collectedResult: Array[NormalizedContactHistoryMessage] = result
        .where(result.col("contact").isNotNull)
        .as[NormalizedContactHistoryMessage]
        .collect()

      collectedResult.isEmpty should be(false) // There should be something
      collectedResult.length should be(1) // There should be exactly 1 message...
      collectedResult.head.contact.isDefined should be(true) // ... of type contact.
    }}
  }
 ...
}

当尝试使用 IntelliJ 工具运行我的测试时,以这种方式编写的所有测试都可以工作(一次运行 Spec 类),但是,sbt test来自终端的命令会使所有测试失败。

我还以为是因为并行性,所以我添加了

concurrentRestrictions in Global += Tags.limit(Tags.Test, 1)

在我的 sbt 设置中,但没有用。

这是我收到的堆栈跟踪:https ://pastebin.com/LNTd3KGW

有什么帮助吗?

谢谢

标签: scalaapache-sparktestingsbt

解决方案


推荐阅读