Scalacheck 嵌套 forall 在 Scalatest 测试失败时抛出 "oneOf called on empty collection"
Scalacheck nested forall throws "oneOf called on empty collection" on failure in Scalatest test
我有一个嵌套的 forAll 调用,它取决于前一个调用生成的值。根据其生成器的定义,此值是一个不应为空的集合:
"test" in {
val listGen: Gen[List[Int]] = Gen.listOfN(3, Gen.choose(0, 100))
def intGen(list: List[Int]) = Gen.oneOf(list)
implicit val arbList = Arbitrary(listGen)
forAll { list: List[Int] =>
forAll(intGen(list)) { int =>
true should be(false)
}
}
}
而不是生成诸如 "true was not false" 之类的错误消息,这给了我:
IllegalArgumentException was thrown during property evaluation.
Message: oneOf called on empty collection
Occurred when passed generated values (
arg0 = List() // 2 shrinks
)
我不知道为什么。这不是我期望(或理解)的测试失败消息...
如果我打印出生成的列表和整数,我会得到以下奇怪的输出:
List(72, 77, 8)
8
4
2
1
0
List(72)
72
36
18
9
4
2
1
0
List()
整数在 属性 失败时除以二,因为列表被截断(但根据生成器定义,列表的大小应始终为 3!)
我正在使用 Scala 2.12.1、scalatest 3.0.1 abd scalacheck 1.13.4。
谢谢!
原来这个机制被称为 "shrinking" 并且可以通过将这些隐式添加到范围中来禁用:
def noShrink[T] = Shrink[T](_ => Stream.empty)
implicit val intListNoShrink = noShrink[List[Int]]
implicit val intNoShrink = noShrink[Int]
这个link帮了大忙:https://github.com/scalatest/scalatest/issues/584
我有一个嵌套的 forAll 调用,它取决于前一个调用生成的值。根据其生成器的定义,此值是一个不应为空的集合:
"test" in {
val listGen: Gen[List[Int]] = Gen.listOfN(3, Gen.choose(0, 100))
def intGen(list: List[Int]) = Gen.oneOf(list)
implicit val arbList = Arbitrary(listGen)
forAll { list: List[Int] =>
forAll(intGen(list)) { int =>
true should be(false)
}
}
}
而不是生成诸如 "true was not false" 之类的错误消息,这给了我:
IllegalArgumentException was thrown during property evaluation.
Message: oneOf called on empty collection
Occurred when passed generated values (
arg0 = List() // 2 shrinks
)
我不知道为什么。这不是我期望(或理解)的测试失败消息...
如果我打印出生成的列表和整数,我会得到以下奇怪的输出:
List(72, 77, 8)
8
4
2
1
0
List(72)
72
36
18
9
4
2
1
0
List()
整数在 属性 失败时除以二,因为列表被截断(但根据生成器定义,列表的大小应始终为 3!)
我正在使用 Scala 2.12.1、scalatest 3.0.1 abd scalacheck 1.13.4。
谢谢!
原来这个机制被称为 "shrinking" 并且可以通过将这些隐式添加到范围中来禁用:
def noShrink[T] = Shrink[T](_ => Stream.empty)
implicit val intListNoShrink = noShrink[List[Int]]
implicit val intNoShrink = noShrink[Int]
这个link帮了大忙:https://github.com/scalatest/scalatest/issues/584