ScalaTest:对GeneratorDrivenPropertyChecks不运行任何测试

时间:2019-01-12 17:30:10

标签: scala scalatest scalacheck property-based-testing

我正在尝试使用ScalaTest和ScalaCheck设置基于属性的测试...并且基于输出,看来我成功了,但是它花费的时间太快了,据我通常的理解,ScalaCheck应该告知您如何可能进行了测试,在我的情况下,此信息不存在:

[IJ]sbt:algorithms2_1> testOnly *MedianOf3PartitioningProps
[info] Compiling 1 Scala source to /Users/vasile.gorcinschi/gitPerso/Algorithms/Chapter 2 Sorting/algorithms2_1/target/scala-2.12/test-classes ...
[warn] there was one deprecation warning; re-run with -deprecation for details
[warn] one warning found
[info] Done compiling.

[info] MedianOf3PartitioningProps:
[info] sort
[info] - should sort array of ints from 0 to 100
[info]   +  
[info] ScalaTest
[info] Run completed in 412 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1

这是测试班:

class MedianOf3PartitioningProps extends FlatSpec with Matchers with GeneratorDrivenPropertyChecks with Gens {

  private val medianOf3Partitioning = new MedianOf3Partitioning[Int]

  implicit override val generatorDrivenConfig: PropertyCheckConfiguration = PropertyCheckConfig(minSuccessful = 1, maxDiscarded = 500, workers = 1)

  behavior of "sort"

  it should "sort array of ints from 0 to 100" in {
    forAll(arraysGen){  a: Array[Int] =>
      info(s"${a.mkString(",")}")
    medianOf3Partitioning.sort(a) shouldEqual a.sorted }
  }
}

Gens特性是我的-仅包含Gen [Array [Int]]的定义:

trait Gens {

  val arraysGen: Gen[Array[Int]] = containerOf[Array, Int](
    chooseNum(Int.MinValue, Int.MaxValue) suchThat { _ < 100 }
  ).suchThat(_.length < 50)
}

我使用this source进行测试。以防万一,我提供了scalacheck和scalatest的版本(来自Dependencies.scala和build.sbt):

lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5"
...
libraryDependencies ++= {
      val scalaTestVersion = "3.0.5"
      val scalaCheckVersion = "1.14.0"
      Seq(scalaTest % Test,
        "org.scalatest" %% "scalatest" % scalaTestVersion % "test",
        "org.scalacheck" %% "scalacheck" % scalaCheckVersion % "test",
        "com.storm-enroute" %% "scalameter" % "0.9"
      )
    }

1 个答案:

答案 0 :(得分:0)

基于M. Odersky的“在Scala中编程”中的一个小示例,我从GeneratorDrivenPropertyChecks切换到更一般的PropertyChecks。我还发现了Gen[Array[Int]]的问题,所以我也不得不这么做。发布有效的解决方案(发现失败的案例)以防万一,这将对其他人有帮助:

Gens特性:

trait Gens {

  val minIntArraysGen: Gen[Array[Int]] = containerOf[Array, Int](Gen.chooseNum(0, 100))
}

基于属性的测试:

import ca.vgorcinschi.Gens
import org.scalatest.MustMatchers._
import org.scalatest.WordSpec
import org.scalatest.prop.PropertyChecks

class MedianOf3PartitioningProps extends WordSpec with PropertyChecks with Gens {

  "sort method" must {
    "sort any Int array" in {
      forAll (minIntArraysGen){ (a: Array[Int]) =>
        whenever(a.nonEmpty) {
          val maybeSorted = new MedianOf3Partitioning[Int].sort(a)
          maybeSorted must equal (a.sorted)
        }
      }
    }
  }
}