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

时间:2016-08-27 22:24:23

标签: java android gradle groovy spock

我正在使用:

  • Android Studio 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 repository

谢天谢地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'
}

2 个答案:

答案 0 :(得分:3)

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文件夹而不是测试文件夹下有源。

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

答案 1 :(得分:1)

如果您到达此处尝试配置gradle 6.6,这将为您提供帮助:

在尝试在具有gradle 6.6的android系统中配置Spock时,我多次尝试执行此操作,gradle进行了多次更改,因此他们不推荐使用此插件'groovyx.android'https://github.com/groovy/groovy-android-gradle-plugin

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进行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'

}