KotlinTest is a flexible and comprehensive testing tool for Kotlin.
Full documentation
For latest updates see Changelog
- Forum
- Stack Overflow (don't forget to use the tag "kotlintest".)
- Contribute
For latest updates see Changelog.
Write simple and beautiful tests with the StringSpec style:
class MyTests : StringSpec() {
init {
"length should return size of string" {
"hello".length shouldBe 5
}
}
}
You can choose the testing style that fits your needs.
Use property based testing to test your code with automatically generated test data:
class PropertyExample: StringSpec() {
init {
"String size" {
forAll { a: String, b: String ->
(a + b).length == a.length + b.length
}
}
}
Handle even an enormous amount of input parameter combinations easily with table driven tests:
class StringSpecExample : StringSpec() {
init {
"should add" {
val myTable = table(
headers("a", "b", "result"),
row(1, 2, 3),
row(1, 1, 2)
)
forAll(myTable) { a, b, result ->
a + b shouldBe result
}
}
}
}
Testing for exceptions is easy with KotlinTest:
val exception = shouldThrow<IllegalAccessException> {
// code in here that you expect to throw an IllegalAccessException
}
exception.message should startWith("Something went wrong")
You can specify the number of threads, invocations, and a timeout for each test or for all tests. And you can group tests by tags or disable them conditionally. All you need is config
:
class MySpec : StringSpec() {
override val defaultTestCaseConfig = TestCaseConfig(invocations = 3)
init {
"should use config" {
// ...
}.config(timeout = 2.seconds, invocations = 10, threads = 2, tags = setOf(Database, Linux))
}
}
This page gives you just a short overview over KotlinTest. There are some more useful things:
- Check whole collections with Inspectors.
- Write elegant conditions with the matcher DSL:
"hello" should haveSubstring("ell")
. - Reuse test logic, e. g. for setup or tear down, with Interceptors.
- Let KotlinTest close resources automatically:
val reader = autoClose(StringReader("xyz"))
- Test asynchronous code with
eventually
.
See full documentation.
Gradle:
testCompile 'io.kotlintest:kotlintest:xxx'
Maven:
<dependency>
<groupId>io.kotlintest</groupId>
<artifactId>kotlintest</artifactId>
<version>xxx</version>
<scope>test</scope>
</dependency>