gpt4 book ai didi

android - API 级别 24 中不推荐使用 ApplicationTestCase

转载 作者:IT老高 更新时间:2023-10-28 22:10:02 25 4
gpt4 key购买 nike

我使用 API 24Android Studio 2.1.2 上创建了一个默认的空项目。在示例项目中,Google 提供了一个折旧类 ApplicationTestCase :

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?


编辑:

我尝试使用 Expresso ,但在 API 24 (compileSdkVersion 24) 上出现此错误:

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 中添加这个库时:

// 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'

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 上的先前错误:

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 示例,如下所示:

@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 运行它们。

build.gradle 中相关的dependencies 部分如下所示:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

关于android - API 级别 24 中不推荐使用 ApplicationTestCase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38308041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com