【发布时间】:2015-01-29 09:29:56
【问题描述】:
我正在尝试使用 ScalaTest 编写一个基本上声明“它不应该抛出异常,或者抛出可能的异常列表之一”的属性,它是 GeneratorDrivenPropertyChecks,它反过来使用 scalatest。问题是我无法将noException 与逻辑或结合起来,所以我能想到的最好的就是这个丑陋的测试:
it should "not throw unexpected exceptions" in {
forAll { (s: String) =>
try { parse(s) }
catch { case e:Throwable => e shouldBe a [ParsingFailedException] }
true shouldBe true // prevent compile error
}}
我希望看到的内容更像
it should "not throw unexpected exceptions" in {
forAll { (s: String) => {
(noException should Be thrownBy) or (a [ParsingFailedException] shouldBe thrownBy) { parse(s) }
}}
【问题讨论】:
-
这种差异是不确定的吗?因为否则我建议将您的测试分为两种情况:一种用于不应抛出异常的情况,另一种用于应抛出列表之一。通过这种方式,其他读者也更清楚地知道什么时候应该发生
标签: scala testing scalatest scalacheck property-based-testing