Are there any example repositories of running Comp...
# compose
a
Are there any example repositories of running Compose tests on Android, I can run on my computer? The docs for it seem out of date and don't compile. After finally being able to create a common test module and run, my tests fail without any logs (I suspect they fail before running)
Re: out of date This part is not multiplatform
j
That part is for running tests on a device or emulator
To test against Android, you need to be running in an emulator or on device
a
I am aware
j
So what exactly are you looking for? Just how to test on Android or?
Actually even this doc isn't completely up to date because the comment still says "androidUnitTest".
j
It's a bit of a mixed bag right now, there's the Android Gradle Plugin and the Android Kotlin Multiplatform Gradle Plugin, both of which are "current" and have slightly different source set names 😔
a
My goal is to run compose ui tests on Android. Currently I am running tests but the reports are empty. Logcat shows that the test process starts and ends, so something is up there. Ideally I want a working repository that I can run on my computer to see how my setup behaves. Alternatively, looking for info on how to properly run the tests from someone who is actively running compose ui tests on Android
j
You'll need
Copy code
androidx.compose.ui:ui-test-manifest:1.8.2
in your Android dependencies so that the empty Activity for tests to launch is available. Last I checked that needed to be in your main dependencies, not the test source dependencies. Not sure if that's still the case The docs you linked correctly say you need to specify Android JUnit runner
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
But it misses the dependency you need for that
androidx.test:runner:1.7.0
From there it should just work, but if it doesn't I most always have stacktraces to follow somewhere. If the runner crashed, it'll show up in logcat, it's not often I see the problem in build output
a
I was able to run compose UI tests on an Android emulator. The documentation is out of date and doesn't compile. Here are the up to date change you need to do:
Copy code
// app/build.gradle.kts
kotlin {
    androidTarget {
        instrumentedTestVariant.sourceSetTree.set(KotlinSourceSetTree.test) //<- add this
    }

sourceSets {
        androidInstrumentedTest.dependencies { // <- add this entire block
            implementation("androidx.compose.ui:ui-test-junit4-android:1.8.2")
            implementation("androidx.compose.ui:ui-test-manifest:1.8.2")
        }
    }
}

android {
    defaultConfig {
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" // <- add this
    }
}
Then move your test (or create new ones) at the
commonTest
directory. You can run your tests with
./gradlew core:connectedAndroidTest
PS: If your test runs but there is no report, check to see if you have an error like this in your console:
Copy code
> Task :core:connectedDebugAndroidTest Tests on Pixel_6_-_35(AVD) - 15 failed: There was 1 failure(s). Test run failed to complete. Instrumentation run failed due to Process crashed. > Task :core:connectedDebugAndroidTest FAILED Build e74eb030-655f-4d24-bd9f-8e40db14964d is closed FAILURE: Build failed with an exception.
^ this means that the test process fails. I made this working by switching to a different emulator. (went from API lvl 36 to API lvl 27). This is probably an Android issue not a CMP issue, but worth mentioning.
🙌 1
a
The question that I always have is usually, if I have 300 compose multiplatform tests, and I just need to run 20 that in a specific package says "com.sample.b", how do I filter to run just these tests?
o
I can relate, the current state of Android/KMP testing documentation is less than ideal. I‘m working on TestBalloon testballoon making this a lot easier. We had a lot of progress recently, which I hope to release in the weeks ahead. But you can already look into the examples, they contain working build scripts, which might help even if you’re using the traditional test frameworks.
a
@Oliver.O does test balloon support filtering for instrumentationTest?
o
Yes, the framework supports it. In the currently released version, you'd use Gradle properties for that like so: •
./gradlew -Pandroid.testInstrumentationRunnerArguments.TEST_INCLUDE=com*sample*b* pixel2api30AndroidDeviceTest
(The next release will support dots in patterns.) What still needs to be done is make the IDE plugin's editor gutter icons support instrumented tests.
a
I will definitely try this then. Thank @Oliver.O
o
Cool. Get back to me with any issues you might encounter.