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

Hannes

01/03/2020, 6:45 PM
Hey folks, I'm struggeling a bit with kotlin mutliplatform and
commonTest
. In my
build.gradle
i have the following config:
Copy code
kotlin {
  ... 
  sourceSets {
     ...
       commonTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test-common'
                implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
            }
        }
   }
}
However, if I run a test in
src/commonTest/kotlin/my/package/FooTest.kt
which looks like this
Copy code
import kotlin.test.Test
import kotlin.test.assertEquals

class FooTest {
    @Test
    fun filterState(){
        assertEquals(1, 2)
    }
}
I get
Unresolved reference: Test
e: ... /src/commonTest/kotlin/my/package/FooTest.kt: (3, 20): Unresolved reference: Test
e: ... /src/commonTest/kotlin/my/package/FooTest.kt: (4, 20): Unresolved reference: assertEquals
Any idea what I have misconfigured?
g

grandstaish

01/03/2020, 6:48 PM
I saw this once when my top level source folder for the common module was called
java
instead of
kotlin
h

Hannes

01/03/2020, 6:49 PM
thanks, I double checked it, it's called
kotlin
k

Kris Wong

01/03/2020, 6:59 PM
you need the test dependencies for your test targets as well
if you are running a common test then it is probably defaulting to running using your JVM target, assuming you have one
h

Hannes

01/03/2020, 7:06 PM
What do you mean exactly? I only have
commonMain
, I just have a bunch of pure kotlin code in this library project that has no
expected / actual
platform specific implementation and therefore also no
jvmTest
etc.
k

Kris Wong

01/03/2020, 7:11 PM
you have to have a target
otherwise what are you building?
common source exists only to be shared with each of your targets
h

Hannes

01/03/2020, 7:15 PM
I have
Copy code
kotlin {
    jvm()
    js()
    macosX64()
    iosArm32()
    iosArm64()
    iosX64()
    ... 
    sourceSets {
     ...
       commonTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test-common'
                implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
            }
        }
   }
k

Kris Wong

01/03/2020, 7:16 PM
right, so you have 6 targets. which one was used to run the tests?
h

Hannes

01/03/2020, 7:26 PM
Ah, now I see what you are pointing out (thanks a lot for taking the time and effort!) I just run
gradlew check
and it failed in
compileTestKotlinJvm
I guess I miss the dependencies in
Copy code
implementation 'org.jetbrains.kotlin:kotlin-test'
                implementation 'org.jetbrains.kotlin:kotlin-test-junit'
I had a different understanding how
commonTest
works
k

Kris Wong

01/03/2020, 7:28 PM
you will need the right dependencies for every target on which you want to run the tests
that same principle applies for
main
dependencies as well
h

Hannes

01/03/2020, 7:29 PM
yea you are right (and I found that totally unintuitive)
k

Kris Wong

01/03/2020, 7:32 PM
just like in your own project, common sources are not the full implementation. and common dependencies are also not the full implementation.
i mean it's possible, but i haven't seen any libraries where that's the case
h

Hannes

01/03/2020, 7:38 PM
Thanks a lot for your help!
🍻 1
btw. is there a website / documentation that lists all the dependencies for each target? i.e. how do I find the kotlin test artifact for javascript?
k

Kris Wong

01/03/2020, 7:50 PM
the specific library should have it in their own docs. in this case, the new project template would also include it if it also generated the target.
👍 1
8 Views