Skip to content

Instantly share code, notes, and snippets.

@Daenyth
Created December 20, 2018 14:22
Show Gist options
  • Save Daenyth/249952f96dd3ca2aff55a2dae17d19af to your computer and use it in GitHub Desktop.
Save Daenyth/249952f96dd3ca2aff55a2dae17d19af to your computer and use it in GitHub Desktop.
Save scalacheck generated data as json on failure in scalatest
class TestFailExample extends FlatSpec with GeneratorDriverPropertyChecks
it should "foo" in {
val genData: Gen[T]
IO(
forAll(genData) {
// test here
}
).onError {
case e: GeneratorDrivenPropertyCheckFailedException =>
val data = e.args.head
.asInstanceOf[Prop.Arg[T]]
.arg
val path = new File("test_fail_data.json").toPath
fs2.Stream
.emit(data.toJson.compactPrint) // spray, but could be anything
.covary[IO]
.through(fs2.text.utf8Encode)
.to(
fs2.io.file.writeAll[IO](path)
)
.compile
.drain
}
.unsafeRunSync()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment