Skip to content

kmpbits/uiTestsComposeMultiplatform

Repository files navigation

Compose Multiplatform UI Testing Demo

This repository is the official companion code for the article: "Clean Lap: UI Testing in Compose Multiplatform".

It serves as a practical implementation of a unified testing strategy, demonstrating how to write UI tests once in commonTest and execute them across Android, iOS, and Desktop targets.

📖 What This Demo Covers

This project showcases several advanced Compose Multiplatform concepts discussed in the article:

  • Unified UI Testing: Leveraging the ComposeUiTest API to run the same test suite across all supported platforms.
  • Asynchronous Testing: Implementing waitUntil and semantic node polling to handle LaunchedEffect and Coroutine-driven state changes (see FeedScreenTest.kt).
  • Shared Architecture: A clean, multiplatform implementation of ViewModel and StateFlow using Jetpack Lifecycle.
  • Navigation & State: A simple but effective screen switching mechanism (Home -> Login -> Feed) managed within the shared source set.

🛠 Tech Stack

  • Framework: Compose Multiplatform
  • Architecture: Jetpack ViewModel (shared in commonMain)
  • Testing: Compose UI Test API (v2)
  • Concurrency: Kotlinx Coroutines

🚀 Getting Started

Prerequisites

  • Android Studio Koala (or newer)
  • JDK 17
  • Xcode (for iOS testing/running)

Running the Application

  • Android: Run the composeApp configuration.
  • Desktop: Execute ./gradlew :composeApp:run
  • iOS: Open iosApp/iosApp.xcworkspace in Xcode.

🧪 Running the Tests

The core value of this repository is the shared testing suite located in composeApp/src/commonTest.

Desktop (JVM)

./gradlew :composeApp:test

Android (Instrumented)

./gradlew :composeApp:connectedDebugAndroidTest

Example: Testing Async Loading

@Test
fun feedScreen_showsLoadingThenContent() = runComposeUiTest {
    // ... initial loading state check ...
    
    // Explicitly wait for the list to appear after async load
    waitUntil(timeoutMillis = 1000) {
        onAllNodesWithTag("feed_list").fetchSemanticsNodes().isNotEmpty()
    }
}

For the full breakdown of how this was built, read the complete article on KMP Bits.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors