0
# Test Annotations
1
2
Platform-agnostic test annotations that automatically map to the appropriate testing framework on each platform (JUnit, JUnit 5, TestNG, Jasmine, Mocha, etc.).
3
4
## Capabilities
5
6
### Test Annotation
7
8
Marks a function as a test method.
9
10
```kotlin { .api }
11
@Target(AnnotationTarget.FUNCTION)
12
annotation class Test
13
```
14
15
**Usage:**
16
17
```kotlin
18
import kotlin.test.Test
19
20
class MyTests {
21
@Test
22
fun shouldReturnCorrectValue() {
23
// Test implementation
24
}
25
}
26
```
27
28
**Platform Mappings:**
29
- **JVM (JUnit)**: `@org.junit.Test`
30
- **JVM (JUnit 5)**: `@org.junit.jupiter.api.Test`
31
- **JVM (TestNG)**: `@org.testng.annotations.Test`
32
- **JavaScript**: Framework-specific test registration
33
- **Native/WASM**: Framework-specific test registration
34
35
### BeforeTest Annotation
36
37
Marks a function to be invoked before each test method.
38
39
```kotlin { .api }
40
@Target(AnnotationTarget.FUNCTION)
41
annotation class BeforeTest
42
```
43
44
**Usage:**
45
46
```kotlin
47
import kotlin.test.BeforeTest
48
import kotlin.test.Test
49
50
class MyTests {
51
private lateinit var testData: String
52
53
@BeforeTest
54
fun setup() {
55
testData = "initialized"
56
}
57
58
@Test
59
fun testWithSetup() {
60
assertEquals("initialized", testData)
61
}
62
}
63
```
64
65
**Platform Mappings:**
66
- **JVM (JUnit)**: `@org.junit.Before`
67
- **JVM (JUnit 5)**: `@org.junit.jupiter.api.BeforeEach`
68
- **JVM (TestNG)**: `@org.testng.annotations.BeforeMethod`
69
- **JavaScript**: Framework-specific setup hooks
70
- **Native/WASM**: Framework-specific setup hooks
71
72
### AfterTest Annotation
73
74
Marks a function to be invoked after each test method.
75
76
```kotlin { .api }
77
@Target(AnnotationTarget.FUNCTION)
78
annotation class AfterTest
79
```
80
81
**Usage:**
82
83
```kotlin
84
import kotlin.test.AfterTest
85
import kotlin.test.Test
86
87
class MyTests {
88
private var resource: AutoCloseable? = null
89
90
@Test
91
fun testWithResource() {
92
resource = openResource()
93
// Test using resource
94
}
95
96
@AfterTest
97
fun cleanup() {
98
resource?.close()
99
resource = null
100
}
101
}
102
```
103
104
**Platform Mappings:**
105
- **JVM (JUnit)**: `@org.junit.After`
106
- **JVM (JUnit 5)**: `@org.junit.jupiter.api.AfterEach`
107
- **JVM (TestNG)**: `@org.testng.annotations.AfterMethod`
108
- **JavaScript**: Framework-specific teardown hooks
109
- **Native/WASM**: Framework-specific teardown hooks
110
111
### Ignore Annotation
112
113
Marks a test class or test method to be ignored/skipped during test execution.
114
115
```kotlin { .api }
116
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
117
annotation class Ignore
118
```
119
120
**Usage:**
121
122
```kotlin
123
import kotlin.test.Ignore
124
import kotlin.test.Test
125
126
class MyTests {
127
@Test
128
fun runningTest() {
129
// This test will run
130
}
131
132
@Test
133
@Ignore
134
fun skippedTest() {
135
// This test will be skipped
136
}
137
}
138
139
@Ignore
140
class SkippedTestClass {
141
@Test
142
fun thisWillBeSkipped() {
143
// Entire class is ignored
144
}
145
}
146
```
147
148
**Platform Mappings:**
149
- **JVM (JUnit)**: `@org.junit.Ignore`
150
- **JVM (JUnit 5)**: `@org.junit.jupiter.api.Disabled`
151
- **JVM (TestNG)**: `@org.testng.annotations.Test(enabled = false)`
152
- **JavaScript**: Framework-specific skip mechanisms
153
- **Native/WASM**: Framework-specific skip mechanisms
154
155
## Multiplatform Usage
156
157
These annotations work seamlessly across all Kotlin platforms. The same test code can be compiled for different targets:
158
159
```kotlin
160
// commonTest source set
161
import kotlin.test.*
162
163
class MultiplatformTest {
164
@BeforeTest
165
fun setup() {
166
println("Setting up test")
167
}
168
169
@Test
170
fun testMultiplatform() {
171
assertEquals(4, 2 + 2)
172
}
173
174
@AfterTest
175
fun tearDown() {
176
println("Cleaning up test")
177
}
178
}
179
```
180
181
This test will work on:
182
- **JVM**: Using JUnit, JUnit 5, or TestNG
183
- **JavaScript**: Using Mocha, Jest, Jasmine, or other JS test frameworks
184
- **Native**: Using the Kotlin/Native test framework
185
- **WASM**: Using WebAssembly-specific test runners