Rom*_*man 8 android gradle robolectric android-studio
我一直试图这样做几天,没有结果.我需要在Android Studio(0.8.9,最新版本)中设置Robolectric.我遵循不同的教程Android单元和集成测试,Roboelectric安装单元测试,Android Gradle应用程序与Roboelectric,如何运行Roboelectric JUnit测试,但总是有一些错误.
所以我专门为测试创建了模块:


Kedzoh(Project)build.gradle:
// Top-level build file where you can add configuration options common to all sub- projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
classpath 'org.robolectric:robolectric-gradle-plugin:0.12.+'
}
}
allprojects {
repositories {
jcenter()
}
}
Run Code Online (Sandbox Code Playgroud)
app build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
applicationId 'com.dev.kedzoh'
minSdkVersion 9
targetSdkVersion 19
versionCode 14
versionName '1.6.7'
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
compile 'com.google.android.gms:play-services:4.2.42'
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.picasso:picasso:2.3.3'
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.android.support:support-v4:19.+'
compile 'com.google.guava:guava:18.0-rc1'
compile 'com.sothree.slidinguppanel:library:+'
compile 'com.larswerkman:HoloColorPicker:1.4'
}
Run Code Online (Sandbox Code Playgroud)
kedzoh-tests build.gradle:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.picasso:picasso:2.3.3'
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.google.guava:guava:18.0-rc1'
compile 'com.sothree.slidinguppanel:library:+'
compile 'com.larswerkman:HoloColorPicker:1.4'
testCompile 'junit:junit:4.+'
testCompile 'org.robolectric:robolectric:2.2'
}
Run Code Online (Sandbox Code Playgroud)
此时我无法导入Robolectric类,它给了我错误.当我添加apply plugin:'robolectric'到kedzoh-tests build.gradle时,它会询问'android'插件.在我添加之后,它抱怨没有Manifest并且无法构建.我不确定这是正确的配置,因为它从未真正起作用.有人可以给出一些如何在Android Studio中设置Robolectric的建议吗?
编辑:
我尝试了下面的答案,但仍然坚持"未找到类"错误:



我认为通过在主“应用程序”模块之外创建测试模块,您可能会让您的生活变得更加困难和复杂。我见过的大多数教程的应用程序模块中都有一个 androidTest 文件夹,其中可以包含您的 Robolectric 测试。我见过的所有基于 Eclipse 的旧教程似乎都指导我们创建独立于主项目的测试模块。
如果我使用 Robolectric,我会像我为您创建的那样设置项目https://github.com/marctomas2013/Kedzoh。我将使用 androidTest 文件夹来放置我的测试并配置 gradle 文件,如下所示。
请注意,测试的依赖项是使用 androidTestCompile 声明包含的,而应用程序依赖项是使用编译来声明的。
此 gradle 文件中的另一项是 robolectric 部分,其中包含和排除 androidTest 文件夹中的类。espresso 文件夹中没有的任何内容都将使用 gradlew 测试目标运行,并且在调用 gradlewconnectedAndroidTest 目标时,包括 espresso 文件夹在内的所有内容都将运行。
app gradle file
apply plugin: 'com.android.application'
apply plugin: 'robolectric'
android {
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
}
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
minSdkVersion 18
targetSdkVersion 18
versionCode 2
versionName "1.0.0-SNAPSHOT"
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
release {
runProguard false
}
}
sourceSets {
androidTest {
setRoot('src/androidTest')
}
}
}
robolectric {
include '**/*Test.class'
exclude '**/espresso/**/*.class'
}
dependencies {
repositories {
mavenCentral()
}
compile 'com.sothree.slidinguppanel:library:2.0.1'
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
// Espresso
androidTestCompile files('libs/espresso-1.1.jar')
androidTestCompile files('libs/testrunner-1.1.jar')
androidTestCompile files('libs/testrunner-runtime-1.1.jar')
androidTestCompile 'com.google.guava:guava:14.0.1'
androidTestCompile 'com.squareup.dagger:dagger:1.1.0'
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
androidTestCompile('org.robolectric:robolectric:2.3') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
exclude module: 'httpclient'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-provider-api'
}
androidTestCompile 'com.squareup:fest-android:1.0.+'
}
Run Code Online (Sandbox Code Playgroud)
项目构建文件 这里我们包含 robolectric 类路径,以便我们可以在上面的配置文件中使用 robolectric gradle 命令。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2'
classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
Run Code Online (Sandbox Code Playgroud)
您能否解释一下您遇到的错误,因为根据您的工作方式可能会出现许多可能的错误,并且创建单独的测试模块是否有特殊原因。
我提供的项目基于 Deck-gradle 设置(https://github.com/robolectric/deckard-gradle),这里有许多关于 JUnit 冲突等的提示,您可能会遇到麻烦。
一旦您完成了这一步,以便能够在应用程序中调试单元测试,您将必须根据本文中的信息在启动测试时手动调整类路径(一点也不漂亮!) github.com/codepath/android_guides/wiki/Robolectric-Installation-for-Unit-Testing
您将需要运行单元测试,等待它失败,复制 -classpath 参数及其值(它会很长,以“开始和结束”)并将其复制到运行配置中的 VM 选项中,然后在该类路径的末尾添加一个 ; 或 a : ,具体取决于您的操作系统路径定界符,并将路径添加到您的测试类,如下所示,<ABSOLUTE_PATH_TO_PROJECT>/build/test-classes会将您的测试类添加到类路径中,然后 Android Studio 应该能够运行它们。
这还假设运行步骤gradle testClasses已从文章http://blog.blundell-apps.com/how-to-run-robolectric-junit-tests-in-android-studio/中完成
| 归档时间: |
|
| 查看次数: |
4994 次 |
| 最近记录: |