简体   繁体   English

使用Android Studio和Gradle进行单元测试?

[英]Unit tests with Android Studio and Gradle?

How can I add unit tests to my Android projects in Android Studio (IntelliJ) easily? 如何轻松地在Android Studio(IntelliJ)中为我的Android项目添加单元测试?

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). 更确切地说:我想添加一个包含测试代码的文件夹(JUnit 4),并使用常规安装的JDK(不在Android模拟器中)从那里执行单元测试。 So far I added the folders 'test/java/' to my module and added 'test' as a source set and junit as a test dependency: 到目前为止,我将文件夹'test / java /'添加到我的模块中,并将'test'添加为源集,将junit添加为测试依赖项:

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

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

When I now select Run 'All Tests' on the test/java folder it gives me an UnsupportedOperationException. 当我现在在test / java文件夹中选择Run'All Tests'时,它会给我一个UnsupportedOperationException。

What am I missing? 我错过了什么? How do you run your unit tests for Android projects? 你如何运行Android项目的单元测试?

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. 对于插件的建议的加分点,其工作方式类似于Eclipse中的“Infinitest” - 我可以简单地保存一个类,并自动执行单元测试。 :-) :-)

PS: I do not want to use https://github.com/JakeWharton/gradle-android-test-plugin since that plugin seems to be deprecated already. PS:我不想使用https://github.com/JakeWharton/gradle-android-test-plugin,因为该插件似乎已被弃用。

I have spent the past weeks (also) on that. 我已经花了过去几周(也)。 Actually, on that on steroids, since I also had to put Robolectric into the pot. 实际上,关于类固醇,因为我还必须将Robolectric放入锅中。

Short answer: unless I confuse that error with something else, I think you are trying to run unit tests with the Android testrunner. 简短的回答:除非我把这个错误与其他东西混淆,否则我认为你正试图用Android testrunner进行单元测试。

Long answer: I ended up with the deprecated plugin (after trying it, and then trying to put tests into androidTest folder) because it solved quite some issues on Jenkins for me. 答案很长:我最终得到了弃用的插件(在尝试之后,然后尝试将测试放入androidTest文件夹中),因为它为我解决了Jenkins的一些问题。 The downside is that it does not recognizes the folder itself as java or android code, so code completion works with everything except the content of the folder (for example it sees correctly the base project and all dependencies). 缺点是它不能将文件夹本身识别为java或android代码,因此代码完成适用于除文件夹内容之外的所有内容(例如,它正确地查看了基础项目和所有依赖项)。

What I had to do run it in Android Studio was to create a JUnit (not Android) testrunner. 我在Android Studio中运行它所要做的就是创建一个JUnit(不是Android)testrunner。 It won't work out of the box probably, because the classpath order is messed up (although it works fine by just running tests in gradle). 它可能无法开箱即用,因为类路径顺序搞砸了(尽管只通过在gradle中运行测试它可以正常工作)。

Basically the manual steps I had to do are: 基本上我必须做的手动步骤是:

  1. Run the junit testrunner in order to see the command line used by AS 运行junit testrunner以查看AS使用的命令行
  2. Edit the -classpath argument doing the following: 编辑-classpath参数执行以下操作:

    • move junit > 3.8 at the top of the string (I guess because otherwise you will get the one within android, which is too old) 在字符串的顶部移动junit> 3.8(我想因为否则你将得到android中的那个,这太旧了)
    • move robolectric as second if you reference it, otherwise real android methods will be found before robolectric ones 如果您引用它,则将robolectric移动到第二个,否则将在robolectric之前找到真正的android方法
    • append your test classes output path to the end, so it will find your tests (remember to end your test classes with 'Test' otherwise they won't be found) 将测试类的输出路径追加到最后,这样它就会找到你的测试(记得用'Test'结束你的测试类,否则就找不到它们)
  3. Append the -classpath you just created to the runner VM options after -ea (I found this solution, or parts of it, in many places, a very good one is: http://kostyay.name/android-studio-robolectric-gradle-getting-work/ and it applies also to simple unit tests). -ea之后将刚创建的-classpath附加到运行者VM选项(我发现这个解决方案,或者在很多地方找到它的一部分,非常好的一个是: http//kostyay.name/android-studio-robolectric- gradle-getting-work /它也适用于简单的单元测试)。

An alternative I did not try but I considered has been suggested this morning (see last post): https://groups.google.com/forum/#!topic/adt-dev/Y8-ppkWell0 一个替代方案我没有尝试,但我认为今天早上有人建议(见上一篇文章): https//groups.google.com/forum/#!topic / sq-dev / Y8-ppkWell0

I have a lot of scattered knowledge right now due to the many issues I discovered due to preview SW and the tons of info I had to go through to solve them, but if you have more specific issue on the topic, just let me know :) 我现在有很多分散的知识,因为我发现由于预览SW而发现的许多问题以及我必须解决的大量信息,但是如果你对这个主题有更具体的问题,请告诉我: )

I think the answer should be now already in Android Studio 1.1 (and current gradle plugin). 我认为现在答案应该已经在Android Studio 1.1(以及当前的gradle插件)中。 They introduced unit test support. 他们引入了单元测试支持。 It's still experimental, but less so than our previous efforts here :) See here . 它仍然是实验性的,但不如我们此前的努力:)见这里

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

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