简体   繁体   English

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

[英]Running Android unit tests with Spock framework

I am using:我正在使用:

  • Android Studio 2.1.3安卓工作室 2.1.3
  • Gradle 2.14.1 (I tried with 2.14 also) Gradle 2.14.1(我也尝试过 2.14)
  • OpenJDK version "1.8.0_91" OpenJDK 版本“1.8.0_91”

I want to write some Unit tests with Groovy and Spock for sample Android application.我想用GroovySpock为示例 Android 应用程序编写一些单元测试。

I have already read about RoboSpock .我已经读过关于RoboSpock 的内容

When I am trying to run simple test:当我尝试运行简单测试时:

package a.b.regex

class TestSum extends spock.lang.Specification {

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

        then:
        a == 9
    }
}

When I try to run this test in Android Studio I have an error:当我尝试在 Android Studio 中运行此测试时,出现错误:

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

Configurations that I used:我使用的配置:

1) 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) 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'
}

From the console no tests are run at all.从控制台根本没有运行任何测试。 With testing Java applications I have no problem.通过测试 Java 应用程序,我没有问题。

Here is the project code where I want to use Spock: GitHub repository这是我想使用 Spock 的项目代码: GitHub 存储库

Thankfully to Pieces I found the answer.感谢Pieces我找到了答案。

You should use the following configuration:您应该使用以下配置:

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) This should work great other than you are using an outdated version of the groovy android plugin. 1) 除了您使用的是过时版本的 groovy android 插件之外,这应该很好用。 The current version is 1.0.0.当前版本是 1.0.0。 The error you are seeing is that you included your tests in androidTest source folder, when they should be included in the test source folder.您看到的错误是您将测试包含在 androidTest 源文件夹中,而它们应该包含在测试源文件夹中。

2) You do not want groovy-all, and want to exclude that from the spock transitive dependencies as well. 2) 您不想要 groovy-all,并且也希望将其从 spock 传递依赖项中排除。

This would look similar to这看起来类似于

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'
    }
  }

Same as the problem with #1 you probably have the source under androidTest folder instead of the test folder.与 #1 的问题相同,您可能在 androidTest 文件夹而不是 test 文件夹下拥有源代码。

The androidTest folder is for test that will run on the device, and the test folder if for tests that will run on your machines JVM. androidTest 文件夹用于将在设备上运行的测试,而 test 文件夹用于将在您的机器 JVM 上运行的测试。

If you reached here trying to configure for gradle 6.6 this will help you:如果您到达此处尝试配置 gradle 6.6,这将对您有所帮助:

I stepped multiple times into this while trying to configure Spock in android having gradle 6.6, there has been multiple changes in gradle, so they made this plugin deprecated 'groovyx.android' : https://github.com/groovy/groovy-android-gradle-plugin我在尝试在具有 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.

Found out that gradle have a separate plugin for groovy support: https://plugins.gradle.org/plugin/org.codehaus.groovy.android发现 gradle 有一个单独的插件来支持 groovy: https ://plugins.gradle.org/plugin/org.codehaus.groovy.android

This is the configuration for gradle 6.6 using groovy DSL for gradle这是使用 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
    }
}

And then in the app configuration:然后在应用程序配置中:

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