测试工件选择器实际上做了什么?出于某种原因,当我将其设置为单元测试时,Android Studio(AS)能够识别并运行我的Junit 4测试。当我将其设置为Android Instrumentation Tests时,JUnit 4从我的类路径中消失,并且AS将代码标记为不正确(无法解析导入org.junit.Test的符号junit)。
有没有办法告诉AS将选择的类文件视为我不想在模拟器中运行的JUnit测试?这根本就在他们的雷达上吗?我必须将这个开关从JUnit测试转移到集成测试,这似乎很疯狂,它似乎影响整个项目,而不仅仅是一个构建/发布风格,或者只是我项目的一个子目录。
这是我的build.gradle,使用gradle 2.2.1,Android Studio 1.3的canary频道(AI-141.1972460),Android SDK工具24.3.0
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
// Manifest version information!
def versionMajor = 0
def versionMinor = 1
def versionPatch = 0
def versionBuild = 1 // bump for dogfood builds, public betas, etc.apply plugin: 'android'
apply plugin: 'com.android.application'
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
def gitSha = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()
def buildTime = new Date().format("yyyy-MM-dd'T'HH:mm'Z'", TimeZone.getTimeZone("UTC"))
android {
dexOptions {
preDexLibraries = false
}
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
multiDexEnabled true
testInstrumentationRunner "android.support.multidex.MultiDexTestRunner"
versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
buildConfigField "String", "GIT_SHA", "\"${gitSha}\""
buildConfigField "String", "BUILD_TIME", "\"${buildTime}\""
}
productFlavors {
// Define separate dev and prod product flavors.
dev {
// dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
// to pre-dex each module and produce an APK that can be tested on
// Android Lollipop without time consuming dex merging processes.
minSdkVersion 21
}
prod {
// The actual minSdkVersion for the application.
minSdkVersion 14
}
}
buildTypes {
debug {
applicationIdSuffix '.dev'
versionNameSuffix '-dev'
}
release {
}
}
lintOptions {
abortOnError false
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
packagingOptions {
exclude 'META-INF/services/javax.annotation.processing.Processor'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'LICENSE.txt'
}
sourceSets {
instrumentTest.setRoot('src/test')
androidTest.setRoot('src/test')
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:support-v4:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.google.android.gms:play-services-base:7.5.0'
compile 'com.google.android.gms:play-services-plus:7.5.0'
compile 'com.google.android.gms:play-services-identity:7.5.0'
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:cardview-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:support-annotations:22.2.0'
compile 'com.squareup.dagger:dagger:1.2.2'
provided 'com.squareup.dagger:dagger-compiler:1.2.2'
compile 'com.squareup.okhttp:okhttp:2.3.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.jakewharton.timber:timber:3.0.1'
compile 'io.reactivex:rxjava:1.0.9'
compile 'io.reactivex:rxandroid:0.24.0'
compile 'io.reactivex:rxandroid-framework:0.24.0'
compile 'com.github.matthewyork:ColoursLibrary:1.0.0@aar'
compile 'com.nispok:snackbar:2.10.7'
compile 'com.github.frankiesardo:icepick:2.3.6'
provided 'com.github.frankiesardo:icepick-processor:2.3.6'
compile 'nl.qbusict:cupboard:2.1.1'
compile 'org.lucasr.twowayview:core:1.0.0-SNAPSHOT@aar'
compile 'org.lucasr.twowayview:layouts:1.0.0-SNAPSHOT@aar'
compile 'it.neokree:MaterialNavigationDrawer:1.3.3'
testCompile 'junit:junit:4.12'
}
答案 0 :(得分:4)
现在你可以删除行
sourceSets {
instrumentTest.setRoot('src/test')
androidTest.setRoot('src/test')
}
而是在src / test中将Instrumentation测试保存在/ src / androidTest和Junit测试中(不依赖于android api)
您必须单独为检测android测试和非android测试提供依赖项。 对于android测试,使用androidTestCompile和junit测试使用testCompile
dependencies {
...
testCompile 'junit:junit:${junitVersion}'
testCompile 'org.mockito:mockito-core:${mockitoVersion}'
androidTestCompile 'junit:junit:${junitVersion}'
androidTestCompile 'org.mockito:mockito-core:${mockitoVersion}'
androidTestCompile 'com.android.support.test.espresso:espresso-core:${espressoVersion}'
}
单独提供这些依赖项将删除您在问题中遇到的问题。 更多详情http://tools.android.com/tech-docs/unit-testing-support