Common test assertions and utilities for Kotlin multiplatform projects
—
Equality and inequality assertions providing precise comparison testing with support for tolerance-based floating point comparisons and instance reference checks. These functions are essential for verifying expected values and object relationships in tests.
Asserts that two values are equal using structural equality.
/**
* Asserts that the expected and actual values are equal.
* Uses structural equality (equals() method).
* @param expected The expected value
* @param actual The actual value to compare
* @param message Optional message to show if assertion fails
*/
fun <T> assertEquals(expected: T, actual: T, message: String? = null)
/**
* Asserts that two Double values are equal within the given absolute tolerance.
* @param expected The expected Double value
* @param actual The actual Double value to compare
* @param absoluteTolerance The maximum allowed difference between the values
* @param message Optional message to show if assertion fails
* @since Kotlin 1.5
*/
fun assertEquals(expected: Double, actual: Double, absoluteTolerance: Double, message: String? = null)
/**
* Asserts that two Float values are equal within the given absolute tolerance.
* @param expected The expected Float value
* @param actual The actual Float value to compare
* @param absoluteTolerance The maximum allowed difference between the values
* @param message Optional message to show if assertion fails
* @since Kotlin 1.5
*/
fun assertEquals(expected: Float, actual: Float, absoluteTolerance: Float, message: String? = null)Usage Examples:
import kotlin.test.assertEquals
@Test
fun testEquality() {
// Basic equality
assertEquals(42, 40 + 2)
assertEquals("hello", "hel" + "lo")
assertEquals(listOf(1, 2, 3), mutableListOf(1, 2, 3))
// With custom messages
assertEquals(100, calculateScore(), "Score calculation should return 100")
// Floating point equality with tolerance
assertEquals(3.14159, calculatePi(), 0.001, "Pi calculation should be accurate")
assertEquals(2.5f, calculateAverage(), 0.1f, "Average should be close to 2.5")
// Object equality
val expected = User("Alice", 25)
val actual = createUser("Alice", 25)
assertEquals(expected, actual, "Users should be equal")
}Asserts that two values are not equal using structural equality.
/**
* Asserts that the illegal and actual values are not equal.
* Uses structural equality (equals() method).
* @param illegal The value that should not match
* @param actual The actual value to compare
* @param message Optional message to show if assertion fails
*/
fun <T> assertNotEquals(illegal: T, actual: T, message: String? = null)
/**
* Asserts that two Double values are not equal within the given absolute tolerance.
* @param illegal The Double value that should not match
* @param actual The actual Double value to compare
* @param absoluteTolerance The minimum required difference between the values
* @param message Optional message to show if assertion fails
* @since Kotlin 1.5
*/
fun assertNotEquals(illegal: Double, actual: Double, absoluteTolerance: Double, message: String? = null)
/**
* Asserts that two Float values are not equal within the given absolute tolerance.
* @param illegal The Float value that should not match
* @param actual The actual Float value to compare
* @param absoluteTolerance The minimum required difference between the values
* @param message Optional message to show if assertion fails
* @since Kotlin 1.5
*/
fun assertNotEquals(illegal: Float, actual: Float, absoluteTolerance: Float, message: String? = null)Usage Examples:
import kotlin.test.assertNotEquals
@Test
fun testInequality() {
// Basic inequality
assertNotEquals(0, 1 + 1)
assertNotEquals("hello", "world")
assertNotEquals(listOf(1, 2, 3), listOf(3, 2, 1))
// With custom messages
assertNotEquals(0, generateRandomNumber(), "Random number should not be zero")
// Floating point inequality with tolerance
assertNotEquals(0.0, calculateNonZeroValue(), 0.001, "Value should be significantly non-zero")
assertNotEquals(1.0f, calculateRatio(), 0.1f, "Ratio should not be close to 1.0")
}Asserts that two references point to the same object instance.
/**
* Asserts that expected and actual are the same instance (reference equality).
* Uses identity equality (=== operator).
* @param expected The expected object reference
* @param actual The actual object reference to compare
* @param message Optional message to show if assertion fails
*/
fun <T> assertSame(expected: T, actual: T, message: String? = null)Usage Examples:
import kotlin.test.assertSame
@Test
fun testSameInstance() {
// Singleton pattern testing
val instance1 = DatabaseConnection.getInstance()
val instance2 = DatabaseConnection.getInstance()
assertSame(instance1, instance2, "Should return same singleton instance")
// Object reference testing
val originalList = mutableListOf(1, 2, 3)
val returnedList = processListInPlace(originalList)
assertSame(originalList, returnedList, "Should return the same list instance")
// String interning
val str1 = "hello"
val str2 = "hello"
assertSame(str1, str2, "String literals should be interned")
}Asserts that two references do not point to the same object instance.
/**
* Asserts that illegal and actual are not the same instance (reference inequality).
* Uses identity equality (=== operator).
* @param illegal The object reference that should not match
* @param actual The actual object reference to compare
* @param message Optional message to show if assertion fails
*/
fun <T> assertNotSame(illegal: T, actual: T, message: String? = null)Usage Examples:
import kotlin.test.assertNotSame
@Test
fun testDifferentInstances() {
// Copy operations should create new instances
val original = User("Alice", 25)
val copy = original.copy()
assertNotSame(original, copy, "Copy should create a new instance")
// Factory methods should create new instances
val config1 = Configuration.create()
val config2 = Configuration.create()
assertNotSame(config1, config2, "Factory should create new instances")
// List copying
val originalList = listOf(1, 2, 3)
val copiedList = originalList.toList()
assertNotSame(originalList, copiedList, "toList() should create new instance")
}Equality assertion functions will throw an AssertionError when comparisons fail:
Error messages include the expected and actual values for debugging:
// This will throw: AssertionError: Expected <42>, actual <43>.
assertEquals(42, 43)
// This will throw: AssertionError: Expected not same but was same: <User(name=Alice)>
assertNotSame(user1, user1)Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-test-common