标签: android-instrumentation

使用 Workmanager 进行 Hilt Instrumentation 测试无法正常工作

当我尝试在包含 WorkManager 的应用程序中运行 ActivityScenario 时,我在启动时收到以下错误:

java.lang.IllegalStateException: WorkManager is not initialized properly.  You have explicitly disabled WorkManagerInitializer in your manifest, have not manually called WorkManager#initialize at this point, and your Application does not implement Configuration.Provider.
Run Code Online (Sandbox Code Playgroud)

使用工件WorkManagerTestInitHelper中的work-test也没有帮助。

WorkManager 的定义如下:

@Provides
@Singleton
fun provideWorkmanager(@ApplicationContext context: Context) = WorkManager.getInstance(context)
Run Code Online (Sandbox Code Playgroud)

这是我的测试自动取款机:

    @HiltAndroidTest
    @RunWith(AndroidJUnit4::class)
    class LoginTest {
    
        @get:Rule(order = 0)
        var hiltRule = HiltAndroidRule(this)
    
        @get:Rule(order = 1)
        val activityRule = ActivityScenarioRule(MainActivity::class.java)
    
        @Before
        fun before() {
            val context = InstrumentationRegistry.getInstrumentation().targetContext
            val config = …
Run Code Online (Sandbox Code Playgroud)

android android-espresso android-instrumentation android-workmanager dagger-hilt

6
推荐指数
1
解决办法
1464
查看次数

错误:您当前选择的变体(未知输出)的 apk 未签名。请为此变体指定签名配置(调试)

升级到 Android Studio Arctic Fox (2020.3.1) Canary 9 后。我无法运行我的测试。它弹出Edit Configurations窗口以显示Error: The apk for your currently selected variant (Unknown output) is not signed. Please specify a signing configuration for this variant (debug).消息。我似乎无法通过.jks为调试变体以及大多数建议的帖子创建和使用发布签名配置来解决这个问题。我有点犹豫是降级还是尝试以某种方式解决它。有没有人以前遇到过这个问题?欢迎任何建议和意见。

在此处输入图片说明

Android Studio Arctic Fox (2020.3.1) Canary 10 版本同样出现问题。

android unit-testing android-studio android-instrumentation build-variant

6
推荐指数
5
解决办法
1873
查看次数

生成 Jacoco 报告时排除 android Instrumentation 测试

我的 Android 应用程序中有多个模块。我需要在 CI/CD 管道中自动执行代码覆盖率报告,其中 \xe2\x80\x99t 没有物理/虚拟 Android 设备,并且无法附加设备。

\n

集成 Jacoco 来自 - https://github.com/gouline/android-samples/blob/master/jacoco/jacoco.gradle

\n

当我尝试通过 gradle 命令生成覆盖率报告时

\n
>> ./gradlew createDebugCoverageReport\n
Run Code Online (Sandbox Code Playgroud)\n

它失败并显示以下错误日志

\n
* What went wrong:\nExecution failed for task \':app:connectedDebugAndroidTest\'.\n> com.android.builder.testing.api.DeviceException: No connected devices!\n
Run Code Online (Sandbox Code Playgroud)\n

所以我尝试通过命令排除 gradle 任务 (connectdDebugAndroidTest)

\n
>> ./gradlew createDebugCoverageReport -x app:connectedDebugAndroidTest -x module1:connectedDebugAndroidTest\n
Run Code Online (Sandbox Code Playgroud)\n

出现如下错误 -

\n
> Task :module1:createDebugAndroidTestCoverageReport FAILED\n\nFAILURE: Build failed with an exception.\n\n* What went wrong:\nExecution failed for task \':vpn:createDebugAndroidTestCoverageReport\'.\n> java.io.IOException: No coverage data to process in directories [/Users/abc/ws/prjName/module1/build/outputs/code_coverage/debugAndroidTest/connected]\n …
Run Code Online (Sandbox Code Playgroud)

android gradle jacoco android-instrumentation

6
推荐指数
1
解决办法
998
查看次数

Mockito 无法在仪器测试中工作

当我在仪器中使用mockito时出现错误。

这是我正在使用的库:

def mockitoVersion = "3.10.0"
androidTestImplementation "org.mockito:mockito-android:$mockitoVersion"
Run Code Online (Sandbox Code Playgroud)

我添加了一个文件resources/mockito-extensions/org.mockito.plugins.MockMaker,其中包含mock-maker-inline

当我尝试模拟任何对象时,这是一个要模拟的示例类:

open class TestFile {
    val hit=12
}
Run Code Online (Sandbox Code Playgroud)

这里我们嘲讽一下:

class InsTest {

   @Mock
   lateinit var testFile: TestFile

   @Before
   fun init(){
       MockitoAnnotations.initMocks(this)
   }

     @Test
     fun shouldCallMockedListener(){
         assertTrue(testFile.hit==0)
    }
}
Run Code Online (Sandbox Code Playgroud)

也尝试了这个,但返回了同样的错误:

val testFile = Mockito.mock(TestFile::class.java)
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:84)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy4.isTypeMockable(Unknown Source)
at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:33)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:250)
at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:232)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:83)
at org.mockito.Mockito.mock(Mockito.java:1954)
at org.mockito.Mockito.mock(Mockito.java:1865)
at com.example.mytestlab.InsTest.shouldCallMockedListener(InsTest.kt:24)
... 26 …
Run Code Online (Sandbox Code Playgroud)

android mockito kotlin android-instrumentation

6
推荐指数
0
解决办法
707
查看次数

Android单元测试 - 如何使用ServiceTestCase测试服务

我在Android Studio中为服务创建单元测试时遇到问题.我已经设置了我的项目来执行单元测试,并成功地为不同的(非服务)类设置了测试.我可以运行这些测试并让它们通过.

这是我的ServiceTestCase的代码:

package com.roche.parkinsons.service;

import android.content.Intent;
import android.test.ServiceTestCase;
import android.util.Log;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class GeneralServiceTest extends ServiceTestCase<GeneralService> {

    /** Tag for logging */
    private final static String TAG = GeneralServiceTest.class.getName();

    public GeneralServiceTest() {
        super(GeneralService.class);
    }
//
    @Before
    public void setUp() throws Exception {
        super.setUp();

        Log.d(TAG, "Setup complete");
    }

    @After
    public void tearDown() throws Exception {
        Log.d(TAG, "Teardown complete");


        super.tearDown();
    }

    @Test
    public void testOnCreate() throws Exception {
        Intent intent = new Intent(getSystemContext(), GeneralService.class);
        startService(intent);
        assertNotNull(getService()); …
Run Code Online (Sandbox Code Playgroud)

java android unit-testing android-service android-instrumentation

5
推荐指数
0
解决办法
1957
查看次数

Jenkins和运行AndroidJUnitRunner工具测试

我有一个要与Jenkins建立的Android应用程序。我已经使用Android Emulator Plugin启动了一个模拟器,并使用gradle脚本构建了该项目,但是我无法让它运行我使用AndroidJUnitRunner编写的简单测试。

我从詹金斯的输出看起来像这样...

+ adb shell pm list instrumentation
instrumentation:com.android.emulator.connectivity.test/android.test.InstrumentationTestRunner (target=com.android.emulator.connectivity.test)
instrumentation:com.android.emulator.gps.test/android.test.InstrumentationTestRunner (target=com.android.emulator.gps.test)
instrumentation:com.android.smoketest.tests/com.android.smoketest.SmokeTestRunner (target=com.android.smoketest)
instrumentation:com.android.smoketest.tests/android.test.InstrumentationTestRunner (target=com.android.smoketest)
instrumentation:com.example.android.apis/.app.LocalSampleInstrumentation (target=com.example.android.apis)
[Short Sounds] $ /bin/sh -xe /tmp/hudson7415767398022941631.sh
+ adb shell am instrument -w com.sloths.speedy.shortsounds/android.support.test.runner.AndroidJUnitRunner
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.sloths.speedy.shortsounds/android.support.test.runner.AndroidJUnitRunner}
INSTRUMENTATION_STATUS_CODE: -1
android.util.AndroidException: INSTRUMENTATION_FAILED: com.sloths.speedy.shortsounds/android.support.test.runner.AndroidJUnitRunner
Run Code Online (Sandbox Code Playgroud)

如您所见,我已经通过shell命令列出了adb工具。在列表中找不到AndroidJUnitRunner。我是半积极的,应该在那里可以正常工作。

我在build.gradle文件中添加了适当的配置标签...即...

defaultConfig { 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Run Code Online (Sandbox Code Playgroud)

我还添加了这些依赖项。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    ...
    androidTestCompile 'com.android.support.test:runner:0.2'
    // Set this dependency to use JUnit 4 rules
    androidTestCompile 'com.android.support.test:rules:0.2'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
} …
Run Code Online (Sandbox Code Playgroud)

build-automation android junit4 jenkins android-instrumentation

5
推荐指数
2
解决办法
3810
查看次数

由于"进程崩溃",仪表运行失败.经过长时间的测试执行

我有大约700个测试要执行.当我全部运行时,出现了崩溃

"由于'进程崩溃,仪表运行失败.'"检查设备logcat以获取详细信息.测试运行失败:由于"进程崩溃",仪表运行失败.

经过一段时间的执行,大约10分钟和~360-370th执行测试.

Logcat不包含有关此崩溃的任何信息

它适用于从Android Studio,cmd(在PC和Mac上)运行.使用的设备 - Android 4.1.1上的Samsung S3

build.gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 18
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.xxx.yyy"
        minSdkVersion 9
        targetSdkVersion 18

        testApplicationId "com.xxx.zzz"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }

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

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

    project.gradle.taskGraph.whenReady {
        connectedAndroidTestDebug  {
            ignoreFailures = true
        }
    }
}


repositories {
    // The …
Run Code Online (Sandbox Code Playgroud)

crash android automated-tests unit-testing android-instrumentation

5
推荐指数
1
解决办法
6190
查看次数

为Spoon Gradle Plugin设置applicationApk和instrumentationApk

我想设置.apk文件,用于使用SpoonGradlePlugin运行我的测试.

我可以通过gradle文件以编程方式设置可用的属性:

https://github.com/stanfy/spoon-gradle-plugin/blob/master/src/main/groovy/com/stanfy/spoon/gradle/SpoonExtension.groovy

但我的项目有各种口味和名称,我想测试它们.通过当前设置,我得到:

* What went wrong:
A problem was found with the configuration of task ':app:spoonDebugAndroidTest'.
> File '/Users/F1sherKK/Dev/MyApp-Android/app/build/outputs/apk/app-debug.apk' specified for property 'applicationApk' does not exist.
Run Code Online (Sandbox Code Playgroud)

我的构建名称是:

app-debug-androidTest-unaligned.apk
MyApp-debugA-1.2.3-201.apk
MyApp-debugB-1.2.3-201.apk
MyApp-debugC-1.2.3-201.apk
Run Code Online (Sandbox Code Playgroud)

这就是为什么我想在gradle代码中设置我的.apk - 或者控制台.到目前为止我发现Spoon Gradle插件中有可用的字段:

https://github.com/stanfy/spoon-gradle-plugin/blob/master/src/main/groovy/com/stanfy/spoon/gradle/SpoonRunTask.groovy

名字:

  /** Instrumentation APK. */
  @InputFile
  File instrumentationApk

  /** Application APK. */
  @InputFile
  File applicationApk
Run Code Online (Sandbox Code Playgroud)

但我不能在SpoonExtension.groovy中访问gradle中的属性.

有没有办法设置这些字段?

//编辑 - 添加了一些尝试:这是我的基础配置:

spoon {
    debug = true
    baseOutputDir = file("$buildDir/spoon-log")
    if (project.hasProperty('spoonClassName')) {
        className = project.spoonClassName

        if (project.hasProperty('spoonMethodName')) {
            methodName = project.spoonMethodName
        }
    } …
Run Code Online (Sandbox Code Playgroud)

groovy android gradle spoon android-instrumentation

5
推荐指数
2
解决办法
1416
查看次数

AndroidTest从命令行运行时失败:app:connectedDebugAndroidTest

我正在使用android spring RestTemplate在我的应用程序上进行REST服务调用.我添加了android Instrumentation测试,其中包括模拟REST服务调用.从android studio运行时,我的所有测试运行正常,但测试无法在终端使用时加载spring mock类

   ./gradlew clean :app:connectedDebugAndroidTest
Run Code Online (Sandbox Code Playgroud)

spring.gradle上的spring测试依赖项

    androidTestCompile("org.springframework:spring-test:3.2.8.RELEASE")
Run Code Online (Sandbox Code Playgroud)

如果我改为compile而不是androidTestCompile,Android测试从终端正常运行.由于我不希望这种依赖我的生产APK任何帮助,赞赏.

这是开始测试时的日志

08-05 00:04:12.585 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.asm.commons.JSRInlinerAdapter 08-05 00:04:12.587 22274-22302/com.libin .androiduitesting E/TestLoader:找不到类:org.springframework.asm.commons.TryCatchBlockSorter 08-05 00:04:12.593 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework. cglib.transform.AbstractProcessTask 08-05 00:04:12.593 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.cglib.transform.AbstractTransformTask 08-05 00:04:12.603 22274- 22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.core.convert.support.ConvertingPropertyEditorAdapter 08-05 00:04:12.610 22274-22302/com.libin.androiduitesting E/TestLoader:无法find class:org.springframework.core.io.ResourceEditor 08-05 00:04:12.611 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.spring framework.core.io.support.ResourceArrayPropertyEditor 08-05 00:04:12.617 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.mock.http.MockHttpInputMessage 08-05 00:04 :12.618 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.mock.http.MockHttpOutputMessage 08-05 00:04:12.618 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.mock.http.client.MockClientHttpRequest 08-05 00:04:12.618 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.mock.http. client.MockClientHttpResponse 08-05 00:04:12.618 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.mock.jndi.ExpectedLookupTemplate 08-05 00:04:12.619 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.mock.jndi.SimpleNamingContext 08-05 00:04:12.619 22274-22302/com.libin.androiduitesting …

android spring-test android-espresso spring-android android-instrumentation

5
推荐指数
1
解决办法
1084
查看次数

在检测测试期间,库gradle模块中的资源的ResourceNotFoundException

在仪器测试期间,我的应用程序抛出以下错误.我的非测试版本中没有抛出此错误.

android.content.res.Resources$NotFoundException: String resource ID #0x7f030002
Run Code Online (Sandbox Code Playgroud)

我有两个gradle模块

:app (android-application) //applicationId: com.app
:library (android-library) //applicationId: com.library
//:app includes :library as a dependency
Run Code Online (Sandbox Code Playgroud)

有问题的资源在:library模块中定义并抛出.

在仪器下,我的:library模块调用:

  • context.getResources().getString(com.library.R.string.pref_key) - 这会引发异常

如果我在检测期间调试断点并调用:

  • context.getResources().getString(com.app.R.string.pref_key) - 这个有效!但这是我的:library模块中的代码,它没有依赖性:app.

我怎样才能解决这个问题?注 - 错误仅发生在检测测试期间,而不是正常构建期间.

更新:在检测中,调用getString(com.library.R.string.pref_key):app模块中的类调用时起作用,但是从:library模块中的类抛出异常.从字面上看,Resources当从:appvs 调用时,在同一个实例上调用完全相同的方法会有不同的行为:library.

android android-testing android-gradle-plugin android-instrumentation

5
推荐指数
1
解决办法
259
查看次数