https://kotlinlang.org logo
#compose
Title
# compose
d

Darryl Pierce

12/12/2023, 7:53 PM
Hi, all. I've got a project I'm working on that's using KMP to share backend code between Android (UX written with Compose) and iOS (UX written with SwiftUI). I'm hitting an issue with running unit tests for the Android Compose pieces, and wondered if someone here might be able to help out with identifying the issue. What happens is, when I run the unit test for one of my Composables, I'm getting: java.lang.RuntimeException: Intent in process org.comixedproject.variant resolved to different process org.comixedproject.variant.test: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10008000 cmp=org.comixedproject.variant.test/androidx.activity.ComponentActivity } My feature branch is here: https://github.com/mcpierce/variant/tree/feature/issue-35
j

Jeff Lockhart

12/12/2023, 7:59 PM
Should probably clarify, this is running Android instrumented tests, rather than unit tests.
d

Darryl Pierce

12/13/2023, 12:39 AM
Sorry to confuse. But the question for me then is: should composables be tested as unit tests or as instrumented tests?
j

Jeff Lockhart

12/13/2023, 1:27 AM
My understanding is Compose Android tests should be run as Android instrumented tests.
d

Dario Ahdoot

12/13/2023, 10:55 AM
It is possible to get them to run under unit tests under robolectric
d

Darryl Pierce

12/14/2023, 2:42 PM
@Dario Ahdoot Do tell.
d

Dario Ahdoot

12/14/2023, 2:51 PM
I wasn't the one who did it on my team, but, looking at the PR where we moved them from UI to unit tests, we just needed to make add all of the Compose dependencies as
testImplementation
, and then each Test class needs to be annotated with:
Copy code
@RunWith(AndroidJUnit4::class)
@Config(instrumentedPackages = ["androidx.loader.content"])
The second being required as some sort of workaround (I think). We needed to update some of the tests as well, particularly the ones where we needed to scroll things into view, as I think robolectric has a relatively small and fixed screen size
d

Darryl Pierce

12/14/2023, 3:34 PM
@Dario Ahdoot Thanks for that, I'll look into it.
That all said, I've done some changes to the project structure, moving files around using an updated project template from the KMP wizard. When I run a compose unit test, I'm now seeing this error:
12-14 10:35:23.296 20711 20728 I TestRunner: started: warning(junit.framework.TestSuite$1)
12-14 10:35:23.297 20711 20728 I TestRunner: failed: warning(junit.framework.TestSuite$1)
12-14 10:35:23.297 20711 20728 I TestRunner: ----- begin exception -----
12-14 10:35:23.297 20711 20728 I TestRunner: junit.framework.AssertionFailedError: No tests found in org.comixedproject.variant.ui.server.ServerColorTest
12-14 10:35:23.297 20711 20728 I TestRunner: 	at junit.framework.Assert.fail(Assert.java:50)
12-14 10:35:23.297 20711 20728 I TestRunner: 	at junit.framework.TestSuite$1.runTest(TestSuite.java:97)
12-14 10:35:23.297 20711 20728 I TestRunner: 	at junit.framework.TestCase.runBare(TestCase.java:134)
12-14 10:35:23.297 20711 20728 I TestRunner: 	at junit.framework.TestResult$1.protect(TestResult.java:115)
12-14 10:35:23.297 20711 20728 I TestRunner: 	at junit.framework.TestResult.runProtected(TestResult.java:133)
12-14 10:35:23.297 20711 20728 I TestRunner: 	at junit.framework.TestResult.run(TestResult.java:118)
12-14 10:35:23.297 20711 20728 I TestRunner: 	at junit.framework.TestCase.run(TestCase.java:124)
12-14 10:35:23.297 20711 20728 I TestRunner: 	at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:195)
12-14 10:35:23.297 20711 20728 I TestRunner: 	at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:181)
12-14 10:35:23.297 20711 20728 I TestRunner: 	at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:563)
12-14 10:35:23.297 20711 20728 I TestRunner: 	at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2248)
12-14 10:35:23.297 20711 20728 I TestRunner: ----- end exception -----
12-14 10:35:23.298 20711 20728 I TestRunner: finished: warning(junit.framework.TestSuite$1)
My branch is here: https://github.com/mcpierce/variant/tree/feature/issue-35
I feel like I'm finally almost there being able to create composable tests.
I'm almost there: now on my branch tests almost run, the issue I'm seeing is that the runner says "No tests found in org.comixedproject.variant.ui.server.ServerColorTest", even though I have a function annotated with \@Test from org.junit.Test. I tried adding robolectric and using it as the runner, but it made no difference.
j

Jeff Lockhart

12/15/2023, 12:52 AM
You should use
androidTarget()
to target Android.
android()
is deprecated, eventually to be replaced by the Android Gradle plugin supporting KMP directly.
Also, I would avoid setting up source set dependencies by calling
dependsOn
. Since Kotlin 1.9.20, the default hierarchy template is applied automatically and calling
dependsOn
prevents it from being applied by default.
Your Android tests are still instrumented tests, so without this code, the androidInstrumentedTest source set is not connected to the commonTest tree.
d

Darryl Pierce

12/15/2023, 1:01 AM
@Jeff Lockhart I've applied the changes you recommended and am trying them out now.
I got a popup saying there's a newer version of the app alread installed when I try to run the single test.
Maybe I have something still setup wrong? After I click to uninstall the current version, the single test is just sitting there and not moving forward, but there are no error messages.
Just sitting on this output:
4 Views