scalacheck

Pattern for generating negative Scalacheck scenarios: Using property based testing to test validation logic in Scala

*爱你&永不变心* 提交于 2019-12-05 14:09:07
We are looking for a viable design pattern for building Scalacheck Gen (generators) that can produce both positive and negative test scenarios. This will allow us to run forAll tests to validate functionality (positive cases), and also verify that our case class validation works correctly by failing on all invalid combinations of data. Making a simple, parameterized Gen that does this on a one-off basis is pretty easy. For example: def idGen(valid: Boolean = true): Gen[String] = Gen.oneOf(ID.values.toList).map(s => if (valid) s else Gen.oneOf(simpleRandomCode(4), "").sample.get) With the above

Scalacheck won't properly report the failing case

六眼飞鱼酱① 提交于 2019-12-05 02:01:14
I've wrote the following spec "An IP4 address" should "belong to just one class" in { val addrs = for { a <- Gen.choose(0, 255) b <- Gen.choose(0, 255) c <- Gen.choose(0, 255) d <- Gen.choose(0, 255) } yield s"$a.$b.$c.$d" forAll (addrs) { ip4s => var c: Int = 0 if (IP4_ClassA.unapply(ip4s).isDefined) c = c + 1 if (IP4_ClassB.unapply(ip4s).isDefined) c = c + 1 if (IP4_ClassC.unapply(ip4s).isDefined) c = c + 1 if (IP4_ClassD.unapply(ip4s).isDefined) c = c + 1 if (IP4_ClassE.unapply(ip4s).isDefined) c = c + 1 c should be (1) } } That is very clear in its scope. The test passes successfully but

java.lang.IncompatibleClassChangeError: Implementing class with ScalaCheck and ScalaTest

女生的网名这么多〃 提交于 2019-12-03 23:23:23
问题 I'm facing a nasty exception when trying to write a test using ScalaCheck and ScalaTest. Here's my dependencies: libraryDependencies ++= Seq( "org.scalatest" %% "scalatest" % "2.2.6" % "test", "org.scalacheck" %% "scalacheck" % "1.13.0" % "test" ) Here's my test: import org.scalatest.PropSpec import org.scalatest.prop.Checkers class MyPropSpec extends PropSpec with Checkers { property("List.concat") { check((a: List[Int], b: List[Int]) => a.size + b.size == (a ::: b).size) } } When I try to

Make ScalaCheck tests deterministic

可紊 提交于 2019-12-03 00:27:26
I would like to make my ScalaCheck property tests in my specs2 test suite deterministic, temporarily, to ease debugging. Right now, different values could be generated each time I re-run the test suite, which makes debugging frustrating, because you don't know if a change in observed behaviour is caused by your code changes, or just by different data being generated. How can I do this? Is there an official way to set the random seed used by ScalaCheck? I'm using sbt to run the test suite. Bonus question: Is there an official way to print out the random seed used by ScalaCheck, so that you can

scalacheck case class random data generator

为君一笑 提交于 2019-11-30 11:43:33
I'm trying to generate random data with Scalacheck. I have a hierarchy of case classes with many properties. The only way I've found so far to populate the case classes is like this : case class Data(a: String, b: String, c: String) val genLigneDecompte: Gen[Data] = for { ag <- Gen.alphaStr bg <- Gen.alphaStr cg <- Gen.alphaStr } yield Data( a = ag, b = bg, c = cg ) For a case class with 10-20 properties it's pretty tedious. I was wondering if there was a way to automate it somehow... I am sure somebody will come up with a solution that abstracts over arity using shapeless . But there are some

Why do you need Arbitraries in scalacheck?

℡╲_俬逩灬. 提交于 2019-11-30 06:06:18
I wonder why Arbitrary is needed because automated property testing requires property definition, like val prop = forAll(v: T => check that property holds for v) and value v generator. The user guide says that you can create custom generators for custom types (a generator for trees is exemplified). Yet, it does not explain why do you need arbitraries on top of that. Here is a piece of manual implicit lazy val arbBool: Arbitrary[Boolean] = Arbitrary(oneOf(true, false)) To get support for your own type T you need to define an implicit def or val of type Arbitrary[T]. Use the factory method

scalacheck case class random data generator

狂风中的少年 提交于 2019-11-29 17:17:31
问题 I'm trying to generate random data with Scalacheck. I have a hierarchy of case classes with many properties. The only way I've found so far to populate the case classes is like this : case class Data(a: String, b: String, c: String) val genLigneDecompte: Gen[Data] = for { ag <- Gen.alphaStr bg <- Gen.alphaStr cg <- Gen.alphaStr } yield Data( a = ag, b = bg, c = cg ) For a case class with 10-20 properties it's pretty tedious. I was wondering if there was a way to automate it somehow... 回答1: I