繁体   English   中英

使用 Spock 框架运行 Android 单元测试

[英]Running Android unit tests with Spock framework

我正在使用:

  • 安卓工作室 2.1.3
  • Gradle 2.14.1(我也尝试过 2.14)
  • OpenJDK 版本“1.8.0_91”

我想用GroovySpock为示例 Android 应用程序编写一些单元测试。

我已经读过关于RoboSpock 的内容

当我尝试运行简单测试时:

package a.b.regex

class TestSum extends spock.lang.Specification {

    def "test adding some numbers"() {
        when:
        def a = 5 + 4

        then:
        a == 9
    }
}

当我尝试在 Android Studio 中运行此测试时,出现错误:

Process finished with exit code 1
Class not found: "a.b.regex.TestSum"Empty test suite.

我使用的配置:

1)

buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
        classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
    }
}

apply plugin: 'groovyx.grooid.groovy-android'
// ...
dependencies {
    testCompile 'org.robospock:robospock:1.0.0'
}

2)

buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
        classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.0.0'
    }
}

apply plugin: 'groovyx.android'
dependencies {
    testCompile "org.codehaus.groovy:groovy-all:2.4.1"
    testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
    testCompile 'org.codehaus.groovy:groovy:2.4.6:grooid'
}

从控制台根本没有运行任何测试。 通过测试 Java 应用程序,我没有问题。

这是我想使用 Spock 的项目代码: GitHub 存储库

感谢Pieces我找到了答案。

您应该使用以下配置:

apply plugin: 'groovyx.android'

buildscript {
    repositories {
        jcenter() // or mavenCentral, etc.
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
        classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.0.0'
    }
}

testCompile 'org.codehaus.groovy:groovy:2.4.7:grooid'
testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
    exclude group: 'org.codehaus.groovy'
    exclude group: 'junit'
}

1) 除了您使用的是过时版本的 groovy android 插件之外,这应该很好用。 当前版本是 1.0.0。 您看到的错误是您将测试包含在 androidTest 源文件夹中,而它们应该包含在测试源文件夹中。

2) 您不想要 groovy-all,并且也希望将其从 spock 传递依赖项中排除。

这看起来类似于

dependencies {
    testCompile 'org.codehaus.groovy:groovy:2.4.7:grooid'
    testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
      exclude group: 'org.codehaus.groovy'
      exclude group: 'junit'
    }
  }

与 #1 的问题相同,您可能在 androidTest 文件夹而不是 test 文件夹下拥有源代码。

androidTest 文件夹用于将在设备上运行的测试,而 test 文件夹用于将在您的机器 JVM 上运行的测试。

如果您到达此处尝试配置 gradle 6.6,这将对您有所帮助:

我在尝试在具有 gradle 6.6 的 android 中配置 Spock 时多次介入,gradle 中发生了多次更改,因此他们弃用了此插件'groovyx.android'https : //github.com/groovy/groovy-android -gradle 插件

Deprecated: This plugin has been deprecated in favor of Kotlin which has the full support of JetBrains and Google. The changes that go into each Android Plugin Version make it really hard for this plugin to keep up. As of Gradle 6.0 this plugin does not work.

发现 gradle 有一个单独的插件来支持 groovy: https ://plugins.gradle.org/plugin/org.codehaus.groovy.android

这是使用 groovy DSL for gradle 的 gradle 6.6 配置

buildscript {
    ext.kotlin_version = "1.3.72"
    repositories {
        google()
        jcenter()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    dependencies {
        classpath "com.android.tools.build:gradle:4.0.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        classpath "gradle.plugin.org.codehaus.groovy:groovy-android-gradle-plugin:3.0.0"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

然后在应用程序配置中:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "org.codehaus.groovy.android"


def groovyVersion = "3.0.5"
def spockVersion = "2.0-M3-groovy-3.0"
dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.1'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
    testImplementation 'junit:junit:4.13'

    testImplementation("org.codehaus.groovy:groovy:${groovyVersion}")
    testImplementation("org.codehaus.groovy:groovy-all:${groovyVersion}")
    testImplementation("org.spockframework:spock-core:${spockVersion}")
    testImplementation("org.spockframework:spock-spring:${spockVersion}")


    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM