android - 构建本地单元测试(未注册仪器!必须在注册仪器下运行)

标签 android unit-testing documentation robolectric

<分区>

看看官方documentation . Include framework dependencies 部分提供了一个示例,说明如何设置本地单元测试以使用环境 android sdk。但是,如果您按照示例中的方式执行所有操作,则测试不会开始。我得到一个错误

java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.

所有尝试都是在一个新项目上进行的。 Android Studio 3.3、gradle-4.10.1、build:gradle:3.3.0、Kotlin,并包含 Androidx Artifact 。

然后在指定配置的项目中添加如下几行:

build.gradle

android {
    // ...
    testOptions {
        unitTests.includeAndroidResources = true
    }
}

dependencies {
    // ...
    // Already exist
    testImplementation 'junit:junit:4.12'
    // Added this line
    testImplementation 'androidx.test:core:1.0.0'
}

以及测试体本身:

package com.example.myapplication

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import org.junit.Test

class ExampleUnitTest {

    val context = ApplicationProvider.getApplicationContext<Context>()

    @Test
    fun readStringFromContext_LocalizedString() {
        System.out.println(context.applicationInfo.packageName)
    }
}

我做错了什么?


apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    testOptions {
        unitTests.includeAndroidResources = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.core:core-ktx:1.1.0-alpha03'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    testImplementation 'junit:junit:4.12'
    testImplementation 'androidx.test:core:1.0.0'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}

最佳答案

更新:

如果您使用的是最新的 gradle 版本,您应该不会再遇到此错误。


我想您需要在 build.gradle 中包含 Robolectric 依赖项,并为您的测试指定测试运行器:

@RunWith(RobolectricTestRunner.class)
class ExampleUnitTest {

在那之后它对我有用。我不知道为什么这个信息没有包含在 Android 文档中。

关于android - 构建本地单元测试(未注册仪器!必须在注册仪器下运行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54222533/

相关文章:

java - M3 中的classes()、fields() 和methods() 函数在哪里记录?

documentation - 有没有办法同步不同 Word 文档的部分?

java - 无法使用特定对象填充 LinearLayout

java - 在代码中创建 File 实例

AngularJS 单元测试错误 : scope is undefined

c# - 枚举类型 : variable 'x' of type 'xxx' referenced from scope '' , 的起订量子属性,但未定义

perl - 如何使用 Pod::Weaver 为方法定义多个小节?

android - 在 WebView 的情况下尝试隐藏和显示布局时屏幕闪烁

android - 无法加载文件或程序集 'UnityEngine.Purchasing'

java - 文件被删除而没有被复制