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.
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'))
Check Spock-Genesis documentation is here or see SamplesSpec for more examples
- Update dependencies
- Add support for using regular expressions for String generation. Thanks to Generex
- Using Groovy constructor selection for single arg Pojo construction
- improve toGenerator extension methods
- better error handling for POJO construction
- isFinite method to determine if generator will terminate
- switch to Iterable from Iterator
- improve performance of long generation using min and/or max
- improved documentation (thanks to @mariogarcia)
- @Iterations annotation
- updated dependencies
- ability to set the seed of random generators
- permute to generate combinations from composite generators
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).
- Spock Homepage -- http://spockframework.org
- GitHub -- https://github.com/Bijnagte/spock-genesis
- Generex -- https://github.com/mifmif/Generex