Multiplatform testing framework providing unified API for writing tests across all Kotlin platforms with assertions and test annotations.
—
Core assertion functions for verifying boolean conditions, equality, and identity comparisons. These are the most commonly used assertions in Kotlin Test.
Asserts that a Boolean expression or lambda returns true.
/**
* Asserts that the expression is true with an optional message
* @param actual - Boolean value to check
* @param message - Optional custom failure message
*/
fun assertTrue(actual: Boolean, message: String? = null)
/**
* Asserts that the given block returns true
* @param message - Optional custom failure message
* @param block - Lambda that should return true
*/
inline fun assertTrue(message: String? = null, block: () -> Boolean)Usage Examples:
import kotlin.test.*
@Test
fun testTrueAssertions() {
// Direct boolean check
assertTrue(5 > 3)
assertTrue(true, "This should always pass")
// Lambda form
assertTrue("List should not be empty") {
listOf(1, 2, 3).isNotEmpty()
}
// With calculation
val result = calculateValue()
assertTrue(result > 0, "Result should be positive: $result")
}Asserts that a Boolean expression or lambda returns false.
/**
* Asserts that the expression is false with an optional message
* @param actual - Boolean value to check
* @param message - Optional custom failure message
*/
fun assertFalse(actual: Boolean, message: String? = null)
/**
* Asserts that the given block returns false
* @param message - Optional custom failure message
* @param block - Lambda that should return false
*/
inline fun assertFalse(message: String? = null, block: () -> Boolean)Usage Examples:
@Test
fun testFalseAssertions() {
// Direct boolean check
assertFalse(5 < 3)
assertFalse(false, "This should always pass")
// Lambda form
assertFalse("List should be empty") {
emptyList<Int>().isNotEmpty()
}
// With string operations
val text = "hello"
assertFalse(text.contains("world"), "Text should not contain 'world'")
}Asserts that two values are equal using the equals method.
/**
* Asserts that the expected value is equal to the actual value
* @param expected - Expected value
* @param actual - Actual value to compare
* @param message - Optional custom failure message
*/
fun <T> assertEquals(expected: T, actual: T, message: String? = null)
/**
* Asserts that doubles are equal within an absolute tolerance
* @param expected - Expected double value
* @param actual - Actual double value
* @param absoluteTolerance - Maximum allowed difference
* @param message - Optional custom failure message
*/
fun assertEquals(expected: Double, actual: Double, absoluteTolerance: Double, message: String? = null)
/**
* Asserts that floats are equal within an absolute tolerance
* @param expected - Expected float value
* @param actual - Actual float value
* @param absoluteTolerance - Maximum allowed difference
* @param message - Optional custom failure message
*/
fun assertEquals(expected: Float, actual: Float, absoluteTolerance: Float, message: String? = null)Usage Examples:
@Test
fun testEquality() {
// Basic equality
assertEquals(42, 6 * 7)
assertEquals("hello", "he" + "llo")
// Collections
assertEquals(listOf(1, 2, 3), mutableListOf(1, 2, 3))
// With message
val userName = getUserName()
assertEquals("admin", userName, "User should be admin")
// Floating point with tolerance
assertEquals(0.1 + 0.2, 0.3, 0.0001, "Floating point addition")
assertEquals(3.14f, calculatePi(), 0.01f, "Pi approximation")
}Asserts that two values are not equal.
/**
* Asserts that the actual value is not equal to the illegal value
* @param illegal - Value that should not match
* @param actual - Actual value to compare
* @param message - Optional custom failure message
*/
fun <T> assertNotEquals(illegal: T, actual: T, message: String? = null)
/**
* Asserts that doubles are not equal within an absolute tolerance
* @param illegal - Double value that should not match
* @param actual - Actual double value
* @param absoluteTolerance - Tolerance for comparison
* @param message - Optional custom failure message
*/
fun assertNotEquals(illegal: Double, actual: Double, absoluteTolerance: Double, message: String? = null)
/**
* Asserts that floats are not equal within an absolute tolerance
* @param illegal - Float value that should not match
* @param actual - Actual float value
* @param absoluteTolerance - Tolerance for comparison
* @param message - Optional custom failure message
*/
fun assertNotEquals(illegal: Float, actual: Float, absoluteTolerance: Float, message: String? = null)Usage Examples:
@Test
fun testInequality() {
// Basic inequality
assertNotEquals(0, 5)
assertNotEquals("hello", "world")
// Different object instances
assertNotEquals(listOf(1, 2), listOf(3, 4))
// With message
val sessionId = generateSessionId()
assertNotEquals("", sessionId, "Session ID should not be empty")
// Floating point with tolerance
assertNotEquals(0.1, 0.2, 0.05, "Values should be different")
}Asserts that two references point to the same object instance (using ===).
/**
* Asserts that expected and actual refer to the same instance
* @param expected - Expected object reference
* @param actual - Actual object reference
* @param message - Optional custom failure message
*/
fun <T> assertSame(expected: T, actual: T, message: String? = null)Usage Examples:
@Test
fun testSameInstance() {
val list = mutableListOf(1, 2, 3)
val sameList = list
// Same reference
assertSame(list, sameList)
// Singleton pattern
val singleton1 = MySingleton.getInstance()
val singleton2 = MySingleton.getInstance()
assertSame(singleton1, singleton2, "Singleton should return same instance")
}Asserts that two references do not point to the same object instance.
/**
* Asserts that illegal and actual do not refer to the same instance
* @param illegal - Object reference that should not match
* @param actual - Actual object reference
* @param message - Optional custom failure message
*/
fun <T> assertNotSame(illegal: T, actual: T, message: String? = null)Usage Examples:
@Test
fun testDifferentInstances() {
val list1 = mutableListOf(1, 2, 3)
val list2 = mutableListOf(1, 2, 3)
// Different instances (even with same content)
assertNotSame(list1, list2)
// Object creation
val obj1 = createNewObject()
val obj2 = createNewObject()
assertNotSame(obj1, obj2, "Factory should create different instances")
}@Test
fun testMultipleAssertions() {
val result = processData("input")
// Multiple assertions on the same result
assertTrue(result.isNotEmpty(), "Result should not be empty")
assertEquals("processed", result.status)
assertNotEquals(0, result.timestamp)
}@Test
fun testWithCustomMessages() {
val user = authenticateUser("testuser", "password")
assertNotNull(user, "User authentication should succeed")
assertEquals("testuser", user.username, "Username should match input")
assertTrue(user.isActive, "User should be active after authentication")
}@Test
fun testEdgeCases() {
// Empty values
assertEquals("", "".trim())
assertTrue(emptyList<Int>().isEmpty())
// Boundary values
assertEquals(Int.MAX_VALUE, Int.MAX_VALUE)
assertEquals(0.0, -0.0) // Special case for doubles
// Floating point precision
val calculated = 0.1 + 0.1 + 0.1
assertEquals(0.3, calculated, 0.0001)
}Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-test