为什么JUnit 4中,在Android上不工作? [英] Why is JUnit 4 on Android not working?
问题描述
由于Android的文件说,需要注意的是Android的测试API支持的JUnit 3 code风格,但不是的JUnit 4。 (测试基础)。它应当清楚,JUnit 4中不能用于用机器人的方块。
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.
但是,为什么是这样?是不是因为测试全部在DVM中执行(在选择JUnit 3 Android的运行时间只有支持)?关于其自己的一个JVM人们可以选择的JUnit运行时应该使用。这不是可能的DVM内?
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?
推荐答案
更新一十分之二千○十五
有经由AndroidJUnitRunner,这是机器人的一部分,现在可以测试支持库的。总之,你需要做以下的摇篮为基础的项目:
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:
-
添加依赖关系:
Add the dependencies:
dependencies {
compile 'com.android.support:support-annotations:22.2.0'
androidTestCompile 'com.android.support.test:runner:0.4.1'
}
指定testInstrumentationRunner:
Specify the testInstrumentationRunner:
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
与 @RunWith(AndroidJUnit4.class)标注的测试类
。
另请参阅居preSSO设置说明。请注意,你并不需要长者preSSO本身对于普通JUnit4测试。
Also see the Espresso setup instructions. Note that you don't need Espresso itself for plain JUnit4 tests.
为什么需要它
有很少的信息,我能找到关于这一主题在互联网上。我发现最好是在 InstrumentationTestRunner 。
There are very little information I could find on the internet on this topic. The best I found was the info on InstrumentationTestRunner.
有什么preventing JUnit4的,就像任何其他的Java code在Android设备上运行测试。然而,Android的测试框架做一些额外的工作:
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:
- 这将测试结果反馈给您的开发机器在亚洲开发银行。
- 它确实仪表(我不知道究竟这涉及到)。
以上是通过一些扩展专为JUnit3执行。
The above is performed by some extensions specifically for JUnit3.
这JUnit3专用code似乎是InstrumentationTestRunner的一部分,形成核心的Android系统的一部分。然而,如明显与AndroidJUnitRunner,可以使用自定义的测试运行,支持JUnit4或其他框架
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.
这篇关于为什么JUnit 4中,在Android上不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!