Android Studio 1.1,简单的 junit 测试设置

Android Studio 1.1, simple junit test setup

我已经阅读过,有很多广泛的答案(比如 this one) but the Android world evolves so fast that they seem to be a bit outdated and the official documentation 仍然指的是带有 ADT 的 Eclipse。

我是 运行ning AS 1.1,我正在尝试在模拟器上为 运行 设置简单的 junit 测试,没有 Robolectric。如果我不在我的 build.gradle 中包含 junit,它就找不到 @After@Before@Test,我会得到 package org.junit does not exist。添加后

// unit tests
androidTestCompile 'junit:junit:4.11'

错误变为

Error:duplicate files during packaging of APK
[...]/app/build/outputs/apk/app-debug-test-unaligned.apk
Path in archive: LICENSE.txt
Origin 1: [...]/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar
Origin 2: [...]/.gradle/caches/modules-2/files-2.1/junit/junit/4.11/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar
You can ignore those files in your build.gradle:
android {
  packagingOptions {
    exclude 'LICENSE.txt'
  }
}

按照排除 LICENSE.txt 的控制台建议,它可以正常工作,但感觉像是 hack。所以我想知道,我是否可能遗漏了什么?谢谢。

Android Studio 单元测试支持来自 1.1 Beta 4 (release announcement) 和 Gradle 插件版本 1.1.0-rc1official document 中的更多信息。

不过目前它是实验性功能。例如。它 breaks installDebug gradle 任务。

为了在仪器测试中使用 JUnit,good guide for Espresso library and another 涵盖了新的 AndroidJUnitRunner。

如果有用的话,我设置了一个样板项目,允许通过使用切换构建变体来使用单元测试和 Espresso 测试。您不需要使用任何第三方插件。

https://github.com/hitherejoe/Android-Boilerplate