Is there a sample with `androidTest` (instrumented...
# multiplatform
p
Is there a sample with
androidTest
(instrumented) sourceset in common module?
j
Are you wanting to run `commonTest`s as `androidInstrumentedTest`s and not run them as `androidUnitTest`s?
p
No, I just want to run it as regular instrumented test. I’m migrating an existing project to KMM. There was a module with unit tests in plain
test
sourceset which I moved to
commonTest
and it worked, and instrumented tests in
androidTest
sourceset which I’m not sure where to move and how to run now
j
Are these tests testing common code or Android platform-specific code? What is the reason they are running as Android instrumented tests?
p
Because it was using jetpack compose, now it’s using compose multiplatform
j
The tests that are in
androidTest
currently, do they access Android-specific APIs like
Context
? Or after migrating the code they test to KMP, is the code all in common? This will determine if the tests need to run as Android instrumented tests, or can just be added to your common tests as well.
In KMP, the Android platform has
androidUnitTest
, which equates to the Android
test
source set, and
androidInstrumentedTest
, which equates to the Android
androidTest
source set (note this changed as of Kotlin 1.8.0).
p
Actually I think it doesn’t depend on android-specific things. So it can be moved to
commonTest
, can’t it?
j
Yes, if the tests don't require any APIs that can only be used when running on a physical Android device, you should be able to add the tests to the
commonTest
source set as well, possibly in a different package than the unit tests, if that makes sense.
By default, the
androidUnitTest
source set is wired to the
commonTest
source set tree. The
androidInstrumentedTest
source set is not connected at all though, so it won't run common tests. You can change this, depending on your needs with a new API as of Kotlin 1.9.0. If you have code that is Android-specific in your
androidMain
source set that needs testing that can't use pure Kotlin common APIs, this is where you'd want to use `androidInstrumentedTest`s to run those tests on an Android device or emulator.
p
Thanks, I’ll try to use
commonTest
. But I’ve tried to use
androidInstrumentedTest
sourceset, but for some reason they are not executed when run with gradle and IDE shows this image when trying to launch it there
j
IDE support for running individual tests is lacking in KMP currently. You can run them with Gradle on the terminal or manually create a Gradle run configuration. Run all tests:
Copy code
./gradlew connectedAndroidTest
Run specific test class:
Copy code
./gradlew connectedAndroidTest --tests *.ClassName
Run specific test method:
Copy code
./gradlew connectedAndroidTest --tests *.ClassName.methodName
These test filters work with any of the platform-specific test tasks.
p
No I think it should be
connectedAndroidTest
j
Oh, yes, you're correct. Android tests are unique in that regard, where the gradle task is different than the source set names. You can also manually create an Android Instrumented Tests run configuration to run them in the IDE with a better UI to see the test results than the Gradle task.
132 Views