Kotlin Test library for experimental WebAssembly JS platform - provides test framework adapters and assertions for testing Kotlin/Wasm applications that target JavaScript environments
—
Test annotations for marking functions as tests and controlling test lifecycle. These annotations provide the foundation for organizing and running Kotlin tests in WebAssembly JavaScript environments.
Marks a function as a test case that should be executed by the test framework.
/**
* Marks a function as a test.
*
* Only functions of a class are supported as test functions.
* Test functions must have no parameters and return type Unit.
*/
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class TestUsage Example:
import kotlin.test.*
class MyTests {
@Test
fun simpleTest() {
assertEquals(4, 2 + 2)
}
@Test
fun stringTest() {
val message = "Hello, World!"
assertTrue(message.isNotEmpty())
assertContains(message, "World")
}
@Test
fun asyncTest(): Promise<*> {
return Promise.resolve("async result").then { result ->
assertEquals("async result", result)
}
}
}Marks a function to be invoked before each test in a test class.
/**
* Marks a function to be invoked before each test case.
*
* Only functions of a class are supported as before test functions.
* Before test functions must have no parameters and return type Unit.
*/
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class BeforeTestUsage Example:
import kotlin.test.*
class DatabaseTests {
private lateinit var database: TestDatabase
@BeforeTest
fun setup() {
database = TestDatabase()
database.connect()
database.initializeTestData()
}
@Test
fun testUserCreation() {
val user = database.createUser("test@example.com")
assertNotNull(user)
assertEquals("test@example.com", user.email)
}
@Test
fun testUserDeletion() {
val user = database.createUser("delete@example.com")
database.deleteUser(user.id)
assertNull(database.findUser(user.id))
}
}Marks a function to be invoked after each test in a test class.
/**
* Marks a function to be invoked after each test case.
*
* Only functions of a class are supported as after test functions.
* After test functions must have no parameters and return type Unit.
*/
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class AfterTestUsage Example:
import kotlin.test.*
class FileSystemTests {
private val tempFiles = mutableListOf<String>()
@Test
fun testFileCreation() {
val fileName = "test-${System.currentTimeMillis()}.txt"
createTestFile(fileName)
tempFiles.add(fileName)
assertTrue(fileExists(fileName))
}
@AfterTest
fun cleanup() {
// Clean up any files created during the test
tempFiles.forEach { fileName ->
deleteFile(fileName)
}
tempFiles.clear()
}
}Marks a test function or test class as ignored, preventing it from being executed.
/**
* Marks a test or test class as ignored.
* Ignored tests are not executed by the test framework.
*
* @param value optional reason for ignoring the test
*/
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Ignore(val value: String = "")Usage Examples:
import kotlin.test.*
class FeatureTests {
@Test
fun workingTest() {
assertEquals(2 + 2, 4)
}
@Ignore("Feature not implemented yet")
@Test
fun futureFeatureTest() {
// This test won't run until @Ignore is removed
fail("This feature is not ready")
}
@Ignore("Flaky test - investigating")
@Test
fun flakyTest() {
// Temporarily ignored due to intermittent failures
assertTrue(Math.random() > 0.5) // This would be flaky
}
}
@Ignore("Entire class under development")
class ExperimentalTests {
@Test
fun experimentalFeature1() {
// None of these tests will run because the class is ignored
fail("Not ready")
}
@Test
fun experimentalFeature2() {
fail("Not ready")
}
}Test Lifecycle Order:
@BeforeTest functions are called before each individual test@Test function is executed@AfterTest functions are called after each individual testClass vs Function Ignoring:
@Ignore, all tests in that class are ignored@Ignore, only those specific tests are ignoredBest Practices:
@BeforeTest for test setup that needs to be fresh for each test@AfterTest for cleanup to prevent test isolation issues@Ignore to help future maintainersInstall with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-test-wasm-js