最近有一个短暂的时刻,Android Studio 中的“构建变体” View 包含“单元测试”选项,如果选中此选项,您只需选择测试即可在 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}"
}
}
}
最佳答案
您有androidTest
源集被覆盖以查看 src/test/
。因此,Gradle 认为您的 test/
代码代表仪器测试。
Should I just remove the whole sourceSets block?
理想情况下,您遵循库存配置:androidTest/
进行仪器测试,test/
保存单元测试。
如果你想要别的东西,那也没关系。但是,当您右键单击仪器测试类并选择“运行”时,它将运行仪器测试。所以,如果你想要androidTest/
进行单元测试,以及 instrumentationTestBecauseLongDirectoryNamesAreFun/
进行仪器测试,这很好,但您需要跟踪自己所在的位置。
那么,你是否去掉 sourceSets
关闭,或更新它以反射(reflect)您希望仪器测试和单元测试进行的位置,由您决定。
关于android - 如何在 Android Studio 2.1.3 中运行简单的 junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39003240/