简体   繁体   English

Android Studio 0.8中的单元测试

[英]Unit tests in Android Studio 0.8

BREAKING NEWS: Android Studio 1.1.0 now has built-in support for unit testing. 突发新闻: Android Studio 1.1.0现在内置了对单元测试的支持。 See more here: https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support 点击此处了解详情: https//sites.google.com/a/android.com/tools/tech-docs/unit-testing-support

At first I know that there are very, very, very much similar questions (even here on SO). 起初我知道有很多非常非常相似的问题(即使在这里也是如此)。 Most of them are terribly outdated so I'm asking a fresh new question which should be up to date for Android Studio 0.8.x. 他们中的大多数都非常过时,所以我要问一个新的问题,这个问题应该是Android Studio 0.8.x的最新版本。

I know there are some libraries which work somehow, but in almost each case I had no evidence that the information were up to date. 我知道有些库可以某种方式工作,但几乎在每种情况下我都没有证据表明这些信息是最新的。 I know there is eg roboelectric , there are also some deprecated Jake Wharton like gradle-android-test-plugin or double-espresso , I also found the library RoboSpock and Deckard . 我知道有例如roboelectric ,还有一些不赞成的Jake Wharton,比如gradle-android-test-plugindouble-espresso ,我还找到了图书馆RoboSpockDeckard But none of them seems to have any Android Studio integration. 但它们似乎都没有任何Android Studio集成。

After long reseach I found this two bugs in all implementations: 经过长时间的研究,我在所有实现中发现了这两个错误:

  • The classpath is broken and needs to been hacked to get junit running 类路径被破坏,需要被黑客攻击才能运行junit
  • The junit tests cannot been debugged junit测试无法调试

What I'm looking for: 我在找什么:

  • I want to automatically test an algorithm (which is plain java) 我想自动测试一个算法(这是普通的java)
  • I want to check the integration in my Android app works 我想检查我的Android应用程序中的集成是否有效
  • I want an integration in Android Studio so that I can jump into the code out of a stacktrace 我想在Android Studio中进行集成,以便我可以从堆栈跟踪中跳转到代码中
  • I want to step thrue the test code 我想步骤测试代码

I found also somewhere a nice hack which run the tests in gradle as an external task and pushed the results into AS so that the results could been displayed, but unfortunately I cannot find that link again (and if I remember correctly it did also not work for me). 我发现某个地方还有一个很好的黑客,它将gradle中的测试作为一个外部任务运行,并将结果推送到AS,以便显示结果,但遗憾的是我再也找不到该链接(如果我没记错的话,它也无法正常工作为了我)。

Do you guys have some fresh references? 你们有新鲜的参考资料吗? Does it work for you? 对你起作用吗?

You have to edit the .iml file that Android Studio generates to point to your test-classes directory and also to fix the Stub error from Junit. 您必须编辑Android Studio生成的.iml文件以指向您的测试类目录,并修复Junit中的Stub错误。 There is currently no work around for this. 目前没有解决此问题的方法。

Gradlectric is a sample that uses the Robolectric Gradle Plugin to run unit tests in Android Studio. Gradlectric是一个使用Robolectric Gradle Plugin在Android Studio中运行单元测试的示例。

Here you miss one example project setup https://github.com/nenick/android-gradle-template 在这里,您将错过一个示例项目设置https://github.com/nenick/android-gradle-template

But none of them seems to have any Android Studio integration. 但它们似乎都没有任何Android Studio集成。

This project is maintained for android studio 这个项目是为android studio维护的

The classpath is broken and needs to been hacked to get junit running 类路径被破坏,需要被黑客攻击才能运行junit

gradle scripts will fix the classpath gradle脚本将修复类路径

The junit tests cannot been debugged junit测试无法调试

tests a running inside AS so they are easy to debug 测试在AS内部运行,以便它们易于调试

This project of mine does not cover all your requests, but a fair lot of them. 我的这个项目并没有涵盖你的所有要求,但其中包括很多。 It covers a default Java test case, and some tests interacting with Views. 它涵盖了一个默认的Java测试用例,以及一些与Views交互的测试。

The root project uses Travis CI, which uses gradle connectedCheck to run the tests. 根项目使用Travis CI,它使用gradle connectedCheck来运行测试。 To be able to debug the tests, you can just create a test run configuration in AS and run it in debugging mode. 为了能够调试测试,您只需在AS中创建测试运行配置并在调试模式下运行它。

Note that this does not use any special Android testing frameworks like robolectric. 请注意,这不使用任何特殊的Android测试框架,如robolectric。

You can find a fork of Jake Wharton's gradle-android-test-plugin here . 你可以找到杰克沃顿gradle这个-Android的测试插件的一个分支在这里 This is compatible with AS 0.6 and is regularly maintained by Robolectric. 这与AS 0.6兼容,并由Robolectric定期维护。 You can look at this project to set up Robolectric in Android Studio with help of this plugin. 你可以看一下这个项目 ,借助这个插件在Android Studio中设置Robolectric。 You can achieve fair bit of functionality you mentioned with the help of robolectric-gradle-plugin. 在robolectric-gradle-plugin的帮助下,您可以实现您提到的相当多的功能。 You can also successfully debug your test code using AS. 您还可以使用AS成功调试测试代码。

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

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