News from KotlinTest

In the last weeks KotlinTest got some new and useful features:

Testing for Exceptions

val exception = shouldThrow<IllegalAccessException> {
  // code in here that you expect to throw an IllegalAccessException
}
exception.message should start with "Something went wrong"

Before and after each

override fun beforeEach() {
  println("Test starting")
}

override fun afterEach() {
  println("Test completed")
}

Test configuration

Each test case can be followed by a call to the config function.

To execute a test ten times with two threads you would write:

class MyTests : ShouldSpec() {
  init {

    should("return the length of the string") {
      "sammy".length shouldBe 5
      "".length shouldBe 0
    }.config(invocations = 10, threads = 2)

  }
}

You can tag tests to be able run them selectively:

...
}.config(tags = listOf("database", "linux"))

Timeouts are configured like this:

...
}.config(timeout = 2, timeoutUnit = TimeUnit.SECONDS)

To ignore a test, you set ignored to true:

...
}.config(ignored = true)

Future

Some more features like property based (and table driven) testing are on the roadmap.

1 Like

That looks great!

Table driven tests will be a good addition.

1 Like