# Multiplatform project - testing coroutines in common module

**URL:** <https://discuss.kotlinlang.org/t/multiplatform-project-testing-coroutines-in-common-module/13251>\
**Category:** Multiplatform\
**Created:** [June 28, 2019, 10:16am UTC](https://discuss.kotlinlang.org/t/multiplatform-project-testing-coroutines-in-common-module/13251 "2019-06-28T10:16:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcin](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@marcin](https://discuss.kotlinlang.org/u/marcin)\
**Post date:** [June 28, 2019, 10:16am UTC](https://discuss.kotlinlang.org/t/multiplatform-project-testing-coroutines-in-common-module/13251/1 "2019-06-28T10:16:43Z")

</div>

I have difficulties integrating kotlin testing framework for coroutines `kotlinx.coroutines.test` with multiplatform project that targets Android (and ios in the future)

My common code relies heavily on coroutines but I’m unable to test them (it looks like the testing library is not in the classpath?)

`build.gradle` for common module:

```auto
plugins {
    id 'org.jetbrains.kotlin.multiplatform'
    id 'com.android.library'
    id 'kotlin-kapt'
}

android {
    defaultConfig {
        compileSdkVersion 28
        javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true
    }
    lintOptions {
        checkAllWarnings true
    }
}

def coroutinesVersion = "1.3.0-M2"
def mockKVersion = "1.9.3"

kotlin {
    targets {
        fromPreset(presets.android, 'android')
    }

    sourceSets {
        commonMain.dependencies {
            //Kotlin
            implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
            implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" //used in Log implementation
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion"
        }
        commonTest.dependencies {
            //Kotlin
            implementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
            implementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
            //Coroutines testing
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion"
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"
            implementation "io.mockk:mockk-common:$mockKVersion"
        }
        androidMain.dependencies {
            implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
        }
        androidTest.dependencies {
            implementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
            implementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
            implementation "io.mockk:mockk:$mockKVersion"
        }
    }
}

/*
* Due to the current limitations, it requires that the Android target is created before the kapt dependencies are configured,
* which needs to be done in a top-level dependencies { ... } block rather than within Kotlin source sets dependencies.
*/
dependencies {
    implementation 'javax.annotation:javax.annotation-api:1.3.2'
    kapt 'com.google.auto.factory:auto-factory:1.0-beta6@jar'
    compileOnly "com.google.auto.factory:auto-factory:1.0-beta6"
}

```

Example test in `commonTest` source set:

```auto
package some.package.common.test

import kotlin.test.Test
import kotlin.test.assertTrue

class SomeTest {

    @Test
    fun `should pass`() {
        //none of the kotlinx.coroutines.test content is available here (eg. runBlockingTest)
        assertTrue { 2 + 2 == 4 }
    }
}

```

What is the preffered way to test methods (`suspended` functions, ordinary functions starting new coroutines via `launch`, etc.) that are implemented in commonMain sourceSet?

---

<div class="post-metadata">

**Author:** ![seekdasky](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/seekdasky/32/4673_2.png) [@seekdasky](https://discuss.kotlinlang.org/u/seekdasky)\
**Post date:** [June 28, 2019, 7:57pm UTC](https://discuss.kotlinlang.org/t/multiplatform-project-testing-coroutines-in-common-module/13251/2 "2019-06-28T19:57:51Z")

</div>

`kotlinx.coroutines.test` is a JVM library, you can’t use it in common code. The best way to test multiplatform coroutines is to build your own multiplatofme `runTest` function that will use `runBlocking` on JVM/Native and `Promise` on JS

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [June 29, 2019, 1:38pm UTC](https://discuss.kotlinlang.org/t/multiplatform-project-testing-coroutines-in-common-module/13251/3 "2019-06-29T13:38:22Z")

</div>

You can run JVM tests in an Android module. But common code cannot be directly tested, it can only be tested on a platform. The tests can be common code and shared for all platforms. In that case you can run them on all supported platforms.

---

<div class="post-metadata">

**Author:** ![marcin](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@marcin](https://discuss.kotlinlang.org/u/marcin)\
**Post date:** [July 1, 2019, 11:09am UTC](https://discuss.kotlinlang.org/t/multiplatform-project-testing-coroutines-in-common-module/13251/4 "2019-07-01T11:09:02Z")

</div>

Thank you for the explanation.
