"Test Artifact" 选择器在 Android STudio 中的作用
What does the "Test Artifact" selector do in Android STudio
测试工件选择器的实际作用是什么?出于某种原因,当我将其设置为单元测试时,Android Studio (AS) 能够识别并 运行 我的 Junit 4 测试。当我将其设置为 Android Instrumentation Tests 时,JUnit 4 从我的 class 路径中消失并且 AS 将代码标记为不正确(无法解析符号 junit 以导入 org.junit.Test)。
有什么方法可以告诉 AS 将选择的 class 文件视为我不想 运行 在模拟器内部进行的 JUnit 测试?这在他们的雷达上吗?我必须翻转这个开关以从 JUnit 测试转移到集成-y 测试,这似乎很疯狂,而且它似乎影响了整个项目,而不仅仅是一个 build/release 风格,或者只是我项目的一个子目录。
这是我的 build.gradle,使用 gradle 2.2.1,Android Studio 1.3 (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'
}
现在您可以删除行
sourceSets {
instrumentTest.setRoot('src/test')
androidTest.setRoot('src/test')
}
而是在 /src/androidTest 中保留 Instrumentation 测试,在 src/test
中保留 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
测试工件选择器的实际作用是什么?出于某种原因,当我将其设置为单元测试时,Android Studio (AS) 能够识别并 运行 我的 Junit 4 测试。当我将其设置为 Android Instrumentation Tests 时,JUnit 4 从我的 class 路径中消失并且 AS 将代码标记为不正确(无法解析符号 junit 以导入 org.junit.Test)。
有什么方法可以告诉 AS 将选择的 class 文件视为我不想 运行 在模拟器内部进行的 JUnit 测试?这在他们的雷达上吗?我必须翻转这个开关以从 JUnit 测试转移到集成-y 测试,这似乎很疯狂,而且它似乎影响了整个项目,而不仅仅是一个 build/release 风格,或者只是我项目的一个子目录。
这是我的 build.gradle,使用 gradle 2.2.1,Android Studio 1.3 (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'
}
现在您可以删除行
sourceSets {
instrumentTest.setRoot('src/test')
androidTest.setRoot('src/test')
}
而是在 /src/androidTest 中保留 Instrumentation 测试,在 src/test
中保留 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