Java 为什么 Android 上的 JUnit 4 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9809180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is JUnit 4 on Android not working?
提问by Matthew
as the documentation of Android says, "Note that the Android testing API supports JUnit 3 code style, but not JUnit 4." (Testing Fundamentals). It should be clear that JUnit 4 cannot be used out of the box with Android.
正如 Android 的文档所说,“请注意,Android 测试 API 支持 JUnit 3 代码样式,但不支持 JUnit 4。” (测试基础知识)。应该清楚的是,JUnit 4 不能在 Android 上开箱即用。
But why is this the case? Is it because the tests are executed within the DVM (in that the Android Runtime only has support for JUnit 3)? On a JVM one on its own could choose the JUnit runtime that should be used. Isn't this possible within the DVM?
但为什么会这样呢?是否因为测试是在 DVM 中执行的(因为 Android 运行时仅支持 JUnit 3)?在 JVM 上,可以自行选择应该使用的 JUnit 运行时。这在 DVM 中是不可能的吗?
采纳答案by Ralf
Update 2015/10
更新 2015/10
It is now possible via the AndroidJUnitRunner, which is part of the Android Testing Support Library. In short, you need to do the following in a Gradle-based project:
现在可以通过 AndroidJUnitRunner 实现,它是Android 测试支持库的一部分。总之,你需要在一个基于 Gradle 的项目中做以下事情:
Add the dependencies:
dependencies { compile 'com.android.support:support-annotations:22.2.0' androidTestCompile 'com.android.support.test:runner:0.4.1' }
Specify the testInstrumentationRunner:
android { defaultConfig { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } }
Annotate your test class with
@RunWith(AndroidJUnit4.class)
.- Write your tests in normal JUnit4 style.
添加依赖项:
dependencies { compile 'com.android.support:support-annotations:22.2.0' androidTestCompile 'com.android.support.test:runner:0.4.1' }
指定 testInstrumentationRunner:
android { defaultConfig { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } }
使用
@RunWith(AndroidJUnit4.class)
.- 以正常的 JUnit4 风格编写您的测试。
Also see the Espresso setup instructions. Note that you don't need Espresso itself for plain JUnit4 tests.
另请参阅Espresso 设置说明。请注意,对于普通的 JUnit4 测试,您不需要 Espresso 本身。
Why it's needed
为什么需要它
There are very little information I could find on the internet on this topic. The best I found was the info on InstrumentationTestRunner.
我在互联网上找到的关于这个主题的信息很少。我发现的最好的是有关InstrumentationTestRunner的信息。
There are nothing preventing JUnit4 tests from running on an Android device just like any other Java code. However, the Android test framework does some additional work:
与任何其他 Java 代码一样,没有什么可以阻止 JUnit4 测试在 Android 设备上运行。然而,Android 测试框架做了一些额外的工作:
- It sends test results back to your development machine over ADB.
- It does instrumentation (I'm not sure what exactly this involves).
- 它通过 ADB 将测试结果发送回您的开发机器。
- 它进行检测(我不确定这到底涉及什么)。
The above is performed by some extensions specifically for JUnit3.
以上是由一些专门针对 JUnit3 的扩展执行的。
This JUnit3-specific code seems to be part of the InstrumentationTestRunner which forms part of the core Android system. However, as is evident with AndroidJUnitRunner, it is possible to use a custom test runner that supports JUnit4 or other frameworks.
这个特定于 JUnit3 的代码似乎是 InstrumentationTestRunner 的一部分,它构成了核心 Android 系统的一部分。但是,正如 AndroidJUnitRunner 所证明的那样,可以使用支持 JUnit4 或其他框架的自定义测试运行器。
回答by usethe4ce
An explanation (in Japanese) is here:
解释(日文)在这里:
http://d.hatena.ne.jp/esmasui/20120910/1347299594
http://d.hatena.ne.jp/esmasui/20120910/1347299594
I gather that the problem arises from the implementation of InstrumentationTestRunner
.
我认为问题出在InstrumentationTestRunner
.
The author's solution is the AndroidJUnit4project.
作者的解决方案是AndroidJUnit4项目。