使用自定义运行器时未找到测试

时间:2018-12-27 20:14:21

标签: android junit-runner androidjunitrunner androidx-test

我在项目中使用androidx.test库(我最近迁移到了这些库),并使用了自定义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)

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

3 个答案:

答案 0 :(得分: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'

答案 1 :(得分:0)

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

答案 2 :(得分: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