…from kotlin.test
Keep your assertions🔗
You can keep your assertion library (most assertion libraries work out of the box, for Kotest Assertions there is a TestBalloon integration for it). Code inside tests can remain unchanged.
What needs to change🔗
Test Classes and Methods🔗
Top-level test classes become top-level suite properties. Test methods become test function invocations:
Class properties, setup and teardown🔗
Keep your code inside tests, and
- wrap
kotlin.testclass properties into a fixture, omittingvarandlateinit, - omit
runTest, - co-locate setup code with initialization,
- use the optional
closeWithlambda for tear-down code, - make it a test-level fixture, providing a fresh value to each test:
class KotlinTestFixture {
private lateinit var service: WeatherService
@BeforeTest
fun setup() = runTest {
service = FakeWeatherService()
service.connect(token = "TestToken")
}
@AfterTest
fun teardown() = runTest {
service.disconnect()
}
@Test
fun `Temperature in Hamburg is 21_5 °C`() = runTest {
assertEquals(21.5, service.location("Hamburg").temperature)
}
// more tests...
}
val FromKotlinTestFixture by testSuite {
testFixture {
FakeWeatherService().apply {
connect(token = "TestToken") // (1)!
}
} closeWith {
disconnect() // (2)!
} asParameterForEach { // (3)!
test("Temperature in Hamburg is 21.5 °C") { service ->
assertEquals(21.5, service.location("Hamburg").temperature)
}
// more tests...
}
}
- As the
testFixturelambda is suspending, you can co-locate any setup code here. - A suspending tear-down function.
- Making it a test-level fixture provides a fresh, isolated value as a parameter for each test.
Tip
For a fixture with multiple properties, use an object expression and provide it via asContextForEach to each test.
Other🔗
To @Ignore a test or suite, pass testConfig = TestConfig.disable() as a parameter to the test or testSuite function.
To migrate @BeforeClass and @AfterClass (which kotlin.test provides on Native), see the corresponding JUnit section on sharing state across tests.