使用 Android Studio 和 Gradle 进行单元测试? [英] Unit tests with Android Studio and Gradle?

查看:42
本文介绍了使用 Android Studio 和 Gradle 进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Android Studio (IntelliJ) 中轻松地向我的 Android 项目添加单元测试?

How can I add unit tests to my Android projects in Android Studio (IntelliJ) easily?

更准确地说:我想添加一个包含测试代码 (JUnit 4) 的文件夹,并使用常规安装的 JDK(不在 Android 模拟器中)从那里执行单元测试.到目前为止,我将文件夹test/java/"添加到我的模块中,并将test"添加为源集,将 junit 添加为测试依赖项:

To be more exact: I want to add a folder with test code (JUnit 4) and execute the unit tests from there using the regular installed JDK (not in an Android emulator). So far I added the folders 'test/java/' to my module and added 'test' as a source set and junit as a test dependency:

sourceSets {
  instrumentTest.setRoot('src/test')
}

dependencies {
  instrumentTestCompile 'junit:junit:4.+'
  // ...
}

当我现在在 test/java 文件夹上选择 Run 'All Tests' 时,它会给我一个 UnsupportedOperationException.

When I now select Run 'All Tests' on the test/java folder it gives me an UnsupportedOperationException.

我错过了什么?您如何为 Android 项目运行单元测试?

What am I missing? How do you run your unit tests for Android projects?

推荐一个像 Eclipse 中的Infinitest"一样工作的插件的奖励积分——我可以简单地保存一个类并自动执行它的单元测试.:-)

Bonus points for a recommendation of a plugin that works like 'Infinitest' in Eclipse - where I can simply save a class and its unit test is automatically executed. :-)

PS:我不想使用 https://github.com/JakeWharton/gradle-android-test-plugin 因为该插件似乎已被弃用.

PS: I do not want to use https://github.com/JakeWharton/gradle-android-test-plugin since that plugin seems to be deprecated already.

推荐答案

我认为答案现在应该已经在 Android Studio 1.1(和当前的 gradle 插件)中了.他们引入了单元测试支持.它仍然是实验性的,但比我们之前在这里的努力要少:)请参阅此处.

I think the answer should be now already in Android Studio 1.1 (and current gradle plugin). They introduced unit test support. It's still experimental, but less so than our previous efforts here :) See here.

这篇关于使用 Android Studio 和 Gradle 进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆