【问题标题】:How to Write Kotlin Unit Test with kotlin-test?如何使用 kotlin-test 编写 Kotlin 单元测试?
【发布时间】:2017-07-16 17:35:31
【问题描述】:

我正在尝试使用 Kotlin 测试我的代码。尝试导入 kotlin.test 包时,没有任何显示。

我必须先在某处添加依赖项吗?之后如何运行单元测试?

【问题讨论】:

    标签: unit-testing testing kotlin


    【解决方案1】:

    kotlin.test 包被打包在kotlin-test 模块中,因此您需要在build.gradle 中添加依赖项,如下所示:

    dependencies{
       testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
    }
    

    除此之外,您还需要在build.gradle 中添加一个单元测试框架,例如junit-4,例如:

    dependencies{
       testCompile "junit:junit:4.12"
    }
    

    如何编写 JUnit 测试?您可以在junit.org 中看到它。

    【讨论】:

    • 我目前不在AndroidStudio,所以没有build.gradle文件。我只是创建了一个标准的IntelliJ Kotlin JVM项目。
    • @Rechunk 从 maven 存储库下载 junit4kotlin-test 的 jar,然后将它们作为库添加到您的 intellij 项目中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 2023-01-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多