Android 什么将运行ExampleUnitTest但不查找ExampleInstrumentedTest?
安卓工作室:3.6.1 Gradle:Gradle-5.6.4-all 我准备测试我的应用程序,发现Android Studio生成了示例单元和测试。因此,我右键单击ExampleUnitTest,选择Run,它就可以正常运行了。但是,当我为示例InstrumentedTest执行此操作时,它会失败,并显示以下错误消息: 进程已完成,退出代码为1 未找到类:“com.example.jbiss.petminder.ExampleInstrumentedTest” 但是,ExampleInstrumentedTest位于默认的androidTest路径(..\app\src\androidTest\java\com\example\jbiss\petminder)和ExampleUnitTest位于默认的路径(..\app\src\test\java\com\example\jbiss\petminder) 虽然我修改了自动生成的AndroidStudio源代码以开始试验测试编码,但这与未找到的类无关:“com.example.jbiss.petminder.ExampleInstrumentedTest”错误。这是我修改过的代码(使用自动生成的示例InstrumentedTest代码作为基线): 虽然我的代码中可能存在其他问题,但是什么原因导致找不到在该文件中显式声明了Android 什么将运行ExampleUnitTest但不查找ExampleInstrumentedTest?,android,unit-testing,android-studio,junit4,instrumented-test,Android,Unit Testing,Android Studio,Junit4,Instrumented Test,安卓工作室:3.6.1 Gradle:Gradle-5.6.4-all 我准备测试我的应用程序,发现Android Studio生成了示例单元和测试。因此,我右键单击ExampleUnitTest,选择Run,它就可以正常运行了。但是,当我为示例InstrumentedTest执行此操作时,它会失败,并显示以下错误消息: 进程已完成,退出代码为1 未找到类:“com.example.jbiss.petminder.ExampleInstrumentedTest” 但是,ExampleInstru
公共类ExampleInstrumentedTest{}
的现有ExampleInstrumentedTest文件
代码的格式如中所示
以下没有提供答复:
- 在
androidx.test
和android.support.test
/android.test
之间存在混合,这是不建议的。将testInstrumentationRunner更改为以下选项可以解决此问题:
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
这里有一个可以参考的测试框架,用于使用
androidx.test
作为主要测试框架。好的。从阿哈斯比尼的讨论中,我发现我可能有一些格拉德尔问题。因此,我对build.gradle文件做了以下更改:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
}
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.example.jbiss.petminder"
minSdkVersion 24
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
encoding "UTF-8"
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dataBinding {
enabled = true
}
// Gradle automatically adds 'android.test.runner' as a dependency.
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
testOptions {
unitTests.includeAndroidResources = true
}
}
dependencies {
def nav_version = "2.3.0-alpha03"
implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0"
implementation "android.arch.navigation:navigation-ui-ktx:1.0.0"
// Java language implementation: navigation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
// Dynamic Feature Module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
// use -ktx for Kotlin
implementation "android.arch.navigation:navigation-ui:$nav_version"
// use -ktx for Kotlin
//noinspection GradleCompatible
implementation 'com.android.support:support-v4:28.1.0'
implementation
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.android.support:design:28.1.0'
implementation 'com.android.support:cardview-v7:28.1.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.android.support:mediarouter-v7:28.1.0'
/**
* directly from https://developer.android.com/topic/libraries/architecture/adding-components#lifecycle
*/
def lifecycle_version = "2.2.0"
def arch_version = "2.1.0"
def room_version = "2.2.4"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// Lifecycles only (without ViewModel or LiveData)
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
// Saved state module for ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
// Annotation processor
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
// optional - helpers for implementing LifecycleOwner in a Service
implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
// optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
// optional - ReactiveStreams support for LiveData
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"
// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$arch_version"
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"
// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$arch_version"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// use kapt for Kotlin
// optional - RxJava support for Room
//implementation "androidx.room:room-rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
//implementation "androidx.room:room-guava:$room_version"
// Test helpers
testImplementation "androidx.room:room-testing:$room_version"
testImplementation 'org.testng:testng:6.9.10'
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Robolectric environment
testImplementation 'androidx.test:core:1.2.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:2.19.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.api-client:google-api-client:1.30.2'
// Core library
androidTestImplementation 'androidx.test:core:1.2.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.ext:truth:1.2.0'
androidTestImplementation 'com.google.truth:truth:0.42'
// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.2.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.2.0'
// The following Espresso dependency can be either "implementation"
// or "androidTestImplementation", depending on whether you want the
// dependency to appear on your APK's compile classpath or the test APK
// classpath.
androidTestImplementation 'androidx.test.espresso:espresso-idling-resource:3.2.0'
// debugImplementation because LeakCanary should only run in debug builds.
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'
// Optional -- Hamcrest library
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with UI Automator
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
//if (requested.group == "androidx") {
if (!requested.name.startsWith("multidex")) {
details.useVersion '25.3.1'
}
}
}
}
发件人:
致:
现在我得到了以下错误:
- 出了什么问题: 评估项目“:app”时出现问题。 找不到参数[build_ezdi8oaa9gsnmfo7o2e18xyfx$\u run\u closure2]的方法androidx.test.ext:truth:1.1.1()$_closure11@6b8f394a]在类型为org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler的对象上
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'
此代码导致“未找到”错误。但是,仍然存在一个问题,因为没有告诉任何人使用{exclude…}东西,也没有告诉任何人使用哪个版本的androidx.test.ext:truth来获取官方文档中的函数代码。请注意,我使用的是1.1.1版本,因为Android Studio中的Gradle告诉我,androidx.test.ext:junit有一个“更新版本可用”
注意:首先,我的插装测试代码没有使用真值方法,所以发生这种情况的原因毫无意义。其次,我在搜索当前版本时找到了Truth.dev,并尝试使用他们在“如何使用Truth:Gradle”中所说的内容,但由于出现“未找到…”错误而失败。确定。我找到了解决这个问题的办法。我创建了一个新的插入指令的测试文件,将原始问题文件的内容复制到其中,并更改了类名。它跑了。我删除了原始文件。你能发布你的
build.gradle
文件吗?添加了build.gradleu很幸运地做了那个更改没有用,我收到了同样的错误消息。我还检查了“添加Gradle依赖项”的第3步,我们被告知对useLibrary使用“android.test.”。今天晚些时候我会检查一下样品。谢谢。我刚刚创建了一个新项目,它自动生成的默认ExampleInstrumentedTest成功运行。将此代码复制到失败的文件中不起作用。不要将代码从项目复制到新的“干净”项目中,只需按原样在新项目中运行测试,测试至少应该成功。从这里,您可以找到新项目和实际项目中的build.gradle
文件和ExampleInstrumentedTest
之间的差异,以使它们以某种方式匹配以进行工作测试。@ahabini,我复制并粘贴了一个实验,只是想看看失败的仪器测试中是否隐藏着一些奇怪的东西。当我比较两个build.gradle文件并将“def nav_version=“2.3.0-alpha03”更改为“def nav_version=“2.2.1”(与“测试”项目中一样)时,我得到了一个无法确定任务依赖关系的答案:app:compiledBugrenderScript。错误。新的“测试”项目的build.gradle较短,但我进行了比较。我刚刚编写了一个全新的插入指令的测试(基于),结果是“无法确定任务的依赖项:app:processDebugAndroidTestManifest”。错误等等。我找不到关于这些的任何信息。我的应用程序似乎有点问题,我无法修复。
// Assertions
androidTestImplementation 'androidx.test.ext:truth:1.2.0'
androidTestImplementation 'com.google.truth:truth:0.42'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.ext:truth:1.1.1'{
exclude group: "com.google.truth", module: "truth"
}
androidTestImplementation 'com.google.truth:truth:0.44'{
exclude group: "org.checkerframework", module: "checker-compat-qual"
exclude group: "com.google.errorprone", module: "error_prone_annotations"
}
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'