'./gradlew -Dtest.single = SimpleTest test'运行我所有的测试

Woo*_*Kim 8 testing android gradle robolectric android-studio

我的项目是在Android Studio上创建的普通Android项目.

项目结构

我google了很多次来进行单一测试(在SimpleTest中测试,在这种情况下测试1次),每个人都说我应该这样做

./gradlew -Dtest.single=SimpleTest test
Run Code Online (Sandbox Code Playgroud)

在我的根文件夹中.

或者我在我的内部项目文件夹中

../gradlew -Dtest.single=SimpleTest test
Run Code Online (Sandbox Code Playgroud)

我尝试了很多像这样的方法,但它总是运行我所有的测试.(所有类中的11个测试)

我的build.gradle上有问题,还是有遗漏的东西?

这是我的build.gradle文件.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'

        classpath 'com.github.jcandksolutions.gradle:android-unit-test:1.0.+'
    }
}

apply plugin: 'android'

    repositories {
        mavenCentral()
    }

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 18
        // I changed this for this question
        packageName "com.XXX.XXXX"
    }

    sourceSets {
        instrumentTest.setRoot('src/test')
    }
}

apply plugin: 'android-unit-test'

dependencies {
    repositories {
        mavenCentral()
    }
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.android.support:appcompat-v7:+'

    compile 'com.google.code.gson:gson:2.2.4'
    compile group:'com.squareup.picasso', name:'picasso', version:'2.1.1'
    compile group:'com.squareup.okhttp', name:'okhttp', version:'1.2.1'
    compile group:'com.squareup', name:'otto', version:'1.3.4'

    compile group:'com.jakewharton', name:'butterknife', version:'3.0.0'

    compile group:'com.github.kevinsawicki', name:'http-request', version:'5.4.1'

    compile fileTree(dir: 'libs', include: '*.jar')

    testCompile 'junit:junit:4.10'
    testCompile 'org.robolectric:robolectric:2.2'
    testCompile 'com.squareup:fest-android:1.0.+'

    instrumentTestCompile 'junit:junit:4.10'
    instrumentTestCompile 'org.robolectric:robolectric:2.2'
    instrumentTestCompile 'com.squareup:fest-android:1.0.+'
}

tasks.withType(Compile) {
    options.encoding = "UTF-8"
}

tasks.withType(Test) {
    testLogging {
        events 'started', 'passed'
    }
}
Run Code Online (Sandbox Code Playgroud)

Ren*_*hke 8

符号

-Dtest.single=SimpleTest
Run Code Online (Sandbox Code Playgroud)

意味着在名为'test'的任务中只运行SimpleTest.如果测试任务名称不同,则必须更改系统属性.例如,您的测试任务被命名为"instrumentationTest"属性必须

-DinstrumentationTest.single=SimpleTest
Run Code Online (Sandbox Code Playgroud)

欢呼,勒内


Woo*_*Kim 2

@PeterNiederwieser给了我一个线索。Robolectric我从https://github.com/JCAndKSolutions/android-unit-test获得了一个新的 Android Studio 插件

任何像我一样想要相同解决方案的人都可以使用这个项目来解决问题。

build.gradle你也可以像这样使用它

buildscript {
repositories {
    mavenCentral()
    mavenLocal()

}

dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
        classpath 'com.github.jcandksolutions.gradle:android-unit-test:1.0.1'
    }
}
Run Code Online (Sandbox Code Playgroud)

现在../gradlew clean check -Dtest.single=SomeTest效果很好。