Sharing elements between generated objects in ScalaCheck using nested forAll
最近开始使用 Scala 编码,我尝试编写一些基于属性的测试用例。在这里,我试图生成模拟我正在测试的系统的原始数据。目标是首先生成基本元素(ctrl 和 idz),然后使用这些值生成两个类(A1 和 B1),最后检查它们的属性。我首先尝试了以下 –
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import org.scalatest._
import prop._ import scala.collection.immutable._ import org.scalacheck.{Gen, Arbitrary} case class A( case class B( object BaseGenerators { trait Generators { val A1 = for { val B1 = for { } class Something extends PropSpec with PropertyChecks with Matchers with Generators{ property(“Controllers are equal”) { property(“IDs are equal”) { } |
在终端中运行 sbt test 给了我以下 –
1
2 3 4 5 6 7 8 9 10 |
[info] Something:
[info] – Controllers are equal [info] – IDs are equal *** FAILED *** [info] TestFailedException was thrown during property evaluation. [info] Message: 1.1794559135007427E-271 was not equal to 7.871712821709093E212 [info] Location: (testnew.scala:52) [info] Occurred when passed generated values ( [info] arg0 = A(ABC,1.1794559135007427E-271,-1.6982696700585273E-23), [info] arg1 = B(ABC,7.871712821709093E212,-8.820696498155311E234) [info] ) |
现在很容易看出为什么第二个属性失败了。因为每次我产生 A1 和 B1 我都会为 id 而不是为 ctrl 产生不同的值,因为它是一个常数。以下是我的第二种方法,其中,我创建嵌套 for-yield 来尝试实现我的目标 –
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
case class Popo(
controller: String, id: Double, someA: Gen[A], someB: Gen[B] ) trait Generators { class Something extends PropSpec with PropertyChecks with Matchers with Generators{ property(“Controllers are equal”) { property(“IDs are equal”) { |
在第二种方法中运行 sbt test 告诉我所有测试都通过了。
1
2 3 4 5 6 7 8 9 |
[info] Something:
[info] – Controllers are equal [info] – IDs are equal [info] ScalaTest [info] Run completed in 335 milliseconds. [info] Total number of tests run: 2 [info] Suites: completed 1, aborted 0 [info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0 [info] All tests passed. |
有没有更好/替代的方法来重现我想要的结果?嵌套 forAll 对我来说似乎相当笨拙。如果我的依赖图中有 R -> S -> … V -> W 用于共享元素的对象,那么我将不得不创建尽可能多的嵌套 forAll.
- 很难说出您的属性实际上是什么。例如,”IDs are equal” 似乎表示任何一对 A 和 B 都应该具有相同的 id,这似乎不是一个有意义的属性。我的猜测是您希望拥有一些具有 A 和 B 成员(而不是 Gen[A] 和 Gen[B])的数据类型,然后您的属性将与该数据类型的成员之间的关系有关。
- 好的,现在属性测试是一个占位符。我最终要测试的是对于 controller 和 id 相同的每一对随机生成的 A 和 B,函数 GaGa 是否满足有效属性。所以,从某种意义上说,我确实需要一个创建 Gen[A], Gen[B] 的数据类型来测试 GaGa 属性的有效性。
- 对不起,这对我来说仍然很不清楚。我认为您更有可能通过更完整和最小的示例获得有用的答案。
- 好的,我将在接下来的几个小时内编辑问题以更好地说明它。
我将在 Scalacheck 中给出答案。我知道 Scalatest 很受欢迎,但我发现将其包含在有关 Scalacheck 的问题中会分散注意力,尤其是当没有理由没有理由无法编写示例时。
您似乎想测试 A 和 B,但它们共享信息。表示该依赖关系的一种方法是您编写的 Popo 类。它既包含共享信息,也包含 A 和 B 的生成值。另一种选择是在类中生成 A 和 B 之间的共享值。
最简单的解决方案是成对生成 A 和 B(两个元组)。
不幸的是,有一些技巧可以让它发挥作用。您需要在 forAll 属性中使用 case 关键字。您不能为 Arbitrary 元组的 implicit 值提供证据,因此您必须在 forAll.
中明确指定元组的生成器
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import org.scalacheck.Gen
import org.scalacheck.Arbitrary import org.scalacheck.Prop import org.scalacheck.Prop.AnyOperators import org.scalacheck.Properties case class A( case class B( object BaseGenerators { object Generators { val genAB: Gen[(A,B)] = for { class Something extends Properties(“Something”) { property(“Controllers and IDs are equal”) = { |
关于您关于让对象共享信息的更广泛的问题,您可以通过编写带有函数参数的生成器来表示它。但是,它仍然需要嵌套的 forAll 生成器。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
object Generators {
val obj = BaseGenerators val genA = for { def genB(a: A) = for { // ! class Something extends Properties(“Something”) { implicit val arbA: Arbitrary[A] = Arbitrary { property(“Controllers and IDs are equal”) = { |
来源:https://www.codenong.com/56388415/