【问题标题】:How to run ./gradlew test如何运行 ./gradlew 测试
【发布时间】:2017-01-06 13:09:04
【问题描述】:

我正在尝试从命令行测试应用程序。当我从 Android Studio 运行测试时,一切正常。但是当我尝试运行命令时:

./gradlew test

我得到这个错误:

Task 'test' not found in root project 'mvp_kotlin_example'.

我在使用 ./gradlew build 或 ./gradlew clean 时没有问题。我刚刚创建了一个项目来尝试 ./gradlew 测试并且它有效。

在 mvp_kotlin_example 中,我使用 Robolectric 进行测试,使用 Kotlin 进行开发。

那么我怎样才能让我的测试从命令行运行呢?

编辑:

这是一个开源项目,所以所有代码都可以在

https://github.com/leandroBorgesFerreira/mvp-kotlin-example

这是我的 build.gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply from: '../jacoco.gradle'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        applicationId "br.com.simplepass.simplepassnew"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled = true
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    testCompile "org.robolectric:robolectric:3.1.4"
    testCompile 'com.squareup.okhttp3:mockwebserver:3.3.1'
    testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'
    testCompile "org.mockito:mockito-core:2.4.2"
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile 'br.com.simplepass:loading-button-android:1.5.0'
    compile 'org.jetbrains.anko:anko-sdk15:0.9' // sdk19, sdk21, sdk23 are also available
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.1.6'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
    compile 'com.google.dagger:dagger:2.8'
    kapt 'com.google.dagger:dagger-compiler:2.8'
    provided 'org.glassfish:javax.annotation:10.0-b28'
}
repositories {
    mavenCentral()
}

kapt {
    generateStubs = true
}

编辑2:

这是我在运行 ./gradlew 任务后看到的(它的任务比平时少很多)

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'mvp_kotlin_example'.
components - Displays the components produced by root project 'mvp_kotlin_example'. [incubating]
dependencies - Displays all dependencies declared in root project 'mvp_kotlin_example'.
dependencyInsight - Displays the insight into a specific dependency in root project 'mvp_kotlin_example'.
help - Displays a help message.
model - Displays the configuration model of root project 'mvp_kotlin_example'. [incubating]
projects - Displays the sub-projects of root project 'mvp_kotlin_example'.
properties - Displays the properties of root project 'mvp_kotlin_example'.
tasks - Displays the tasks runnable from root project 'mvp_kotlin_example'.

Other tasks
-----------
clean

【问题讨论】:

  • 请出示build.gradle文件。
  • 在我看来你有 androidTest(与传统的单元测试相反)。为此,命令是 ./gradlew connectedAndroidTest(或者您可以使用缩写形式 ./gradlew cAT
  • Budius,我没有使用 androidTest。我所有的测试都在 test/java/[mypackage] 中,我正在尝试使用传统的单元测试。它适用于 Android Studio
  • @LeandroBorgesFerreira 执行./gradlew tasks 后你看到了什么?
  • 查看已编辑的问题。结果比平时少。

标签: android unit-testing gradle android-gradle-plugin gradlew


【解决方案1】:

这里的问题与您的 build.gradle 无关,您的项目缺少 settings.gradle 文件,该文件告诉 gradle 将 app 文件夹作为模块包含在内。我在您的项目上创建了 PR #1 来解决此缺陷,合并后您应该能够从根项目目录运行 ./gradlew clean test

https://github.com/leandroBorgesFerreira/mvp-kotlin-example/pull/1

解决方案是在根项目中添加一个单行 settings.gradle 文件,用于声明构建中包含的模块。

$ cat settings.gradle 
include 'app'

【讨论】:

    猜你喜欢
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 2020-02-21
    相关资源
    最近更新 更多