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

问题描述 投票:8回答:3

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

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

./gradlew -Dtest.single=SimpleTest test

在我的根文件夹中。

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

../gradlew -Dtest.single=SimpleTest test

我尝试了很多像这样的方法,但它总是运行我所有的测试。(所有类中的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'
    }
}
android testing gradle android-studio robolectric
3个回答
8
投票

符号

-Dtest.single=SimpleTest

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

-DinstrumentationTest.single=SimpleTest

欢呼,勒内


3
投票

这对我有用:

./gradlew testDebug --tests *TaskCommandSpec

使用testDebugtestRelease而不仅仅是test。如果你有构建变体,那么使用例如testProRelease

docs:https://docs.gradle.org/current/userguide/userguide_single.html


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

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

© www.soinside.com 2019 - 2024. All rights reserved.