【发布时间】:2016-08-18 02:10:20
【问题描述】:
最近有一个短暂的时刻,Android Studio 中的 Build Variants 视图包含一个“单元测试”选项,如果选中该选项,您只需选择测试即可在 Android Studio 中轻松运行基本单元测试上课并点击“跑步”。然后我认为当 Android Studio 2.0 发布时,他们从构建变体菜单中删除了该选项。所有文档都说您应该能够右键单击测试类并说“运行”,但是每当我这样做时,系统都会提示我选择一个用于仪器测试的部署目标,而我对此不感兴趣。我在这里错过了什么吗?您如何在 Android Studio 2.1.3 中运行基本单元测试?
这是我当前 build.gradle 的摘录,其中包含我之前运行单元测试所需的代码:
android {
compileSdkVersion project.COMPILE_SDK_VERSION.toInteger()
buildToolsVersion project.BUILD_TOOLS_VERSION
defaultConfig {
minSdkVersion project.SDK_MIN_VERSION_LIBRARY
targetSdkVersion project.COMPILE_SDK_VERSION.toInteger()
versionName project.VERSION_NAME
versionCode project.VERSION_CODE.toInteger()
}
lintOptions {
abortOnError false
}
buildTypes {
debug{
versionNameSuffix = "DEBUG"
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
androidTest {
setRoot('src/test')
java.srcDir file('src/test/java')
resources.srcDir file('src/test/resources')
}
}
testOptions {
unitTests.returnDefaultValues = true
}
android.testOptions.unitTests.all {
// Configure includes / excludes
include '**/*Test.class'
exclude '**/espresso/**/*.class'
// Configure max heap size of the test JVM
maxHeapSize = '2048m'
// Configure the test JVM arguments - Does not apply to Java 8
jvmArgs '-XX:MaxPermSize=4096m', '-XX:-UseSplitVerifier'
// Specify max number of processes (default is 1)
maxParallelForks = 4
// Specify max number of test classes to execute in a test process
// before restarting the process (default is unlimited)
forkEvery = 250
// configure whether failing tests should fail the build
ignoreFailures false
// use afterTest to listen to the test execution results
afterTest { descriptor, result ->
println "Executing test for ${descriptor.parent}: ${descriptor.name} with result: ${result.resultType}"
}
}
}
【问题讨论】:
-
"如何在 Android Studio 2.1.3 中运行基本单元测试?" -- 你按照你描述的做(右键单击类并选择运行),或者单击编辑器中类声明旁边的装订线图标。 “但是每当我这样做时,系统都会提示我选择一个用于仪器测试的部署目标,而我对此不感兴趣”——您的单元测试是否在模块中的
test/源集中?你有没有在build.gradle中做任何与源代码集有关的时髦的事情? -
啊……是的,我做到了!我有过去几年需要的代码。我用相关代码更新了我的问题。你发现问题了吗?
标签: android unit-testing