我有一个 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
我无法使用
it
和 describe
在这一点上,因为他们已经在外面。
最佳答案
这实际上取决于您要做什么,您可能应该添加一个更完整的示例来说明您要实现的目标,但您可以使用 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/