Skip to content

Commit

Permalink
Merge pull request #195 from nevillelyh/neville/seed
Browse files Browse the repository at this point in the history
Report accurate ScalaCheck seed on failing test
  • Loading branch information
gabro authored Sep 9, 2020
2 parents 4dc7b16 + cfa00c5 commit 0c4ff57
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
14 changes: 10 additions & 4 deletions munit-scalacheck/shared/src/main/scala/munit/ScalaCheckSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,29 @@ trait ScalaCheckSuite extends FunSuite {

private def propToTry(prop: Prop, test: Test): Try[Unit] = {
import ScalaCheckTest._
def seed =
def makeSeed() =
scalaCheckTestParameters.initialSeed.getOrElse(
Seed.fromBase64(scalaCheckInitialSeed).get
)
val initialSeed = makeSeed()
var seed: Seed = initialSeed
val result = check(
scalaCheckTestParameters,
Prop(genParams => prop(genParams.withInitialSeed(seed)))
Prop { genParams =>
val r = prop(genParams.withInitialSeed(seed))
seed = seed.next
r
}
)
def renderResult(r: Result): String = {
val resultMessage = Pretty.pretty(r, scalaCheckPrettyParameters)
if (r.passed) {
resultMessage
} else {
val seedMessage = s"""|Failing seed: ${seed.toBase64}
val seedMessage = s"""|Failing seed: ${initialSeed.toBase64}
|You can reproduce this failure by adding the following override to your suite:
|
| override val scalaCheckInitialSeed = "${seed.toBase64}"
| override val scalaCheckInitialSeed = "${initialSeed.toBase64}"
|""".stripMargin
seedMessage + "\n" + resultMessage
}
Expand Down
23 changes: 23 additions & 0 deletions tests/shared/src/test/scala/munit/ScalaCheckInitialSeedSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package munit

import scala.collection.mutable
import org.scalacheck.Prop.forAll

final class ScalaCheckInitialSeedSuite extends ScalaCheckSuite {

// initial seed should be used for the first out of 100 `minSuccessfulTests` only
override val scalaCheckInitialSeed =
"9SohH7wEYXCdXK4b9yM2d6TKIN2jBFcBs4QBta-2yTD="
private val ints = mutable.Set.empty[Int]

property("generating int") {
forAll { (i: Int) =>
ints.add(i)
true
}
}

test("generated int are not all the same") {
assert(ints.size > 1)
}
}

0 comments on commit 0c4ff57

Please sign in to comment.