Run tests

By default, we support running tests for JVM, JS, Android, Linux, Windows, macOS as well as iOS, watchOS, and tvOS simulators. To run tests for other Kotlin/Native targets, you need to configure them manually in an appropriate environment, emulator, or test framework.

To run tests for all targets, run the check task.

To run tests for a particular target suitable for testing, run a test task <targetName>Test.

The kotlin.test API is available for multiplatform tests. When you create a multiplatform project, the Project Wizard automatically adds test dependencies to common and platform-specific source sets. If you didn’t use the Project Wizard to create your project, you can add the dependencies manually.

For testing shared code, you can use actual declarations in your tests.

For example, to test the shared code in commonMain:

  1. expect object Platform {
  2. val name: String
  3. }
  4. fun hello(): String = "Hello from ${Platform.name}"
  5. class Proxy {
  6. fun proxyHello() = hello()
  7. }

You can use the following test in commonTest:

  1. import kotlin.test.Test
  2. import kotlin.test.assertTrue
  3. class SampleTests {
  4. @Test
  5. fun testProxy() {
  6. assertTrue(Proxy().proxyHello().isNotEmpty())
  7. }
  8. }

And the following test in iosTest:

  1. import kotlin.test.Test
  2. import kotlin.test.assertTrue
  3. class SampleTestsIOS {
  4. @Test
  5. fun testHello() {
  6. assertTrue("iOS" in hello())
  7. }
  8. }

You can also learn how to create and run multiplatform tests in this tutorial.