简体   繁体   English

Scalacheck忽略了提供的生成器

[英]Scalacheck is ignoring the provided generators

I'm trying to implement a simple property check but Scalacheck is ignoring my generators. 我正在尝试实现一个简单的属性检查,但是Scalacheck忽略了我的生成器。 What I'm doing wrong here? 我在这里做错了什么?

object AlgorithmTest extends Properties("Algorithm") {
  property("Test") = forAll (Gen.choose(0,10)) (n => n>=0 & n<10)
}

and this is the result in SBT 这是SBT的结果

[info] ! Algorithm.Test: Falsified after 12 passed tests. [info] >
ARG_0: -1 [error] Failed: : Total 1, Failed 1, Errors 0, Passed 0,
Skipped 0

It looks like the Shrink instance which is passed to the forAll method is not using the generator when searching for smaller counter-examples. 搜索较小的反示例时,传递给forAll方法的Shrink实例似乎未使用生成器。 If you change your property to: 如果您将财产更改为:

property("Test") = Prop.forAllNoShrink(Gen.choose(1, 10)) (n => n >= 0 && n < 10)

Then it should properly fail with: 然后它应该正确地失败,并带有:

[info] ! Algorithm.Test: Falsified after 7 passed tests.
[info] > ARG_0: 10
[error] Failed: : Total 1, Failed 1, Errors 0, Passed 0, Skipped 0

One way to visualize the Shrink values is to use the Prop.collect method: 可视化收缩值的一种方法是使用Prop.collect方法:

property("Test") = Prop.forAll(Gen.choose(1, 10)) { n =>
  Prop.collect(n) { n >= 0 && n < 10 }
}

Then the collected values look like: 然后收集的值如下所示:

[info] ! Algorithm.Test: Falsified after 40 passed tests.
[info] > ARG_0: -1
[info] > Collected test data: 
[info] 17% 3
[info] 17% 1
[info] 15% 6
[info] 12% 9
[info] 10% 2
[info] 10% 5
[info] 7% 4
[info] 7% 8
[info] 2% -1
[info] 2% 7

Where you can see that -1 has been used during the shrinking process. 可以看到在缩小过程中使用了-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM