Hedgehog will eat all your bugs.
Clone or download

README.md

Build Status

Hedgehog will eat all your bugs.

Hedgehog is a modern property-based testing system, in the spirit of QuickCheck (and ScalaCheck). Hedgehog uses integrated shrinking, so shrinks obey the invariants of generated values by construction.

Current Status

This project is still in some form of early release. The API may break during this stage until (if?) there is a wider adoption.

Please drop us a line if you start using scala-hedgehog in anger, we'd love to hear from you.

Features

  • Integrated shrinking, shrinks obey invariants by construction.
  • Generators allow monadic effects.
  • Range combinators for full control over the scope of generated numbers and collections.
  • SBT test runner
  • Currently no external dependencies in the core module

Getting Started

SBT Binary Dependency

In your build.sbt you will unfortunately need to add a custom resolver. Hedgehog is released for every commit and so the "version" will be a git commit hash. You can find the bintray repository here.

val hedgehogVersion = "${COMMIT}"

libraryDependencies ++= Seq(
  "hedgehog" %% "hedgehog-core" % hedgehogVersion,
  "hedgehog" %% "hedgehog-runner" % hedgehogVersion,
  "hedgehog" %% "hedgehog-sbt" % hedgehogVersion
)

resolvers += Resolver.url("bintray-scala-hedgehog",
    url("https://dl.bintray.com/hedgehogqa/scala-hedgehog")
  )(Resolver.ivyStylePatterns)

SBT Source Dependency

This project can be added as an SBT subproject.

// This can also be a branch name, like 'master'`, if you want to live on the edge
val hedgehogVersion = "${COMMIT}"
val hedgehogUri = uri("https://github.com/hedgehogqa/scala-hedgehog.git#" + hedgehogVersion)

lazy val root =
  (project in file("."))
    .dependsOn(ProjectRef(hedgehogUri, "core"))
    .dependsOn(ProjectRef(hedgehogUri, "runner"))
    .dependsOn(ProjectRef(hedgehogUri, "sbt-test"))

NOTE: Depending on your scala version(s) SBT might not resolve.

SBT Testing

Scala Hedgehog comes with a very primitive runner interface, and supports the SBT testing extension.

testFrameworks := Seq(TestFramework("hedgehog.sbt.Framework"))

IntelliJ

The IntelliJ scala plugin only has hard-coded support for the most popular test frameworks. While Hedgehog is obviously not included in that list, an may never be, by extending the runner Properties tests can be run as an application (as Properties includes a handy main function). NOTE: This requires the test to be an object and not a class.

Example

See the examples module for working versions.

import hedgehog._
import hedgehog.runner._

object PropertyTest extends Properties {

  def tests: List[Prop] =
    List(
      Prop("reverse", testReverse)
    )

  def testReverse: Property[Unit] =
    for {
      xs <- Gen.alpha.list(Range.linear(0, 100)).forAll
      _ <- xs.reverse.reverse === xs
    } yield ()
}

Motivation

The background and motivation for Hedgehog in general is still best described by the original author in this excellent presenation:

A very quick summary is that the original QuickCheck and it's derivatives (like ScalaCheck) separate the generation of data from the shrinking, which results in something that cannot be composed easily. It turns out it's fairly simple to combine them in a single data-type.

If you've used ScalaCheck before, it's exactly the same as writing your normal Gen functions, but now those generated value will shrink without any extra information. Magic!

Design Considerations

As a general rule, the current Scala API is intended to be direct port of haskell-hedgehog, much like scalacheck was for QuickCheck. The idea being that people familiar with one of the libraries will be comfortable with the other. It also makes it easier not having to re-invent any wheels (or APIs). There will obviously be exceptions where Scala forces us to make a different trade-off, such as the Gen type alias of GenT to assist with type-inference.

Resources

Fortunately there is much in common across property-testing material, and as such the following are still relevant despite whatever language they are presented with.

Blogs

Presentations

Books

Alternatives

In Scala there are other property-testing alternatives: