如何在Scalatest FlatSpec中使用scalacheck道具生成器 [英] How to use scalacheck prop generators in scalatest FlatSpec

查看:258
本文介绍了如何在Scalatest FlatSpec中使用scalacheck道具生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use the scalacheck property generators in a scalatest.FlatSpec test file.

测试应该失败,并由junit框架(在我的情况下为eclipse)报告,但测试通过和错误仅显示在控制台中.

The test should fail and be reported by junit framework (and eclipse in my case) but the test pass and error is just displayed in console.

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框架中.

I was expecting that the error is bubbled up to the junit framework.

我具有以下依赖性:

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

You should use scalatest.prop.Checkers that is different than the scalacheck.Prop.check

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 ***

这篇关于如何在Scalatest FlatSpec中使用scalacheck道具生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆