Scalatest:应该是带有额外描述的匹配器吗?

标签 scala scalatest

我有一个 org.scalatest.FunSpec with org.scalatest.Matchers执行以下操作的测试,例如

val tol = 1e-10
val res = 1.000000000000001
val ref = 1.000000000000000
res should be (ref +- tol)

但是它在循环中以名称为键的多个案例中这样做,当然我不能更改测试代码的粒度,所以我得到了一个与这些名称相关联的值的集合。因此,对于上面的测试,我需要放置一个额外的上下文或额外的描述 name以反射(reflect)它适用于哪个名称。我需要类似的东西:
val name : String = ...
res should be (ref +- tol) for name

我无法使用 itdescribe在这一点上,因为他们已经在外面。

最佳答案

这实际上取决于您要做什么,您可能应该添加一个更完整的示例来说明您要实现的目标,但您可以使用 describe在循环。例如:

class TempTest extends FunSpec with Matchers {

  describe("Some example test") {

    (1 to 10).foreach { i => // your loop here

      describe(s"Scenario $i") {
        it("should be equal to itself") {
          i shouldBe i
        }
      }
    }
  }
}

更新:您可以使用 withClue为匹配器添加更多上下文,例如:
withClue("Some clarifying message") {
    i shouldBe 5
}

如果条件失败,这会将线索字符串添加到错误中。

关于Scalatest:应该是带有额外描述的匹配器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39829145/

相关文章:

ScalaTest:将命令行参数传递给 ScalaTest maven 目标

scala - gradle scalatest留在 “Discovery starting.”

python - 将软件转换为 FPGA 的 VHDL/Verilog 的更好平台

scala - 比较模型的同一性,但与变量?用减号构造?

scala - IntelliJ 显示有关依赖方法类型的类型不匹配错误,即使 sbt 编译良好(scala)

scala - Akka Actor : Remote actor exception "Futures timed out after"

scala - 有没有办法在 Scala 中模拟 Singleton 对象

eclipse - Eclipse 中的 ScalaTest 运行配置 : cannot find Suite Class

scala - 使用多态函数作为参数

java - Spark 中的关系转换