gpt4 book ai didi

AndroidX : No instrumentation registered! 必须在注册工具下运行

转载 作者:IT老高 更新时间:2023-10-28 23:00:52 62 4
gpt4 key购买 nike

我正在尝试运行取决于上下文的本地单元测试,并遵循本指南:https://developer.android.com/training/testing/unit-testing/local-unit-tests#kotlin我这样设置我的项目(点击此链接:https://developer.android.com/training/testing/set-up-project):

build.gradle(app)

android {
compileSdkVersion 28
buildToolsVersion '27.0.3'
defaultConfig {
minSdkVersion 21
targetSdkVersion 27
versionCode 76
versionName "2.6.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true

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

}
testOptions {
unitTests.returnDefaultValues = true
unitTests.all {
// All the usual Gradle options.
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
outputs.upToDateWhen { false }
showStandardStreams = true
}
}
unitTests.includeAndroidResources = true

}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')

androidTestImplementation("androidx.test.espresso:espresso-core:$espressoVersion", {
exclude group: 'com.android.support', module: 'support-annotations'
})
// Espresso UI Testing dependencies
implementation "androidx.test.espresso:espresso-idling-resource:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"

testImplementation 'androidx.test:core:1.0.0'

// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
// Espresso Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'
implementation 'androidx.multidex:multidex:2.0.0'
}

我的 espresso_version 是 espressoVersion = '3.1.0'

我的测试位于 module-name/src/test/java/ 如下所示:

    import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.instacart.library.truetime.TrueTime
import edu.mira.aula.shared.extensions.android.trueDateNow
import edu.mira.aula.shared.network.ConnectivityHelper
import kotlinx.coroutines.experimental.runBlocking
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import java.util.*
import java.util.concurrent.CountDownLatch

class TimeExtensionsUnitTest {
private lateinit var instrumentationCtx: Context

@Before
fun setup() {
instrumentationCtx = ApplicationProvider.getApplicationContext<Context>()
}
@Test
fun testTrueTimeValueReturnsIfInitialized() {
if (ConnectivityHelper.isOnline(instrumentationCtx)) {
runBlocking {
val countDownLatch = CountDownLatch(1)
TrueTime.build()
.withSharedPreferencesCache(instrumentationCtx)
.withConnectionTimeout(10000)
.initialize()
countDownLatch.countDown()

try {
countDownLatch.await()
val dateFromTrueTime = trueDateNow()
val normalDate = Date()
Assert.assertNotEquals(dateFromTrueTime, normalDate)
} catch (e: InterruptedException) {
}
}
}
}

每次我运行它,它都会给我:

java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
at androidx.test.platform.app.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)
at androidx.test.core.app.ApplicationProvider.getApplicationContext(ApplicationProvider.java:41)

如果我将它作为仪器测试运行(更改包),它运行时不会出错。但我认为本指南正是为了能够使用 Context 等 Android 框架类运行单元测试。我什至尝试运行该 class UnitTestSample 但发生同样的错误。

我还从我的项目中删除了所有 android.support 依赖项

关于如何解决它的任何想法?

最佳答案

更新

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


我也遇到过这个问题。

如果您考虑迁移到 Robolectric 4.0 here ,建议在您的 gradle.properties 中添加以下行。

android.enableUnitTestBinaryResources=true

问题是,如果你把这个添加到你的gradle.properties,它会输出这个警告:

WARNING: The option setting 'android.enableUnitTestBinaryResources=true' is experimental and unsupported.

现在,如果您查看 Robolectric 版本 here .您可以看到这是一个已知问题,他们声明

Android Gradle Plugin may report the following warning, which may be safely ignored: WARNING: The option setting 'android.enableUnitTestBinaryResources=true' is experimental and unsupported.. Android Gradle Plugin 3.4 will resolve this issue.

我相信除非您可以将 gradle 更新到 3.4。您将无法解决此问题。

我所做的是将 Robolectric 4.0 作为依赖项包含在内。

testImplementation "org.robolectric:robolectric:4.0.2"

并用

注释我的测试类
@RunWith(RobolectricTestRunner::class)

这应该使您的测试工作。

现在,当您运行测试时,您会注意到 Robolectric 将记录以下内容:

[Robolectric] NOTICE: legacy resources mode is deprecated; see http://robolectric.org/migrating/#migrating-to-40

暂时忽略这一点,但一旦您可以更新您的 gradle,请迁移到新的 Robolectric 测试。

关于AndroidX : No instrumentation registered! 必须在注册工具下运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53595837/

62 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com