您可以在ScalaTest中多次运行测试套件吗



Spark ETL过程中有许多步骤,我创建了套件来测试每个步骤的功能,步骤的输出是下一步的输入。

然而,我想创建一个运行两次ETL过程的测试,例如:

原始数据>>ETL_Step_1>>ETL_Step_2>>ETL_Step_3>>增量数据>>ETL_Step_1>>ETL_Step_2>>ETL_Step_3

我从Reporter收到一个错误,因为它无法运行与以前运行过的名称相同的Suite(例如,我无法运行两次名为ETL_Step_1的Suite(。如果重复Suites并重命名重复项以执行以下操作,问题就会消失:

原始数据>>ETL_Step_1>>ETL_Step_2>>ETL_Step_3>>增量数据>>ETL_Step_1_2>>ETL_Step_2>>ETL_Step_3_2

有没有更好的方法可以在不复制和重命名测试套件的情况下运行我的测试?

您可以将所有ETL步骤放入Definition(集合(并运行它们

import org.scalatest.FunSpec
import com.github.mrpowers.spark.fast.tests.DataFrameComparer
import com.github.mrpowers.spark.daria.sql.SparkSessionExt._
import org.apache.spark.sql.types.{IntegerType, StringType}
describe("etl collection") {
it("can run etls that are organized in a map") {
val sourceDF = spark.createDF(
List(
("bob", 14),
("liz", 20)
), List(
("name", StringType, true),
("age", IntegerType, true)
)
)
val etlDefinition = new EtlDefinition(
name =  "example",
sourceDF = sourceDF, // etl step 1
transform = someTransform(), // etl step 2
write = someWriter(), // etl step 3
hidden = false
)

val etls = scala.collection.mutable.Map[String, EtlDefinition]("example" -> etlDefinition)
etls += ("ex2" -> etlDefinition)
etls("example").process()
}
}

另一个选项可能是scalatest中的eventually。最后的一个小例子:

Post("/something", body) ~> routes ~> check {
response should be a pendingResponse
eventually {
Post("/something", body) ~> routes ~> check {
response should be a expectedResponse
}
}
}

而不是Post,你可以调用一些方法。

你可以在这里测试

更新:

it ("your definition") {
eventually { Thread.sleep(50); step1() should be A // assertion }
eventually { Thread.sleep(50); step2() should be B }
eventually { Thread.sleep(50); step3() should be C }
}

如果你希望,你可以让它们嵌套

最新更新