Skip to content

Commit 7113476

Browse files
committed
Fix experimental API warnings in Compose UI tests
Add @OptIn(ExperimentalTestApi::class) annotation to all test classes to suppress "This testing API is experimental and is likely to be changed or removed entirely" warnings. Changes: - Add @OptIn(ExperimentalTestApi::class) to ComposeMultiplatformUiTests - Add @OptIn(ExperimentalTestApi::class) to ISSPositionUiTests - Add @OptIn(ExperimentalTestApi::class) to TestTagExampleTests - Add @OptIn(ExperimentalTestApi::class) to ViewModelUiTests - Update README.md with documentation about the opt-in requirement - Update all code examples in README to include the annotation - Add note explaining why the annotation is needed The annotation is required because the Compose Multiplatform UI testing framework (runComposeUiTest) is currently experimental. This is standard practice when using experimental APIs and does not affect test functionality. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 9ab77db commit 7113476

File tree

5 files changed

+62
-34
lines changed

5 files changed

+62
-34
lines changed

common/src/commonTest/kotlin/README.md

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class MyTest {
5656

5757
### Multiplatform Tests (common module)
5858
```kotlin
59+
@OptIn(ExperimentalTestApi::class)
5960
class MyTest {
6061
@Test
6162
fun myTest() = runComposeUiTest {
@@ -65,6 +66,8 @@ class MyTest {
6566
}
6667
```
6768

69+
**Note**: The `@OptIn(ExperimentalTestApi::class)` annotation is required because the Compose Multiplatform UI testing API is currently experimental. Add this annotation to your test classes to suppress experimental API warnings.
70+
6871
## Running the Tests
6972

7073
### Run all common tests
@@ -97,27 +100,34 @@ class MyTest {
97100
All tests follow this pattern:
98101

99102
```kotlin
100-
@Test
101-
fun testName() = runComposeUiTest {
102-
// 1. Setup (if needed)
103-
val viewModel = MyViewModel(fakeRepository)
104-
105-
// 2. Set content
106-
setContent {
107-
MaterialTheme {
108-
MyComposable(viewModel)
103+
@OptIn(ExperimentalTestApi::class)
104+
class MyUiTests {
105+
@Test
106+
fun testName() = runComposeUiTest {
107+
// 1. Setup (if needed)
108+
val viewModel = MyViewModel(fakeRepository)
109+
110+
// 2. Set content
111+
setContent {
112+
MaterialTheme {
113+
MyComposable(viewModel)
114+
}
109115
}
110-
}
111116

112-
// 3. Advance time (for async operations)
113-
testDispatcher.scheduler.advanceUntilIdle()
114-
waitForIdle()
117+
// 3. Advance time (for async operations)
118+
testDispatcher.scheduler.advanceUntilIdle()
119+
waitForIdle()
115120

116-
// 4. Assert
117-
onNodeWithText("Expected Text").assertIsDisplayed()
121+
// 4. Assert
122+
onNodeWithText("Expected Text").assertIsDisplayed()
123+
}
118124
}
119125
```
120126

127+
### Important: Experimental API
128+
129+
The Compose Multiplatform UI testing framework is currently experimental. You must add the `@OptIn(ExperimentalTestApi::class)` annotation to your test classes to use `runComposeUiTest` and suppress experimental API warnings.
130+
121131
## Common Test Assertions
122132

123133
### Existence
@@ -288,30 +298,43 @@ val viewModel = MyViewModel(fakeRepository)
288298

289299
### Testing CoordinateDisplay
290300
```kotlin
291-
@Test
292-
fun testCoordinateDisplay() = runComposeUiTest {
293-
setContent {
294-
CoordinateDisplay(label = "Latitude", value = "53.27")
301+
@OptIn(ExperimentalTestApi::class)
302+
class CoordinateDisplayTests {
303+
@Test
304+
fun testCoordinateDisplay() = runComposeUiTest {
305+
setContent {
306+
CoordinateDisplay(label = "Latitude", value = "53.27")
307+
}
308+
onNodeWithText("Latitude").assertIsDisplayed()
309+
onNodeWithText("53.27").assertIsDisplayed()
295310
}
296-
onNodeWithText("Latitude").assertIsDisplayed()
297-
onNodeWithText("53.27").assertIsDisplayed()
298311
}
299312
```
300313

301314
### Testing with ViewModel
302315
```kotlin
303-
@Test
304-
fun testWithViewModel() = runComposeUiTest {
305-
val viewModel = ISSPositionViewModel(fakeRepository)
316+
@OptIn(ExperimentalTestApi::class)
317+
class ViewModelIntegrationTests {
318+
private val testDispatcher = StandardTestDispatcher()
306319

307-
setContent {
308-
ISSPositionContent(viewModel)
320+
@BeforeTest
321+
fun setup() {
322+
Dispatchers.setMain(testDispatcher)
309323
}
310324

311-
testDispatcher.scheduler.advanceUntilIdle()
312-
waitForIdle()
325+
@Test
326+
fun testWithViewModel() = runComposeUiTest {
327+
val viewModel = ISSPositionViewModel(fakeRepository)
328+
329+
setContent {
330+
ISSPositionContent(viewModel)
331+
}
313332

314-
onNodeWithText("53.2743394").assertIsDisplayed()
333+
testDispatcher.scheduler.advanceUntilIdle()
334+
waitForIdle()
335+
336+
onNodeWithText("53.2743394").assertIsDisplayed()
337+
}
315338
}
316339
```
317340

@@ -324,8 +347,9 @@ fun testWithViewModel() = runComposeUiTest {
324347
## Contributing
325348

326349
When adding new UI tests:
327-
1. Follow the existing naming conventions
328-
2. Add test tags to new composables
329-
3. Create test composables for complex scenarios
330-
4. Document any platform-specific limitations
331-
5. Keep tests fast and focused
350+
1. Add `@OptIn(ExperimentalTestApi::class)` to your test class
351+
2. Follow the existing naming conventions (e.g., `*UiTests.kt`)
352+
3. Add test tags to new composables
353+
4. Create test composables for complex scenarios
354+
5. Document any platform-specific limitations
355+
6. Keep tests fast and focused

common/src/commonTest/kotlin/com/surrus/peopleinspace/ui/ComposeMultiplatformUiTests.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import kotlin.test.Test
1616
* - Works with kotlin.test instead of JUnit
1717
* - Can be executed on multiple platforms
1818
*/
19+
@OptIn(ExperimentalTestApi::class)
1920
class ComposeMultiplatformUiTests {
2021

2122
@Test

common/src/commonTest/kotlin/com/surrus/peopleinspace/ui/ISSPositionUiTests.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import kotlin.test.Test
1212
* These tests demonstrate testing Compose Multiplatform UI components
1313
* with realistic data from a fake repository.
1414
*/
15+
@OptIn(ExperimentalTestApi::class)
1516
class ISSPositionUiTests {
1617

1718
private val repository = PeopleInSpaceRepositoryFake()

common/src/commonTest/kotlin/com/surrus/peopleinspace/ui/TestTagExampleTests.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import kotlin.test.Test
2020
* 3. Apply test tags to key interactive elements and containers
2121
* 4. Use test tags over text matching for better test stability
2222
*/
23+
@OptIn(ExperimentalTestApi::class)
2324
class TestTagExampleTests {
2425

2526
companion object {

common/src/commonTest/kotlin/com/surrus/peopleinspace/viewmodel/ViewModelUiTests.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import kotlin.test.Test
2424
* These tests show how to test Compose UI components that interact with ViewModels,
2525
* using a fake repository to provide test data.
2626
*/
27+
@OptIn(ExperimentalTestApi::class)
2728
class ViewModelUiTests {
2829

2930
private val testDispatcher = StandardTestDispatcher()

0 commit comments

Comments
 (0)