4

androidx.test在我的项目中使用库(我最近迁移到)并使用 custom AndroidJUnitRunner。在迁移之前一切正常,但现在我收到了这个错误 -

Started running tests Test running failed: Instrumentation run failed due to 'Process crashed.' Empty test suite.

我使用的自定义运行器类扩展自androidx.test.runner.AndroidJUnitRunner

在我的应用程序build.gradle文件中,我有以下设置 -

testInstrumentationRunner "com.example.CustomTestRunner"

有依赖关系 -

androidTestImplementation "androidx.test.ext:junit:1.1.0" androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test:core:1.1.0' androidTestImplementation "androidx.test:rules:1.1.1"

我所有的测试课程都有@RunWith(androidx.test.ext.junit.runners.AndroidJUnit4.class)

我被困在这一点上。任何帮助,将不胜感激。谢谢。

4

3 回答 3

1

我在使用 Android 4.4 进行测试时看到了这一点,当我切换到 Android 6 (SDK 23) 时,问题就消失了。

我将 androidx.test.ext.junit.runners.AndroidJUnit4 用于我的仪器测试@RunWith(AndroidJunit4.class)

但是我的 testInstrumentationRunner 使用了这个包:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

混合这两个不同的包看起来很奇怪,但它确实有效。

我的 app/build.gradle 有:

android {
    defaultConfig {
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    testOptions { 
        execution 'ANDROIDX_TEST_ORCHESTRATOR'
        unitTests {
            includeAndroidResources = true
        }
    }

    useLibrary 'android.test.runner'
    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'
}

dependencies {
    //--------------------------------------------------------------------------------
    // Test Dependencies

    // Required -- JUnit 4 framework for standard unit tests.
    testImplementation "junit:junit:$rootProject.ext.junitVersion"

    androidTestImplementation "junit:junit:$rootProject.ext.junitVersion"
    androidTestImplementation "org.hamcrest:hamcrest-library:$rootProject.ext.hamcrestVersion"

    // Mockito framework for NMEA-parser unit tests.
    testImplementation "org.mockito:mockito-core:$rootProject.ext.mockitoVersion"

    // Room testing
    androidTestImplementation "androidx.room:room-testing:$rootProject.ext.roomVersion"

    // Core library
    androidTestImplementation "androidx.test:core:$rootProject.ext.testCoreVersion"
    androidTestImplementation "androidx.arch.core:core-testing:$rootProject.ext.coreVersion"

    // AndroidJUnitRunner and JUnit Rules
    // deprecated
    androidTestImplementation "androidx.test:runner:$rootProject.ext.runnerVersion"
    androidTestImplementation "androidx.test:rules:$rootProject.ext.rulesVersion"

    // Assertions
    androidTestImplementation "androidx.test.ext:junit:$rootProject.ext.junitRunnerVersion"
    androidTestUtil "androidx.test:orchestrator:$rootProject.ext.orchestratorVersion"

//    androidTestImplementation "androidx.test.ext:truth:$rootProject.ext.xTruthVersion"
//    androidTestImplementation "com.google.truth:truth:$rootProject.ext.truthVersion"

    // Espresso dependencies
//    androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.ext.espressoVersion"
//    androidTestImplementation "androidx.test.espresso:espresso-contrib:$rootProject.ext.espressoVersion"
//    androidTestImplementation "androidx.test.espresso:espresso-intents:$rootProject.ext.espressoVersion"
//    androidTestImplementation "androidx.test.espresso:espresso-accessibility:$rootProject.ext.espressoVersion"
//    androidTestImplementation "androidx.test.espresso:espresso-web:$rootProject.ext.espressoVersion"
//    androidTestImplementation "androidx.test.espresso.idling:idling-concurrent:$rootProject.ext.espressoVersion"
//    androidTestImplementation "androidx.test.espresso:espresso-idling-resource:$rootProject.ext.espressoVersion"
}

configurations {
    all {
        resolutionStrategy {
            force "androidx.recyclerview:recyclerview:$rootProject.ext.recyclerviewVersion"
            force "org.checkerframework:checker-qual:$rootProject.ext.checkerQualVersion"
            force "org.checkerframework:checker-compat-qual:$rootProject.ext.checkerQualVersion"
            force "com.google.errorprone:error_prone_annotations:$rootProject.ext.errorProneAnnotationsVersion"
        }
    }
}

and I have these library versions:

        // Core Test library
        testCoreVersion = '1.1.0'
        coreVersion = '2.0.0-alpha1'

        // Automated Test Libraries
        // Instrumentation Test Runner
        junitRunnerVersion = '1.1.0'
        runnerVersion = '1.1.1'
        rulesVersion = '1.1.1'
        xTruthVersion = '1.0.0'
        truthVersion = '0.42'
        espressoVersion = '3.1.0'
        hamcrestVersion = '1.3'
        orchestratorVersion = '1.1.0'

        // JUnit library version
        junitVersion = '4.12'

        // Mockito version
        mockitoVersion = '2.21.0'

        // Force testing dependencies
        recyclerviewVersion = '1.0.0'
        checkerQualVersion = '2.5.3'
        errorProneAnnotationsVersion = '2.3.1'
于 2019-02-22T01:35:58.827 回答
0

在使用自定义 Runner 类来使用自定义 Application 类时,我遇到了同样的问题。

什么问题:

我的问题是我传递了唯一的类名,而不是包+名称。

错误的:

package com.teebalhoor.oredoh

import android.app.Application
import android.content.Context
import androidx.test.runner.AndroidJUnitRunner

/**
 * Created by Muhammad Maqsood on 13/09/2020.
 */
class CustomTestRunner : AndroidJUnitRunner() {

override fun newApplication(
    cl: ClassLoader?,
    className: String?,
    context: Context?
): Application {

    return super.newApplication(cl, OredohTestApplication::class.java.simpleName, context)
}
}

正确的路:

package com.teebalhoor.oredoh

import android.app.Application
import android.content.Context
import androidx.test.runner.AndroidJUnitRunner

/**
 * Created by Muhammad Maqsood on 13/09/2020.
 */
class CustomTestRunner : AndroidJUnitRunner() {

override fun newApplication(
    cl: ClassLoader?,
    className: String?,
    context: Context?
): Application {

    return super.newApplication(cl, OredohTestApplication::class.java.canonicalName, context)
}
}

我更换时工作正常(仅提供名称)

OredohTestApplication::class.java.simpleName

(给包+名称)

OredohTestApplication::class.java.canonicalName 
于 2020-09-13T11:04:58.963 回答
0

可能的原因之一是旧的Test Orchestrator (orchestrator-1.1.1.apk)Test Services (test-services-1.1.1.apk)应用程序是为 android 支持库组件构建的,并且仍然安装在目标设备上。打开设置->所有应用,搜索并删除。当您再次从 Android Studio 运行测试时,将为 AndroidX 构建的新应用程序将被安装,您的问题可能会消失。

于 2019-02-28T12:59:09.137 回答