Common test assertions and utilities for Kotlin multiplatform projects
—
Core boolean assertion functions for testing true/false conditions in Kotlin test code. These functions provide both direct value testing and lambda-based conditional testing with optional custom error messages.
Asserts that a boolean expression or lambda result is true.
/**
* Asserts that the value is true with an optional message.
* @param actual The boolean value to test
* @param message Optional message to show if assertion fails
*/
fun assertTrue(actual: Boolean, message: String? = null)
/**
* Asserts that the given block returns true with an optional message.
* @param message Optional message to show if assertion fails
* @param block Lambda function that returns a boolean
*/
inline fun assertTrue(message: String? = null, block: () -> Boolean)Usage Examples:
import kotlin.test.assertTrue
@Test
fun testTrueConditions() {
// Direct boolean assertion
assertTrue(5 > 3)
assertTrue(5 > 3, "Five should be greater than three")
// Lambda-based assertion
assertTrue("List should not be empty") {
listOf(1, 2, 3).isNotEmpty()
}
// Complex condition testing
val user = User("Alice", 25)
assertTrue(user.isAdult())
assertTrue("User should be adult") { user.age >= 18 }
}Asserts that a boolean expression or lambda result is false.
/**
* Asserts that the value is false with an optional message.
* @param actual The boolean value to test
* @param message Optional message to show if assertion fails
*/
fun assertFalse(actual: Boolean, message: String? = null)
/**
* Asserts that the given block returns false with an optional message.
* @param message Optional message to show if assertion fails
* @param block Lambda function that returns a boolean
*/
inline fun assertFalse(message: String? = null, block: () -> Boolean)Usage Examples:
import kotlin.test.assertFalse
@Test
fun testFalseConditions() {
// Direct boolean assertion
assertFalse(3 > 5)
assertFalse(3 > 5, "Three should not be greater than five")
// Lambda-based assertion
assertFalse("List should be empty") {
emptyList<String>().isNotEmpty()
}
// Complex condition testing
val account = Account(balance = 0.0)
assertFalse(account.hasPositiveBalance())
assertFalse("Account should not be overdrawn") { account.balance < 0 }
}Both assertion functions will throw an AssertionError when the condition fails:
Custom messages are included in the error to provide context about the failure:
// This will throw: AssertionError: Expected value to be true.
assertTrue(false)
// This will throw: AssertionError: User should be active.
assertTrue(user.isActive, "User should be active")Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-test-common