Writing Data-Driven Tests

We'll cover the following

Good tests are FAIR—fast, automated, independent, and repeatable. If tests aren’t independent of each other, then they have to run in a specific order. And by adding or removing a test, we may break the order and the tests may fail. Such tests will become expensive and hard to maintain.

One way to guarantee independence of tests is to never place multiple asserts in the same test for verifications that are independent of each other. The obvious benefit of this approach is that tests can evolve without affecting and without being affected by existing tests. The downside, though, is that we end up with too many tests—verbose, hard to understand, and tiring to write.

Data-driven tests

KotlinTest provides data-driven tests to solve this issue. We can provide a table of data and KotlinTest will take input parameters from the table and verify that the output of a function under test is equal to the output expressed in the table. The benefit of this approach is that the asserts are run for each row. If the asserts for one row fail, it doesn’t stop the execution of that test; instead it separately verifies each given row.

An example will help to see data-driven tests in action. First, let’s bring in some necessary imports to the top of the AirportTest.kt file:

// AirportTest.kt
import io.kotlintest.data.forall 
import io.kotlintest.tables.row

Now, replace all the tests we’ve written for the sort() function with just one test, like so:

Get hands-on with 1200+ tech skills courses.