代码之家  ›  专栏  ›  技术社区  ›  Volodymyr

Android Studio单元测试支持vs机器人

  •  3
  • Volodymyr  · 技术社区  · 11 年前

    迄今为止,我使用JUnit4.x的机器人电子单元测试来测试我的业务逻辑。在Android Studio 1.1.0的最新版本中 native support 单元测试的 junit:4.+ .

    我应该拒绝使用机器人吗?机器人电有一些我可能不知道的独特优势吗?

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

    1 回复  |  直到 11 年前
        1
  •  2
  •   jimi312    11 年前

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

    这将允许您查看AndroidStudio中的机器人电测试结果以及简单的JUnit4.x测试

    如果使用渐变 robolectric example github repo 有一个如何做的例子。

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

    以下是建筑的相关部分。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'
        }
    }
    

    测试类不需要任何修改