【问题标题】:How to use scalacheck prop generators in scalatest FlatSpec如何在 scalatest FlatSpec 中使用 scalacheck 道具生成器
【发布时间】:2015-05-17 13:02:56
【问题描述】:

我正在尝试在 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"

【问题讨论】:

    标签: scala generator scalatest scalacheck junit-runner


    【解决方案1】:

    对于很多人来说,raisercostin 的回答应该已经足够好了。但是,我看到了一些问题,即最新版本的 ScalaCheck 和 ScalaTest 没有完全集成,也许您需要一些新功能。

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

    然后,当您运行sbt test 时,它应该只运行您的所有测试并正确返回!为了验证,我在一个包含 33 个 FlatSpec 测试和 2 个 ScalaCheck Prop 的小型应用程序中运行了一个故意错误的 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 
    

    【讨论】:

      【解决方案2】:

      您应该使用与 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 ***
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-21
        • 2017-01-14
        • 2014-12-29
        • 1970-01-01
        • 2016-05-20
        • 1970-01-01
        • 2021-08-29
        • 1970-01-01
        相关资源
        最近更新 更多