为什么官方文档中的Android测试示例不起作用?
根据上的文档,我为我的应用程序模块创建了以下build.gradle文件:为什么官方文档中的Android测试示例不起作用?,android,unit-testing,automated-tests,Android,Unit Testing,Automated Tests,根据上的文档,我为我的应用程序模块创建了以下build.gradle文件: buildscript { repositories { mavenCentral() maven { url 'https://maven.fabric.io/public' } } dependencies { classpath 'io.fabric.tools:gradle:1.25.3' } } apply plugin: 'co
buildscript {
repositories {
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.25.3'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.myapp"
minSdkVersion 19
targetSdkVersion 19
versionCode 7
versionName "1.3.0"
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
}
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
dexOptions {
javaMaxHeapSize "4g"
}
}
dependencies {
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'com.android.support:support-annotations:27.1.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-accessibility:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-web:3.0.2'
androidTestImplementation 'com.android.support.test.espresso.idling:idling-concurrent:3.0.2'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':deviceprint-lib-2.0.1')
implementation files('libs/gson-2.2.4.jar')
implementation files('libs/activation.jar')
implementation files('libs/additionnal.jar')
implementation files('libs/mail.jar')
implementation 'com.android.support:support-v4:27.1.1'
}
以及以下项目级build.gradle:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
}
allprojects {
repositories {
jcenter()
google()
}
}
我在app/src/androidTest/java/com/mypackage创建了一个目录,并添加了以下文件:
package com.mypackage;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class ApplicationTest
{
public ApplicationTest()
{
}
@Test public void failingTest()
{
assertThat(false, is(true));
}
}
所有导入语句和@Test注释都标记为错误,并显示消息“无法解析符号”,如果我在项目窗口中右键单击该文件,则我没有“运行”选项。为什么?在我重新启动Android Studio后,该示例开始正常工作:facepalm: 编辑:
另一个注意事项是,我最近因为不同的原因经历了这些相同的症状:构建变量必须设置为“debug”(或者必须在模块的
build.gradle
文件的android{}
块中设置要测试的变量)您是否检查了项目结构以确保路径/包被视为测试树的一部分?您好@Rob,您能再详细说明一下吗?在“项目结构”对话框中,我没有看到任何对“测试树”的引用,很高兴它能工作