【问题标题】:Instrumentation run failed due to 'Process crashed.'由于“进程崩溃”,检测运行失败。
【发布时间】:2018-10-02 09:06:15
【问题描述】:

我想运行以下测试:

package com.xxx.yyy;

import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.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() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.xxx.yyy", appContext.getPackageName());
    }
}

但我在控制台中收到错误:

$ adb shell am instrument -w -r   -e debug false -e class 'com.xxx.yyy.ExampleInstrumentedTest' com.xxx.yyy.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Test running failed: Instrumentation run failed due to 'Process crashed.'
Empty test suite.

我不知道为什么它不起作用。

这是我的 gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

apply plugin: 'org.greenrobot.greendao' // das kann dann später weg
//apply plugin: 'kotlin-kapt' // if using Kotlin
//apply plugin: 'io.objectbox'


android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.xxx.yyy"
        minSdkVersion 21
        targetSdkVersion 28    
        versionCode 130
        versionName "1.3.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

greendao {
    schemaVersion 4
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
    implementation 'org.greenrobot:greendao:3.2.2'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    implementation 'com.google.firebase:firebase-ads:15.0.1'
    testImplementation 'junit:junit:4.12'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.2'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
}

apply plugin: 'com.google.gms.google-services'

有什么建议吗?

【问题讨论】:

    标签: android testing automated-tests


    【解决方案1】:

    自己找到了解决方案。 我更新到 AndroidX,因此我还需要从以下位置更新我的 build.gradle 文件:

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    

    【讨论】:

    • 对我来说是另一种方式。我没有使用 AndroidX,但将运行器设置为 androidx.test。"
    • 我的 Android Studio 自动更新为 androidx.test.runner.AndroidJUnitRunner,但还是有这个问题
    • 我正在使用自定义 JUnitRunner 并面临这个问题。我已经验证包名是正确的。我不知道还能做什么
    • 这里与@AdamMc331 相同。我有一个自定义的JUnitRunner,有时测试通过,在某些情况下,它会失败。有时它在测试通过后崩溃,有时一起崩溃。这真的很令人沮丧。
    • @Tys 我在这个项目中成功使用了自定义 JUnitRunner:github.com/adammc331/pokedex 我不记得一直回到我遇到的问题,但也许通过比较代码你会看到一些东西突出。
    【解决方案2】:

    我得到了错误:

    Test running failed: Instrumentation run failed due to 'Process crashed.'
    

    在我的例子中,android测试控制台只显示了上面的错误,没有任何细节。

    但是在 logcat 中,显示了完整的错误。就我而言,我忘记在 AndroidManifest.xml 中添加 AdMob app_id

    所以请务必查看 logcat 以了解更多错误详情!

    【讨论】:

    • 当它说检查 logcat 然后 logcat
    • 这让我在花了几个小时研究这个问题后节省了时间。 Logcat 的胜利
    • 问题是我的 logcat 有 100 个记录的内容,我无法找到正确的日志位置
    • 一年多过去了,这为我节省了几天的时间,我正在使用 androidx.startup 并且出于某种奇怪的原因它需要成为我的 androidTestImplementation 的一部分,否则我的仪器测试会失败.test 类路径中缺少它 :) 谢谢!
    【解决方案3】:
    androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0"
    androidTestImplementation "androidx.test:runner:1.3.0"
    androidTestImplementation "androidx.test:core:1.3.0"
    androidTestImplementation "androidx.test.ext:junit:1.1.2"
    androidTestImplementation "androidx.test:rules:1.3.0"
    

    除了 androidx.test.runner.AndroidJunitRunner 配置,还请检查依赖关系。上面的代码对我有用。

    【讨论】:

    • androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_version" 特别是对我造成错误的人:)
    【解决方案4】:

    从以下位置更新 JUnit4 后我遇到了同样的错误:

    androidTestImplementation 'junit:junit:4.12
    

    androidTestImplementation 'junit:junit:4.13
    

    当我降级回:

    androidTestImplementation 'junit:junit:4.12
    

    【讨论】:

      【解决方案5】:

      所以我有相同的症状,但在进行所有这些更改后,我发现我的项目 build.gradle 文件中的以下配置是一个问题

      buildTypes{
           debug {
              debuggable true
              minifyEnabled true
              shrinkResources true
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      

      似乎升级到 androidx 后 minifyEnable 和 shrinkResources 的行为有所不同,可能更早发生,但我现在才意识到。注释掉这些行修复了我的未发现测试问题。

      buildTypes{
           debug {
              debuggable true
              //minifyEnabled true
              //shrinkResources true
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      

      【讨论】:

        【解决方案6】:

        出现此错误是因为我在 build.gradle 中将 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 更改为 testInstrumentationRunner "androidx.test.ext.junit.runners.AndroidJUnit4"。在 LogCat 中我看到了:

        java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo{com.example.debug.test/androidx.test.ext.junit.runners.AndroidJUnit4}: java.lang.ClassNotFoundException: Didn't find class "androidx.test.ext.junit.runners.AndroidJUnit4" on path: DexPathList[[zip file ...
        

        【讨论】:

          【解决方案7】:

          通常您希望整个套件一次匹配:https://developer.android.com/jetpack/androidx/releases/test

          例如,撰写本文时最新的是

          Core 1.4.0
          Espresso 3.4.0
          Intents 3.4.0
          JUnit 1.1.3
          Monitor 1.4.0
          Orchestrator 1.4.0
          Runner 1.4.0
          Rules 1.4.0
          Truth 1.4.0
          Test Services 1.4.0
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-03-03
            • 1970-01-01
            相关资源
            最近更新 更多