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

标签 android junit-runner androidjunitrunner androidx-test

我在我的项目中使用 androidx.test 库(我最近迁移到了该库)并使用自定义 AndroidJUnitRunner。 迁移之前一切正常,但现在出现此错误 -

开始运行测试 测试运行失败:由于“进程崩溃”,检测运行失败。 空测试套件。

我使用的自定义运行器类扩展自 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)

我卡在这了。任何帮助,将不胜感激。 谢谢。

最佳答案

我在使用 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'

关于android - 使用自定义运行器时未找到测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53950381/

相关文章:

android - 如何重构我的代码以使用 AsyncTask?

java - 生成的 Android espresso 测试运行失败,AndroidJUnitRunner 解析失败 : Landroidx/test/platform/io/FileTestStorage;

android - 无法找到 : ComponentInfo{. 的检测信息 ./android.support.test.runner.AndroidJUnitRunner}

android - 我在 sdk 管理器中找不到 "Documentation for Android SDK"

android - 如何从 Place API 对象中检索地址组件?

java - Spring boot模型映射器不满足依赖异常

java - 自定义 Junit 运行程序,运行之前和之后

android - 从 Android 测试 : InstrumentationRegistry. getInstrumentation().getTargetContext() VS myActivityTestRule.getActivity() 获取资源

android - 是否可以在样式中设置约束布局属性?

java - 在定义一些测试规则时从 Suite Runner 与 BlockJUnit4Runner 进行扩展