ApplicationTestCase在API级别24中已弃用

lop*_*ael 27 testing android android-testing

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

此类在API级别24中已弃用.请改用ActivityTestRule.应使用Android测试支持库编写新测试.

样品:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题:为什么现在不推荐使用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.
Run Code Online (Sandbox Code Playgroud)

当我尝试在build.gradle中添加此lib时:

// 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'
Run Code Online (Sandbox Code Playgroud)

我的结论是目前Android Test CaseExpresso都不适用于Android API 24.这是正确的吗?


编辑: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'
}
Run Code Online (Sandbox Code Playgroud)

fri*_*mle 20

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

就像弃用警告所暗示的那样,新的仪器测试应该使用InstrumentationRegistry而不是从中扩展AndroidTestCase.运行它们AndroidJUnit4.

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

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

  • 好的,但是如何替换“ApplicationTestCase”呢?它处理每个测试用例之间的应用程序设置和拆卸。 (3认同)
  • 我知道,我的问题更倾向于:这些方法的内容是什么**正确**每次创建一个新的`Application`实例而不泄漏,冲突并通过`context.getApplicationContext使该测试中创建的实例可用()`在应用程序内需要的地方? (3认同)