gpt4 book ai didi

Android ExampleInstrumentedTest 口味失败

转载 作者:行者123 更新时间:2023-11-28 21:22:41 24 4
gpt4 key购买 nike

我正在测试 Android 应用程序,默认的 PackageName 是 com.kagarlickij.myapplication

我的“开发”和“暂存”productFlavors 添加了 applicationIdSuffix,因此暂存应用程序的 PackageName 是 com.kagarlickij.myapplication.**staging**

当我运行 ./gradlew connectedStagingDebugAndroidTest 时,我遇到了这个异常:

    > Task :app:connectedStagingDebugAndroidTest
Starting 1 tests on Nexus_5X_API_27(AVD) - 8.1.0

com.kagarlickij.myapplication.ExampleInstrumentedTest > useAppContext[Nexus_5X_API_27(AVD) - 8.1.0] FAILED
org.junit.ComparisonFailure: expected:<...lickij.myapplication[]> but was:<...lickij.myapplication[.staging]>
at org.junit.Assert.assertEquals(Assert.java:115)


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:connectedStagingDebugAndroidTest'.
> There were failing tests. See the report at: file:///Users/kagarlickij/Repos/public/MyApplication/app/build/reports/androidTests/connected/flavors/STAGING/index.html

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 12s
50 actionable tasks: 7 executed, 43 up-to-date

默认ExampleInstrumentedTest.java:

package com.kagarlickij.myapplication;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

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

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("com.kagarlickij.myapplication", appContext.getPackageName());
}
}

build.gradle - https://github.com/kagarlickij/MyApplication/blob/dev/0.4.0/app/build.gradle

如何更改 ExampleInstrumentedTest.java 以支持来自 flavors 的 PackageNames

最佳答案

尝试将口味名称添加到您的测试中,例如:

assertEquals("com.kagarlickij.myapplication." + BuildConfig.FLAVOR , appContext.getPackageName());

或者:

assertEquals(BuildConfig.APPLICATION_ID, appContext.getPackageName());

关于Android ExampleInstrumentedTest 口味失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47790657/

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