https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
m

Mgj

06/23/2020, 11:45 AM
I have a multiplatform project (android + ios) which includes a 'commonTest'. How do i run just the common tests on jvm (i.e. not as instrumented platform tests)?
m

mbonnin

06/23/2020, 11:47 AM
./gradlew jvmTest
?
m

Mgj

06/23/2020, 11:48 AM
'jvmTest' is not a defined task. I used IdeaJ to create the project from its "Multiplatform android/ios"-template
'test' is defined and is supposed to run tests for all targets but it seems to actually only run the common tests. Interesting...
m

mbonnin

06/23/2020, 11:56 AM
Did you add
jvm()
as a target to your
kotlin {}
block? According to https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#running-tests, there should be a
jvmTest
target
👍 1
m

Mgj

06/23/2020, 12:00 PM
oh... i did not. Good link:
Copy code
As the commonTest default source set is added to all test compilations, tests and test tools that are needed on all target platforms may be placed there.
t

Tijl

06/23/2020, 12:18 PM
android unit tests also run in the JVM
a

andylamax

06/23/2020, 12:22 PM
@mbonnin if you are targeting
android
, adding
jvm()
isn't advised. Coz you already have an
android()
target. And that's what the IntelliJ plugin does in a mobile multiplatform project
👀 1
m

mbonnin

06/23/2020, 12:25 PM
So just
./gradlew androidTest
? Is there such a task?
m

Mgj

06/23/2020, 12:28 PM
There is not. There's a
test
and
testDebugUnitTest
task
m

mbonnin

06/23/2020, 12:29 PM
So maybe
testDebugUnitTest
is what you want in the end, that shouldn't run iOS tests, right?
m

Mgj

06/23/2020, 12:30 PM
I think so yeah
test
actually seems to not use platform tests either 🤷
k

Kris Wong

06/23/2020, 12:57 PM
when we talk about running, we typically need a target to run with. your common tests will be run whether you run your android or iOS tests.
m

Mgj

06/23/2020, 12:58 PM
Right, but i'd like to be able to run just the common tests without instrumentation (android or ios)
k

Kris Wong

06/23/2020, 12:58 PM
android supports non-instrumented tests
t

Tijl

06/23/2020, 1:05 PM
testDebugUnitTest indeed runs in the JVM, using a mostly stub android API
m

Mgj

06/23/2020, 1:07 PM
I'd like to be able to run "pure" tests with no stubbed (roboelectric?) platform apis available
t

Tijl

06/23/2020, 1:08 PM
if you want a “pure” JVM run, then add a
jvm
target. keep in mind that if you start adding libraries that have common code+platform implementation you’re not testing the android or ios implementation though
👍 1
the stubs are mostly just returning null on every method (or some default primitive)
or a runtime exception (it’s a setting)
so you at least should run your common unit test in both one of the iOS targets and UnitTest or connectedTest for Android to make sure they actually work
but
jvm
can be convinient some times (with some gradle tooling etc)
m

Mgj

06/23/2020, 1:12 PM
Yeah that sounds reasonable. I'd just like to have them explicitly split up. I tried adding a jvm target earlier, i couldnt initially get it to work but i guess i'll give it another go
r

russhwolf

06/23/2020, 3:20 PM
testDebugUnitTest
is the task you want if you have an Android target and want to do the same thing you would have done to run Android unit tests in a non-MPP project. Adding a JVM target will probably overcomplicate things if you're just doing it for tests.
m

Mgj

06/23/2020, 5:44 PM
Good point but i do want something different than android unit tests. I want to completely remove the possibility of hitting any platform API's. I want "common" to be clean, pure kotlin without anything platform specific and i want the same for "commonTest"
r

russhwolf

06/23/2020, 5:50 PM
The tests in
commonTest
will be platform-agnostic. But you still need to call a platform-specific task to run them (or run on all platforms with
./gradlew allTests
)
m

Mgj

06/23/2020, 5:52 PM
Seems cleaner to me to use jvm as a target in that case instead of either ios or android - especially as both have a nasty tendency to actually spawn an instance of the app when running unit tests (if using the copyResources=true flag on android, and always(?) on ios)
3 Views