Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:不同的仪器测试应用程序ID_Android - Fatal编程技术网

Android:不同的仪器测试应用程序ID

Android:不同的仪器测试应用程序ID,android,Android,我的Android instrumentation测试需要修改/清除应用程序数据,因此我希望它们使用一个单独的applicationId,以便将私有数据目录和共享首选项等内容分开 在: 默认情况下,生成工具使用给定生成变量的应用程序ID(附加有.test)将应用程序ID应用于检测测试APK。例如,com.example.myapp.free构建变量的测试APK具有应用程序IDcom.example.myapp.free.test 虽然不需要,但可以通过在defaultConfig或product

我的Android instrumentation测试需要修改/清除应用程序数据,因此我希望它们使用一个单独的applicationId,以便将私有数据目录和共享首选项等内容分开

在:

默认情况下,生成工具使用给定生成变量的应用程序ID(附加有
.test
)将应用程序ID应用于检测测试APK。例如,
com.example.myapp.free
构建变量的测试APK具有应用程序ID
com.example.myapp.free.test

虽然不需要,但可以通过在
defaultConfig
productFlavor
块中定义
testApplicationId
属性来更改应用程序ID

为了测试这一点,我使用Android Studio 4.0.1创建了一个全新的Android应用程序。以下是完整的
构建.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "ch.dbrgn.myapplication"
        testApplicationId "ch.dbrgn.myapplication.test"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:rules:1.1.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}
如您所见,我显式地将
testApplicationId
设置为
ch.dbrgn.myapplication.test

为了测试它是否正常工作,我创建了一个仪器测试:

包ch.dbrgn.myapplication;
导入android.content.Context;
导入androidx.test.ext.junit.runners.AndroidJUnit4;
导入androidx.test.platform.app.InstrumentationRegistry;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入静态org.junit.Assert.assertEquals;
@RunWith(AndroidJUnit4.class)
公共类示例InstrumentedTest{
@试验
public void packageName(){
Context appContext=InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals(“ch.dbrgn.myapplication”,appContext.getPackageName());
}
@试验
公共无效应用程序ID(){
assertEquals(“ch.dbrgn.myapplication.test”,BuildConfig.APPLICATION\u ID);
}
}
包不应具有
.test
后缀,但应具有应用程序ID

但是,该测试失败:

ch.dbrgn.myapplication.ExampleInstrumentedTest > applicationId[MI 6 - 10] FAILED
    org.junit.ComparisonFailure: expected:<....dbrgn.myapplication[.test]> but was:<....dbrgn.myapplication[]>
    at org.junit.Assert.assertEquals(Assert.java:115)
ch.dbrgn.myapplication.ExampleInstrumentedTest>applicationId[MI 6-10]失败
org.junit.ComparisonFailure:应为:但为:
位于org.junit.Assert.assertEquals(Assert.java:115)
如何确保我的插装测试在一个单独的应用程序中运行,并且有它自己完全独立的数据?

因此,从中,您可以看到,实际上,当您进行插装测试时,有两个应用程序正在安装,一个正在进行插装,然后您的应用程序正在测试中。 此
testApplicationId
不是指被测应用程序的应用程序id,而是指执行检测的应用程序的应用程序id。(如果您想访问它,您必须使用
ch.dbrgn.myapplication
.test
.BuildConfig.APPLICATION\u ID
,但这对您没有帮助)

因此,要在执行检测测试时为被测应用程序提供单独的应用程序id,您必须手动定义一个额外的构建类型,并在那里指定另一个
applicationId
,然后将其用于测试。例如:

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        releaseUnderTest {
            initWith buildTypes.release
            applicationId "ch.dbrgn.myapplication.undertest"
        }
    }

谢谢你让我大开眼界。它解释了我在调试时遇到的问题,因为我使用了错误的概念模型。(我必须说,文档在解释这一点上做得很糟糕…)还感谢您指出“initWith”扩展了现有的构建类型,我不知道它的存在!