Property-based testing library for Kotlin.
-
Test-engine agnostic (Mix property-based tests with example-based tests written for the engine of your choice)
-
Multiplatform
-
No reflection (better compile-time feedback and faster runtime)
-
Configurable built-in generators
-
Easy way to create and combine generators
Have a look at the documentation
The project is incubating and its API may change in the future.
Please give it a try and give feed back in the issues.
class PlusOperatorTest {
// Default config and generators
@Test
fun isCommutative() = forAll { x: Int, y: Int ->
x + y == y + x
}
// With a given number of iterations
@Test
fun isAssociative() = forAll(iterations = 1000) { x: Int, y: Int, z: Int ->
(x + y) + z == x + (y + z)
}
// Use a seed to get reproducible results (useful when investigating a failure in the CI for instance)
@Test
fun zeroIsNeutral() = forAll(seed = -4567) { x: Int ->
x + 0 == x
}
// Configure or use a custom generator
@Test
fun addNegativeSubtracts() = forAll(Generator.ints(min = 0), Generator.ints(max = 0)) { x, y ->
x + y <= x
}
// Create a custom generator
val customGenerator1 = Generator.create { rng -> CustomClass(rng.nextInt(), rng.nextInt()) }
// Combine generators
val customGenerator2 = Generator.ints().zip(Generator.ints()) { x, y -> CustomClass(x, y) }
}
Property based testing is great and very powerful. But despite the fact that many good libraries already exist, none of them fully fit my needs.
-
Are bound to a specific test-engine
-
Can only be used in Java module (not in Koltin multiplatform modules)
-
Relies on reflection, making them slower then it could be and make some error detectable only at runtime
-
Some of them also force the user to add unwanted dependencies in his/her classpath
-
Add https://dl.bintray.com/kwik/preview to your maven repositories
-
Add
com.github.jcornaz.kwik:kwik-core-jvm:<version>
as a dependency (replacingjvm
bycommon
in case of a common module, and replacing<version>
by the latest version (see above))
repositories {
maven { url = uri("https://dl.bintray.com/kwik/preview") }
}
dependencies {
// in a JVM module
testCompile("com.github.jcornaz.kwik:kwik-core-jvm:<version>")
// in a common (multiplatform) module
testCompile("com.github.jcornaz.kwik:kwik-core-common:<version>")
}