API级别24中不推荐使用ApplicationTestCase [英] ApplicationTestCase deprecated in API level 24
问题描述
我使用 API 24 在 Android Studio 2.1.2 上创建了一个默认的空项目.在示例项目中,Google提供了一个已贬值的类 ApplicationTestCase :
I created a default empty project on Android Studio 2.1.2 with API 24. In the sample project, Google offers a depreciated class ApplicationTestCase:
该类已在API级别24中弃用.使用ActivityTestRule 反而.新测试应使用Android测试支持编写 图书馆.
This class was deprecated in API level 24. Use ActivityTestRule instead. New tests should be written using the Android Testing Support Library.
示例:
import android.app.Application;
import android.test.ApplicationTestCase;
/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
我的问题:为什么现在不推荐使用Android测试用例?如何用ActivityTestRule替换ApplicationTestCase?
My Question: Why Android Test Case is now deprecated? How to replace ApplicationTestCase by ActivityTestRule?
我尝试使用 Expresso ,但是在 API 24 (compileSdkVersion 24
)上出现此错误:
I try with Expresso, but on API 24 (compileSdkVersion 24
) I have this error:
Error:Conflict with dependency 'com.android.support:appcompat-v7'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Error:Conflict with dependency 'com.android.support:design'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Error:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Error:Conflict with dependency 'com.android.support:recyclerview-v7'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
当我尝试在我的build.gradle中添加此库时:
When I try to add this lib in my build.gradle:
// Android JUnit Runner
androidTestCompile 'com.android.support.test:runner:0.5'
// JUnit4 Rules
androidTestCompile 'com.android.support.test:rules:0.5'
// Espresso core
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
// Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
// Espresso-web for WebView support
androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2'
// Espresso-idling-resource for synchronization with background jobs
androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:2.2.2'
我的结论是,目前 Android测试用例和 Expresso 均不适用于 Android API 24 .是这样吗?
My conclusion is that for the moment neither Android Test Case nor Expresso works on Android API 24. Is this right?
2016-08-05
我这样修复了 Expresso 上的先前错误:
I fix previous error on Expresso like that:
def espressoVersion = '2.2.2'
def testRunnerVersion = '0.5'
androidTestCompile "com.android.support.test:rules:${testRunnerVersion}"
androidTestCompile "com.android.support.test.espresso:espresso-core:${espressoVersion}"
configurations.androidTestCompile.dependencies.each { androidTestCompileDependency ->
androidTestCompileDependency.exclude group: 'com.android.support'
}
推荐答案
Android Studio 2.2测试版生成的新 androidTest 示例如下:
The new androidTest example that the beta version of Android Studio 2.2 generates, look like this:
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("org.mypackage", appContext.getPackageName());
}
}
就像弃用警告所暗示的那样,新的仪器测试应使用InstrumentationRegistry
而不是从AndroidTestCase
扩展.用AndroidJUnit4
运行它们.
Just like the deprecation warning suggests, the new instrumentation tests should use InstrumentationRegistry
instead of extending from AndroidTestCase
. Run them with AndroidJUnit4
.
build.gradle
中相关的dependencies
部分如下所示:
The relevant dependencies
section in build.gradle
looks like this:
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
这篇关于API级别24中不推荐使用ApplicationTestCase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!