scala - 如何在 scalatest FlatSpec 中使用 scalacheck prop 生成器

标签 scala generator scalatest scalacheck junit-runner

我正在尝试在 scalatest.FlatSpec 测试文件中使用 scalacheck 属性生成器。

测试应该失败并由 junit 框架(在我的例子中是 eclipse)报告,但测试通过和错误仅显示在控制台中。

import scala.collection.immutable.TreeSet
import org.junit.runner.RunWith
import org.raisercostin.namek.UnitSpec
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest._

@RunWith(classOf[JUnitRunner])
class SetsTest2 extends FlatSpec with Matchers 
     with OptionValues with Inside with Inspectors {
  import org.scalacheck.Prop
  "set intersection" should "be commutative" in {
    Prop.forAll { (l1: TreeSet[Int], l2: TreeSet[Int]) =>
      l1.intersect(l2) == l1.intersect(l1)
    }.check
  }
}

输出如下

Run starting. Expected test count is: 1
SetsTest2:
set intersection

! Falsified after 1 passed tests.
> ARG_0: TreeSet(0)
> ARG_0_ORIGINAL: TreeSet(1288089760)
> ARG_1: TreeSet()
> ARG_1_ORIGINAL: TreeSet(0)
- should be commutative
Run completed in 505 milliseconds.
Total number of tests run: 1
Suites: completed 1, aborted 0
Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
All tests passed.

我预计错误会冒泡到 junit 框架。

我有以下依赖项:

scalaVersion    = "2.10.4"
"junit" % "junit" % "4.10" % "test"
"org.scalatest" %% "scalatest" % "2.2.4" % "test"
"org.scalacheck" %% "scalacheck" % "1.12.2" % "test"

最佳答案

您应该使用与 scalacheck.P​​rop.check 不同的 scalatest.prop.Checkers

import scala.collection.immutable.TreeSet
import org.junit.runner.RunWith
import org.raisercostin.namek.UnitSpec
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest._
import org.scalatest.prop.Checkers

@RunWith(classOf[JUnitRunner])
class SetsTest2 extends FlatSpec with Matchers 
      with OptionValues with Inside with Inspectors with Checkers {
  import org.scalacheck.Prop
  "set intersection" should "be commutative" in {
    check(Prop.forAll { (l1: TreeSet[Int], l2: TreeSet[Int]) =>
      l1.intersect(l2) == l1.intersect(l1)
    })
  }
}

现在输出如下

Run starting. Expected test count is: 1
SetsTest2:
set intersection
- should be commutative *** FAILED ***
  GeneratorDrivenPropertyCheckFailedException was thrown during property evaluation.
   (SetsTest.scala:17)
    Falsified after 1 successful property evaluations.
    Location: (SetsTest.scala:17)
    Occurred when passed generated values (
      arg0 = TreeSet(0), // 1 shrink
      arg1 = TreeSet() // 1 shrink
    )
Run completed in 452 milliseconds.
Total number of tests run: 1
Suites: completed 1, aborted 0
Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0
*** 1 TEST FAILED ***

关于scala - 如何在 scalatest FlatSpec 中使用 scalacheck prop 生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29059484/

相关文章:

scala - 为什么使用 foldLeft 而不是程序版本?

ScalaTest、Mockito、Guice 和 PartialMocking

ScalaTest 测试序列是否已排序

python - 在代码高尔夫中耗尽生成器的最短代码

python - Keras 自定义生成器 TypeError : 'NoneType' object is not callable

python - 使用生成器对 BST 执行中序树遍历

scala - 覆盖助手默认超时

java - 在 Scala 中覆盖 ArrayBuffer.contains() 方法

java - App 引擎和 Scala

scala - 可以使用 sbt 访问非 scala github 存储库来读入 scala 项目吗?