gpt4 book ai didi

Android:用于仪器测试的不同 applicationId

转载 作者:行者123 更新时间:2023-12-04 23:57:46 25 4
gpt4 key购买 nike

我的 Android 仪器测试需要修改/清除应用程序数据,因此我希望它们使用单​​独的 applicationId,以便将私有(private)数据目录和共享首选项等内容分开。
Android documentation, I read the following :

By default, the build tools apply an application ID to your instrumentation test APK using the application ID for the given build variant, appended with .test. For example, a test APK for the com.example.myapp.free build variant has the application ID com.example.myapp.free.test.

Although it shouldn't be necessary, you can change the application ID by defining the testApplicationId property in your defaultConfig or productFlavor block.


为了测试这一点,我使用 Android Studio 4.0.1 创建了一个全新的 Android 应用程序。这是完整的 build.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'

}
如您所见,我明确设置了 testApplicationIdch.dbrgn.myapplication.test .
为了测试它是否正常工作,我创建了一个仪器测试:
package ch.dbrgn.myapplication;

import android.content.Context;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void packageName() {
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("ch.dbrgn.myapplication", appContext.getPackageName());
}

@Test
public void applicationId() {
assertEquals("ch.dbrgn.myapplication.test", BuildConfig.APPLICATION_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)
如何确保我的仪器测试在具有自己完全独立数据的单独应用程序中运行?

最佳答案

所以从 this answer ,您可以看到,实际上当您进行仪器测试时,会安装 2 个应用程序,一个进行仪器测试,然后是您的被测应用程序。
这个testApplicationId不是指被测应用程序的应用程序 ID,而是指执行检测的应用程序的应用程序 ID。 (如果你想访问它,你必须使用 ch.dbrgn.myapplication .test .BuildConfig.APPLICATION_ID ,但这对你没有帮助)。
因此,要在进行仪器测试时为被测应用程序提供单独的应用程序 ID,您必须手动定义一个额外的 buildType 并指定另一个 applicationId在那里,然后将其用于测试。例如:

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

关于Android:用于仪器测试的不同 applicationId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63674186/

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