android - 我可以在自己的模块中设置 Robolectric

标签 android gradle robolectric

是否可以在自己的模块中设置 robolectric 并将其作为依赖项添加到我的项目中?我可以将它添加到我的项目模块中,但如果它在它自己的模块中,我更喜欢它。我创建了一个 javaLibrary 模块并将以下代码添加到 gradle 构建脚本

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
dependencies {
    //Prior to AS 0.5.9: classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.+'
    classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.0'

    //previous plugin >> classpath 'com.novoda.gradle:robolectric-plugin:0.0.1-SNAPSHOT'
    }
}

allprojects {
repositories {
    mavenCentral()
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
}

apply plugin: 'android'
apply plugin: 'android-test' //previously >> apply plugin: 'robolectric'

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        applicationId "com.irishtimes.newsapp.tests"
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
}
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

sourceSets {
    androidTest.setRoot('src/tests') //note that this is androidTest instead of instrumentTest
    }
}


dependencies {
    androidTestCompile 'junit:junit:4.10'
    //include the stable robolectric 2.3 library
    androidTestCompile 'org.robolectric:robolectric:2.3'
    androidTestCompile 'com.squareup:fest-android:1.0.+'
}

//only include files that are suffixed with Test.class and set the max heap size
androidTest {
    include '**/*Test.class'
    maxHeapSize = "2048m"
}

我还在 src/tests/java 中添加了一个空白 list 文件和一个虚拟测试。在我之前运行 ./gradlew 测试时的设置中,我的测试将运行并且 id 得到反馈。现在,当我运行相同的命令时,我的构建脚本会运行,但它根本不接受我的测试。如果有人对教程或建议或快速解决方案有任何引用,那就太好了。谢谢!

最佳答案

是的,它可以使用不同的插件。 robolectric 支持的插件不支持这个。

插件可以在这里找到 https://github.com/novoda/gradle-android-test-plugin
可以在此处找到一个现成的示例 https://github.com/nenick/android-gradle-template

关于android - 我可以在自己的模块中设置 Robolectric,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24428027/

相关文章:

android - Android 中的可关闭横幅

java - Robolectric 测试中 Android Activity 中的模拟字段

android - 无法运行 Robolectric 测试

android - 无法从布局中获取元素

java - 我如何从适配器获取数据并在 Android 的 Activity 中显示

Android:关于位图、内存使用和缩放的问题

cordova - Task.leftShift(Closure) 方法已被弃用

text - 在 Gradle 中正确显示 UTF-8 文本

java - 创建第一个应用程序时 Flutter 出现错误 :checkDebugDuplicateClasses

android - Robolectric 自定义阴影书写