spock-genesis
spock-genesis copied to clipboard
Mostly lazy data generators for property based testing using the Spock test framework
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.
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
- Update dependencies
0.3.0
- Add support for using regular expressions for String generation. Thanks to Generex
- Using Groovy constructor selection for single arg Pojo construction
0.4.0
- improve toGenerator extension methods
- better error handling for POJO construction
- isFinite method to determine if generator will terminate
0.5.0
- 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
0.6.0
- permute to generate combinations from composite generators
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
- Spock Homepage -- http://spockframework.org
- GitHub -- https://github.com/Bijnagte/spock-genesis
- Generex -- https://github.com/mifmif/Generex