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

mzgreen

12/31/2019, 1:28 PM
I’m trying to add a simple unit test to my project. I added:
Copy code
commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
And then in
commonTest/kotlin/
directory I have this file:
Copy code
class Test {

    @Test
    fun foo() {
        assertEquals(1,2,"Test failed")
    }
}
I run it
./gradlew :common:test
and I get errors like:
Copy code
> Task :common:compileDebugUnitTestKotlinAndroid FAILED
e: .../common/src/commonTest/kotlin/Test.kt: (1, 20): Unresolved reference: Test
e: .../common/src/commonTest/kotlin/Test.kt: (2, 20): Unresolved reference: assertEquals
e: .../common/src/commonTest/kotlin/Test.kt: (6, 5): 'Test' is not an annotation class
e: .../common/src/commonTest/kotlin/Test.kt: (8, 9): Unresolved reference: assertEquals
Not sure what is going on.
e

egorand

12/31/2019, 1:31 PM
Are you missing imports for
Test
and
assertEquals
?
Also, make sure they’re not
org.junit.*
m

mzgreen

12/31/2019, 1:53 PM
I have the imports and they're correct ones. IDE doesn't complain but it fails when I run the test
Here is a full source file:
Copy code
import kotlin.test.Test
import kotlin.test.assertEquals

class Test {

    @Test
    fun foo() {
        assertEquals(1,2,"Test failed")
    }
}
If I remove deps from build.gradle then IDE doesn’t see those classes as expected. Maybe the way I run the test is wrong?
Hmm the task that fails is
common:compileDebugUnitTestKotlinAndroid
🤔
but I’m not using serialization
k

Kris Wong

12/31/2019, 2:20 PM
you need the correct imports for jvm/android
Copy code
implementation(kotlin("test"))
                implementation(kotlin("test-junit"))
the common APIs are just expect declarations
m

mzgreen

12/31/2019, 2:22 PM
It worked, I get it now, thank you very much 🙏
🍻 1
6 Views