Bijnagte / spock-genesis

Mostly lazy data generators for property based testing using the Spock test framework
MIT License
92 stars 15 forks source link

Spock Genesis

Mostly lazy data generators for property based testing using the Spock test framework

Providing test data, especially when attempting to test for a wide range of inputs is tedious if not impossible to do by hand. Generating inputs allows for more thorough testing without a dramatic increase in effort. In Spock [data driven tests] (http://spock-framework.readthedocs.org/en/latest/data_driven_testing.html#data-pipes) can have data provided by any Iterator. Spock Genesis provides a variety of classes that extend from Generator which meet that interface. Where possible the generators are lazy and infinite.

[build status]() codecov JCenter Maven Central

Usage

From gradle:

repositories {
    jcenter()
}

dependencies {
    testCompile 'com.nagternal:spock-genesis:0.6.0'
}

The primary way of constructing generators is spock.genesis.Gen which provides static factory methods for data generators.

@Unroll
def 'test reverse #string'() {
    when:
        def reversed = string.reverse()

    then:
        reversed.size() == string.size()
        if (string) {
            string.eachWithIndex { letter, i ->
                letter == reversed[-(i + 1)]
            }
        }
        reversed.reverse() == string

    where:
        string << Gen.these('', 'foo').then(Gen.string).take(10000)
}

Given a Person class create a generator that can supply instances:

    Gen.type(Person,
        id: Gen.integer(200..10000),
        name: Gen.string(~/[A-Z][a-z]+( [A-Z][a-z]+)?/),
        birthDate: Gen.date(Date.parse('MM/dd/yyyy','01/01/1940'), new Date()),
        title: Gen.these('', null).then(Gen.any('Dr.', 'Mr.', 'Ms.', 'Mrs.')),
        gender: Gen.character('MFTU'))

Documentation

Check Spock-Genesis documentation is here or see SamplesSpec for more examples

Change log

0.2.0

0.3.0

0.4.0

0.5.0

0.6.0

Building Spock Genesis

The only prerequisite is that you have JDK 7 or higher installed.

After cloning the project, type ./gradlew clean build (Windows: gradlew clean build). All build dependencies, including Gradle itself, will be downloaded automatically (unless already present).

Resources