在 Kotlin/JS 平台中运行测试

The Kotlin Multiplatform Gradle plugin lets you run tests through a variety of test runners that can be specified via the Gradle configuration.

When you create a multiplatform project, the Project Wizard automatically adds test dependencies to all the source sets. If you created your project without it, you can add the dependencies manually to make test annotations and functionality available for the JavaScript target:

【Kotlin】

  1. // build.gradle.kts
  2. kotlin {
  3. sourceSets {
  4. val commonTest by getting {
  5. dependencies {
  6. implementation(kotlin("test")) // This brings all the platform dependencies automatically
  7. }
  8. }
  9. }
  10. }

【Groovy】

  1. // build.gradle
  2. kotlin {
  3. sourceSets {
  4. commonTest {
  5. dependencies {
  6. implementation kotlin("test") // This brings all the platform dependencies automatically
  7. }
  8. }
  9. }
  10. }

You can tune how tests are executed in Kotlin/JS by adjusting the settings available in the testTask block in the Gradle build script. For example, using the Karma test runner together with a headless instance of Chrome and an instance of Firefox looks like this:

  1. kotlin {
  2. js {
  3. browser {
  4. testTask {
  5. useKarma {
  6. useChromeHeadless()
  7. useFirefox()
  8. }
  9. }
  10. }
  11. }
  12. }

For a detailed description of the available functionality, check out the Kotlin/JS reference on configuring the test task.

Please note that by default, no browsers are bundled with the plugin. This means that you’ll have to ensure they’re available on the target system.

To check that tests are executed properly, add a file src/jsTest/kotlin/AppTest.kt and fill it with this content:

  1. import kotlin.test.Test
  2. import kotlin.test.assertEquals
  3. class AppTest {
  4. @Test
  5. fun thingsShouldWork() {
  6. assertEquals(listOf(1,2,3).reversed(), listOf(3,2,1))
  7. }
  8. @Test
  9. fun thingsShouldBreak() {
  10. assertEquals(listOf(1,2,3).reversed(), listOf(1,2,3))
  11. }
  12. }

To run the tests in the browser, execute the jsBrowserTest task via IntelliJ IDEA, or use the gutter icons to execute all or individual tests:

Gradle browserTest task

Alternatively, if you want to run the tests via the command line, use the Gradle wrapper:

  1. ./gradlew jsBrowserTest

After running the tests from IntelliJ IDEA, the Run tool window will show the test results. You can click failed tests to see their stack trace, and navigate to the corresponding test implementation via a double click.

Test results in IntelliJ IDEA

After each test run, regardless of how you executed the test, you can find a properly formatted test report from Gradle in build/reports/tests/jsBrowserTest/index.html. Open this file in a browser to see another overview of the test results:

Gradle test summary

If you are using the set of example tests shown in the snippet above, one test passes, and one test breaks, which gives the resulting total of 50% successful tests. To get more information about individual test cases, you can navigate via the provided hyperlinks:

Stacktrace of failed test in the Gradle summary