繁体   English   中英

Android Studio单元测试支持vs robolectric

[英]Android Studio unit testing support vs robolectric

到目前为止,我使用了JUnit 4.x的robolectric单元测试来测试我的业务逻辑。 在最新版本的Android Studio 1.1.0中宣布了对单元测试junit:4.+ 原生支持 junit:4.+ . junit:4.+

我应该拒绝使用robolectric吗? robolectric有一些我可能不知道的明显优势吗?

至于我使用Android Studio本机测试更方便,更简单。 在robolectric测试结果存储在html文件中,可以在浏览器中显示(这对我来说不方便)。 原生Android Studio测试结果显示在“运行”输出窗口中,如果某些测试失败,我们可以通过单击输出窗口中的错误轻松打开此行代码。

没有理由你不能同时使用它们。 Robolectric的优势在于您可以在需要时测试实际的Android行为。 Plain JUnit只允许您测试代码中不与Android交互的部分。

这将让您查看Android Studio中的robolectric测试结果以及简单的JUnit 4.x测试

如果您使用的是gradle,那么robolectric示例github repo就有一个如何操作的示例。

我目前正在迁移一些应用程序以使用此方法

以下是build.gradle的相关部分

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
    }
}
dependencies {
    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-core:1.1'
    testCompile 'org.hamcrest:hamcrest-library:1.1'
    testCompile 'org.hamcrest:hamcrest-integration:1.1'
    testCompile('org.robolectric:robolectric:2.4') {
        exclude module: 'classworlds'
        exclude module: 'commons-logging'
        exclude module: 'httpclient'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-provider-api'
    }
}

测试类不需要任何修改

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM