应用程序测试用例在 API 级别 24 中已弃用



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

此类在 API 级别 24 中已弃用。使用活动测试规则 相反。应使用 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);
    }
}

我的问题:为什么Android测试用例现在被弃用了?如何用 ActivityTestRule 替换 ApplicationTestCase?


编辑:

我尝试使用 Expresso,但在 API 24compileSdkVersion 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'

我的结论是,目前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'
}

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'
})
如 API

文档中所示,API 已被弃用,而是使用 InstrumentationRegistry.getTargetContext() 将依次调用应用程序类的 onCreate 方法。

getTargetContext 将调用 Android Manifest 中定义的 ApplicationStartupService 类,如下所示。

    <?xml version="1.0" encoding="utf-8"?>
     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       <application
           android:name=".service.ApplicationStartupService"
 public class ApplicationStartupService extends Application
 {
       /**
         * Method initializes the application configuration
       */
       @Override
       public void onCreate(){
          super.onCreate();
          this.initResources()
       }
      private void initResource(){
          //do your application init work here.
      }
}
Test Class
  @RunWith(AndroidJUnit4.class)      
  public class ApplicationStartupServiceTest {
  @Test
  public void testResourcesAreInitializedd() throws Exception {
   //do your assertions here.
  }

https://developer.android.com/reference/android/test/ApplicationTestCase

我可以用什么替换

public class ApplicationTest extends ApplicationTestCase<Application> {
    public ApplicationTest() {
        super(Application.class);
    }
}

相关内容

  • 没有找到相关文章

最新更新