0

I am trying to set-up a property based testing with ScalaTest and ScalaCheck ... and based on the ouput it seems that I am succeeding, but it takes too fast and from what I understand normally ScalaCheck should inform you about how may tests were run, in my case this information is absent:

[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

Here is the test class:

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

The Gens trait is mine - it only comprises definition for the Gen[Array[Int]]:

trait Gens {

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

I used this source for the test set-up. Just in case, I'm providing versions of scalacheck and scalatest (from Dependencies.scala and 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"
      )
    }
vasigorc
  • 882
  • 11
  • 22

1 Answers1

0

Based on the small example from M. Odersky's "Programming in Scala", I switched from GeneratorDrivenPropertyChecks to more general PropertyChecks. I've also discovered issues with my Gen[Array[Int]] so I had to tweek that too. Posting a solution that worked (discovered cases that fail) in case this will help anyone else:

Gens trait:

trait Gens {

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

The property based test:

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)
        }
      }
    }
  }
}
vasigorc
  • 882
  • 11
  • 22