KotlinTest

Join the chat at https://gitter.im/kotlintest/lobby Build Status kotlintest by kotlintest GitHub license

KotlinTest is a flexible and comprehensive testing tool for Kotlin.
Full documentation

For latest updates see Changelog

Community

How to use

For latest updates see Changelog.

Test with Style

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.

Let the Computer Generate Your Test Data

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
      }
    }
}

Check all the Tricky Cases With Table Testing

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
       }
    }

  }
}

Test Exceptions

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")

Fine Tune Test Execution

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))
  }
}

And More …

This page gives you just a short overview over KotlinTest. There are some more useful things:

See full documentation.

Use

Gradle:

testCompile 'io.kotlintest:kotlintest:xxx'

Maven:

<dependency>
    <groupId>io.kotlintest</groupId>
    <artifactId>kotlintest</artifactId>
    <version>xxx</version>
    <scope>test</scope>
</dependency>
compile "io.kotlintest:kotlintest:2.0.7"

Related Libraries

expekt

BDD assertion library for Kotlin

Last updated 3 mins ago

assertk

assertions for kotlin inspired by assertj

Last updated 3 mins ago

hamkrest

Hamcrest for Kotlin

Last updated 3 mins ago

mockito-kotlin

Using Mockito with Kotlin

Last updated 3 mins ago

kluent

Fluent Assertion-Library for Kotlin

Last updated 3 mins ago