首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么会运行ExampleUnitTest却找不到ExampleInstrumentedTest呢?

什么会运行ExampleUnitTest却找不到ExampleInstrumentedTest呢?
EN

Stack Overflow用户
提问于 2020-03-13 18:50:23
回答 3查看 1.7K关注 0票数 3

Android : 3.6.1级: gradle-5.6.4-all

我已经准备好测试我的应用程序,并发现Android会生成示例单元和测试。所以,我右键单击ExampleUnitTest,选择Run,它就运行得很好。但是,当我为ExampleInstrumentedTest,执行此操作时,它会失败,出现以下错误消息:

未找到退出代码1类的过程:"com.example.jbiss.petminder.ExampleInstrumentedTest“

但是,ExampleInstrumentedTest位于默认的androidTest路径中,而ExampleUnitTest位于默认的测试路径(...\app\src\test\java\com\example\jbiss\petminder).中。

虽然我修改了自动生成的Android源代码,开始了测试编码的实验,但这与类nothing无关:"com.example.jbiss.petminder.ExampleInstrumentedTest“错误。下面是我修改的代码(使用自动生成的ExampleInstrumentedTest代码作为基线):

代码语言:javascript
复制
package com.example.jbiss.petminder;

import android.content.Context;

import com.example.jbiss.petminder.activities.MainActivity;

import androidx.test.espresso.accessibility.AccessibilityChecks;
import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static org.junit.Assert.*;

/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
@SmallTest
public class ExampleInstrumentedTest {

@Before
public void setUp(){
AccessibilityChecks.enable();
}

@Rule
public ActivityTestRule<MainActivity> mMainActivityActivityTestRule =
new ActivityTestRule<>(MainActivity.class);

@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

assertEquals("com.example.jbiss.petminder", appContext.getPackageName());
onView(withId(R.id.action_add_pet)).perform(click()).check(matches(withContentDescription(R.layout.activity_add_pet)));
}
}

这是我的应用程序的build.gradle文件:

代码语言:javascript
复制
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'
}
}
}
}

虽然我的代码中可能有其他问题,但是是什么原因导致找不到在该文件中显式声明了ExampleInstrumentedTest的现有public class ExampleInstrumentedTest{}文件呢?

代码似乎是按照构建仪器化单元测试中所示的格式格式化的。

以下未作答复:

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-03-13 23:41:02

androidx.testandroid.support.test/android.test之间是混合的,这是不建议的。将testInstrumentationRunner更改为以下内容可以解决这个问题:

代码语言:javascript
复制
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

这里有一个示例,您可以参考使用androidx.test作为主要测试框架。

票数 2
EN

Stack Overflow用户

发布于 2020-03-18 14:00:58

好的。从我的讨论中,我发现我可能有一些分级问题。因此,我对我的build.gradle文件做了以下更改:

发自:

代码语言:javascript
复制
// Assertions
androidTestImplementation 'androidx.test.ext:truth:1.2.0'
androidTestImplementation 'com.google.truth:truth:0.42'

至:

代码语言:javascript
复制
// 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"
}

现在,我得到了以下错误:

  • 哪里出了问题:评估项目':app‘时出现了一个问题。无法找到方法androidx.test.ext: type :1.1.1()用于org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.类型对象上的参数org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

因此,这似乎是developer.android.com文档中的一个问题,因为我按照它的页面的指示来开发我的测试代码,等级依赖告诉我使用以下内容:

代码语言:javascript
复制
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'

此代码将导致“未找到”错误。但是,仍然存在一个问题,因为没有任何命令任何人使用该{排除.}的东西,也没有什么版本的androidx.test.ext:truth可以用于获取正式文档中的功能代码。请注意,我使用1.1.1版本,因为Gradle在Android中告诉androidx.test.ext:junit有一个“新版本可用”。

注意:首先,我的仪器测试代码没有使用真理方法,所以为什么会发生这种情况是没有意义的。第二,我在搜索当前版本时发现了Truth.dev,并试图使用他们在“如何使用真相:分级”中说的话,但是它失败了,没有找到.错误。

票数 1
EN

Stack Overflow用户

发布于 2020-06-20 14:08:19

好的。我在这附近找到了一份工作。我创建了一个新的检测测试文件,并将原始问题文件的内容复制到其中,并更改了类名。它跑了。我删除了原始文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60675812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档