如何在最新的 FlatSpec 中使用 scalacheck 道具生成器



我正在尝试在scalatest中使用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不同的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 ***

对于很多人来说,赖瑟科斯汀的回答应该足够好了。但是,我看到了一些最新版本的ScalaCheck和ScalaTest没有完全集成的问题,也许你想要一些新功能。

但是,使用像 sbt 这样的工具的好处之一是您可以同时运行两者。这可能不是最好的方法,但是您可以将FlatSpec测试放在一个文件中,将ScalaCheck Props放在另一个文件中,例如SetsTest2SetsProps2

然后,当您运行 sbt test 时,它应该运行所有测试并正确返回!为了验证,我在一个小应用程序中运行了一个故意错误的 ScalaCheck Prop,其中包含 33 个 FlatSpec 测试和 2 个 ScalaCheck Prop,并得到了

[info] ScalaTest
[info] Run completed in 2 seconds, 211 milliseconds.
[info] Total number of tests run: 33
[info] Suites: completed 8, aborted 0
[info] Tests: succeeded 33, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[error] Failed: Total 35, Failed 1, Errors 0, Passed 34
[error] Failed tests:
[error]         com.xxx.xxx.TestProps
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful 

相关内容

  • 没有找到相关文章

最新更新