0
我不知道如何讓我的單元測試運行通過Gradle。我創建了一個單元測試任務,設定適當的源組,和適當的依賴關係,但我得到的錯誤:從Gradle Android的JUnit測試:「包android.test不存在」
package android.test does not exist
我也跟着在另一篇文章的方向,並增加了以下我的build.gradle:
// Unit tests
sourceSets {
unitTest {
java.srcDir file('test')
resources.srcDir file('test/res')
}
}
dependencies {
unitTestCompile files("$project.buildDir/classes/debug")
unitTestCompile 'junit:junit:4.11'
unitTestCompile 'com.google.android:android:4.0.1.2'
}
configurations {
unitTestCompile.extendsFrom runtime
unitTestRuntime.extendsFrom unitTestCompile
}
task unitTest(type:Test, dependsOn: assemble) {
description = "run unit tests"
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
}
低於滿的build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
configurations {
apt
}
apply plugin: 'android'
dependencies {
compile project(':PullToRefresh')
compile project(':simple-crop-image-lib')
compile project(':facebook')
compile project(':gridlayout')
compile project(':nineoldandroids')
compile project(':ViewPagerIndicator')
compile project(':volley')
compile fileTree(dir: 'libs', include: '*.jar')
apt files('compile-libs/androidannotations-2.7.1.jar')
}
android {
buildToolsVersion '17.0'
compileSdkVersion 17
signingConfigs {
debug {
storeFile file('keystores/debug.keystore')
}
}
buildTypes {
debug {}
ipconfigurable {
debuggable true
signingConfig signingConfigs.debug
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
// Unit tests
sourceSets {
unitTest {
java.srcDir file('src/test')
resources.srcDir file('src/test/res')
}
}
dependencies {
unitTestCompile files("$project.buildDir/classes/debug")
unitTestCompile 'junit:junit:4.11'
unitTestCompile 'com.google.android:android:4.0.1.2'
}
configurations {
unitTestCompile.extendsFrom runtime
unitTestRuntime.extendsFrom unitTestCompile
}
task unitTest(type:Test, dependsOn: assemble) {
description = "run unit tests"
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
}
目錄結構:
什麼是你的目錄結構爲你的測試?具體來說,您的目錄名稱是否與您的軟件包名稱匹配? –
@PerrynFowler感謝您的意見。我確信我的目錄名與包名匹配,並更新了sourceSets中的srcDir,但仍然出現相同的錯誤。包含在圖像中的目錄結構。 –
它在我看來像你需要將android-test添加到你的unitTestCompileDependencies –